We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now
I'm studying The Definitive Guide to DAX and get confused on how filter context works.
In the measure below (p193), which filter context is applied to values('Date'[Date])? The external filter context or the new context created by filter(all('Date'[Date])? Why?
Similarly, which filter context is applied to YEAR ( 'Date'[Date] ? The external filter context or the new context created by filter(ll('Date'[Date])?
According to the textbook, the measure should work similarly as Dateadd(). So I have written the measure below.
Solved! Go to Solution.
Hi, @Jeanxyz
The same logic is difficult to reproduce in a general dax formula( without considering the number of days), which has been mentioned in 'The Definitive Guide to DAX'.
The following formula is similar, but not completely equivalent.
TREATAS(
SELECTCOLUMNS(
CALCULATETABLE(DISTINCT('Date'[Date])),
"Date",DATE(
YEAR('Date'[Date]),
MONTH('Date'[Date])-1,
DAY('Date'[Date])
)
),
'Date'[Date]))
This formula will work normally only when the target month and the current month have the same number of days, otherwise several days(1day=31-30, 2days=29-31,3days=28-31 ) will be lost or included.
Best Regards,
Community Support Team _ Eason
Hi, @Jeanxyz
The same logic is difficult to reproduce in a general dax formula( without considering the number of days), which has been mentioned in 'The Definitive Guide to DAX'.
The following formula is similar, but not completely equivalent.
TREATAS(
SELECTCOLUMNS(
CALCULATETABLE(DISTINCT('Date'[Date])),
"Date",DATE(
YEAR('Date'[Date]),
MONTH('Date'[Date])-1,
DAY('Date'[Date])
)
),
'Date'[Date]))
This formula will work normally only when the target month and the current month have the same number of days, otherwise several days(1day=31-30, 2days=29-31,3days=28-31 ) will be lost or included.
Best Regards,
Community Support Team _ Eason
Hi,
I am not sure of what you want but i think you want to calculate the sales of the previous month. Try this measure
=calculate([Sales Amount],previousmonth('Date'[Date]))
Please ensure that you drag Year and Month from the Date table to your visual and there is a relationship (Many to One and Single) from the Date column of your Data Table to the Date column of your Date table.
Hope this helps.
Your first measure is missing the transition from January to December of the prior year. The book has that shown correctly.
The applied filter context is ALL(Date[Date]).
@lbendlin is totally correct that the expression doesn't handle dates in January going back to December.
However I think the answer to your other question about context needs expanding.
ALL(Date[Date]) is only providing the base table for the iterator. FILTER doesn't modify the filter context (only calculate expressions can do that). Therefore VALUES(Date[Date]) comes from the external context. YEAR(Date[Date]) comes from the row context of the iterator.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
| User | Count |
|---|---|
| 56 | |
| 40 | |
| 36 | |
| 18 | |
| 18 |
| User | Count |
|---|---|
| 70 | |
| 67 | |
| 38 | |
| 34 | |
| 23 |