I have a Power BI matrix with 3 columns: 2019, 2020, and 2021. Each row is an item number, and each value is a total quantity produced in the corresponding year column.
I am trying to find a way to display the sum of ONLY items that were blank in 2019 and 2020, BUT have a total quantity in the 2021 column. Is there a way to conditionally format this?
Basically, another way of saying the logic is: total sum of all items where 2019 AND 2020 are blank, AND 2021 is not blank.
Try this solution if it helps then mark it as the answer.
Create a calculated column with the following DAX:
Column =
IF (
(
Table[Year] = 2019 || Table[Year] = 2020 ),
IF ( ISBLANK ( Table[Item] ), ( Table[Quant] ) ),
IF ( Table[Year] = 2021, IF ( ISNUMBER ( Table[Item] ), ( Table[Quant] ) ) )
)
Input I used:-
Output where 2019 and 2020 Items are blank and 2021 where Items are not blank:-
You can create the following measure:
Measure =
IF (
CALCULATE(
SUM(Table[Quantity]),
Table[Year] => 2019,
Table[Year] <= 2020,
) > 0,
0,
CALCULATE(
SUM(Table[Quantity]),
Table[Year] = 2021
)
)
And in the table you should filter that visual so it only shows values where this Measure is > 0. However, if your intention is to have this dynamically (i.e. so this still makes sense next year with years 2020, 2021 and 2022), I would recommend this:
Measure =
IF (
CALCULATE(
SUM(Table[Quantity]),
Table[Year] => YEAR(TODAY()) - 2,
Table[Year] <= YEAR(TODAY()) - 1,
) > 0,
0,
CALCULATE(
SUM(Table[Quantity]),
Table[Year] = YEAR(TODAY())
)
)
Related
I have a Financial Year filter that does not select before 2021 . This is because there is PAGE level Filter that only allows the last 2 years , so only 2021 to 2023 .
Problem is that I have a 14 w Rolling Average, that when the 1st week in 2021 is selected, it does not work .
Code for measure
CALCULATE (
DISTINCTCOUNT ( Sales[activity_id] ),
FILTER (
Sales,
Sales[done] = "Yes"
&& Sales[activity_type]
IN { "In-Person Meeting", "Virtual Meeting", "Site Visit" }
) )
Code for Caluclation group
VAR __lastVisibleDate =
LASTDATE ( 'Calendar'[Date] )
VAR _fromDate =
CALCULATE ( DATEADD ( __lastVisibleDate, -91, DAY ), ALL ( 'Time') )
VAR __result =
CALCULATE (
SELECTEDMEASURE (),
REMOVEFILTERS ( 'Calendar' ),
'Calendar'[Date] > _fromDate
&& 'Calendar'[Date] <= __lastVisibleDate
)
VAR _result_divide =
DIVIDE ( __result, 13, 0 )
RETURN
IF ( _result_divide < 0.1, BLANK (), _result_divide )
I have tried every combination of ALL('Calendar' ) to get the rolling 14 week average to work for the 1st week of Jan 2021 . Debugging the _fromDate does give me the 20/10/2020 but the rolling average still does not work out correctly ! Argghhh !
Any ideas ? This is proving quite diffcicult
The PAGE level filter is needed to allow the user to select the financial year .
I have a measure that totals the values for each date in the table. I want to filter this measure so that I can display only the last 28 days present in the table instead of displaying values for all the dates. Following is the code that works for getting totals for full table:
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) ) )
The 'relative date' filter in the Filters pane does not work because it only accepts the last 28 days based on today's date and not the dates in the table. Please suggest a DAX formula that can filter for the last 28 days present in the table.
Try this code
VAR endDay = LastDate(Daily_Reports[Case_Date])
VAR startDay= DATEADD(endDay,-28,DAY)
VAR setOfDates = DATESBETWEEN(Daily_Reports[Case_Date], StartDate, EndDate )
RETURN
CALCULATE(
SUM(Daily_Reports[Confirmed])
,setOfDates
)
You can try this one:
MS =
CALCULATE (
SUM ( Daily_Reports[Confirmed] ),
FILTER (
ALL ( Daily_Reports[Case_Date] ),
Daily_Reports[Case_Date]
>= SELECTEDVALUE ( Daily_Reports[Case_Date] ) - 28
)
)
This is what finally worked for me. I created a measure (not a column), that returns 1 for the last 28 days with an IF clause, leaving it blank if the date is not in the last 28 days as follows:
Last28 =
VAR MaxDate = LASTDATE( ALL(Daily_Reports[Case_Date]) )
VAR MinDate = DATEADD( MaxDate, -28, DAY )
RETURN
IF( SELECTEDVALUE(Daily_Reports[Case_Date]) >= MinDate && SELECTEDVALUE(Daily_Reports[Case_Date]) <= MaxDate, 1 )
Then I incorporated this measure into the Calculate function as follows:
Daily Cases =
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) && NOT(ISBLANK(Daily_Reports[Last28]))
)
)
I have the following fields:
Year
Category
Maker
Month
Month Number
Sales Volume
Sales
Date
So, I have in my dash a filter for "Month Number" and "Year":
My goal is to create two new measure; first with the Rolling Year that need to sum 12 months, ending in the moment that the user select in the mencioned filters. For example if y select Year 2021 and Month 01. The Rolling Year need to sum the sales of a selected category since 2020-02 to 2021-01 (thats mean always 12 months since a pivot month).
For thesecond is exactly the same, a measure called Rolling Last Year, it need to be a rolling sum too, but for the last period in order to compare. Taking the same example if I have the period 2020-02 to 2021-01. The Rolling Last Year for the last period should be 2019-02 to 2020-01.
I tried with this DAX code, that extracted from Microsoft page but without success:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL (Table[Date] ),
AND (
Table[Date] <= MAX ( Table[Date] ),
DATEADD ( Table[Date], 12, MONTH ) > MAX ( Table[Date] ))))
I share you an extract of my base:
Thanks in advance !!!
Based on the table and code you have shared, it is unclear from where the date filters are being applied. In case you have not done it, I strongly suggest to delete the [Month] and [Month Number] field from your Sales table and keep them in a separate Calendar table, from where the filters should be selected. Then, a simple tweak on you current formula should do the trick:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL ('Calendar'[Date] ),
AND (
Table[Date] <= MAX ( 'Calendar'[Date] ),
DATEADD ( 'Calendar'[Date], 12, MONTH ) > MAX ( 'Calendar'[Date] ))))
However you can try with this variation for the code, a little bit optimized so as not to scan your whole Sales table each time:
Rolling Year =
VAR EndSelectedDate = MAX ( 'Calendar'[Date] )
VAR StartSelectedDate =
CALCULATE (
MAX ( 'Calendar'[Date] ),
ALL ( 'Calendar'[Year] ),
'Calendar'[Year]
= MAX ( 'Calendar'[Year] ) - 1
)
RETURN
CALCULATE (
SUM ( Table[Sales] ),
ALL ( 'Calendar' ),
'Calendar'[Date] <= EndSelectedDate,
'Calendar'[Date] > StartSelectedDate
)
I have a requirement in Power bi that i need to display the last month mtd on basis of selection of date slicer.
here is the details
Ex: jan 1 2020 : 10 cases, jan 2, 2020 : 30 cases.. when i select feb 2 2020 in slicer it should display the jan 2 2020 value not mtd of that month(january)
Thanks,
Imran
Without using time intelligence functions, you can probably write something like this:
LastMonthMTD =
VAR DateSelected = SELECTEDVALUE ( 'Calendar'[Date] )
VAR PrevMonth = EOMONTH ( DateSelected, -1 )
VAR PrevDate = DATE ( YEAR ( PrevMonth ), MONTH ( PrevMonth ), DAY ( DateSelected ) )
VAR StartDate = EOMONTH ( DateSelected, -2 )
RETURN
CALCULATE (
[Sum_Covid_Cases],
'Calendar'[Date] > StartDate,
'Calendar'[Date] <= PrevDate
)
Using time intelligence, you could try something like this:
LastMonthMTD =
CALCULATE (
[Sum_Covid_Cases],
DATEADD ( DATESMTD ( 'Calendar'[Date] ), -1, MONTH )
)
I haven't tested these, so let me know if they work or not.
While building a calendar table I came across unexpected hard nut to crack.
How to convert date to the first date of the quarter? Here are some examples:
2019-02-01 > 2019-01-01
2019-03-31 > 2019-01-01
2019-04-02 > 2019-04-01
The function STARTOFQUARTER does not work in my calendar. I do not know why.
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016, 1, 1 ),
DATE ( 2020, 12, 31 )
),
VAR VarDates = [Date]
var YQ_date = STARTOFQUARTER( [Date] ) -- this does not work
RETURN
ROW (
"day" , VarDay,
"YQ_date" , YQ_date
)
)
The only option seems to be adding calculated column to Calendar table. But if it is possible to have it straight, then why not have it straight?
How about as an added column?
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2016, 1, 1 ), DATE ( 2020, 12, 31 ) ),
"YQ_date", EOMONTH ( [Date], -1 - MOD ( MONTH ( [Date] ) - 1, 3 ) ) + 1
)
For the STARTOFQUARTER function to work, you need the dates that you expect to be returned by the function in your datetable.
So in the provided sample, you need to add the dates 2019-01-01 and 2019-04-01.
Something like this:
When you want to build a calendar table with one DAX expression, you cannot use the STARTOFQUARTER function, because it will not work with an in-memory table.
You could use something like this as a workaround though:
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016; 1; 1 );
DATE ( 2020; 12; 31 )
);
ROW (
"YQ_date"; DATE( Year( [Date] ); ROUNDUP( MONTH ( [Date] ) / 3; 0 ) * 3 - 2; 1)
)
)