Forum Discussion
what's the difference creating the filter as a variable or using inside of the filter ?
- 2 years ago
Hey Anonymous ,
I think you may have got things the wrong way round in your post? When you use the measure you won't get a consistent value to compare against each row. When using a measure in another function an implicit CALCULATE is wrapped around the measure and context transition occurs changing that row context into a filter context. Here's an example
- Using MAX directly as a function you will get the same value across every row (the max date in the table). This is because a row has no filter context in this scenario
- Using [Max Date] measure you get a date that is equal to the max date in that rows filter context. This is because Power BI actually runs CALCULATE( [Max Date] ) under the hood.
- Finally for clarity if you run CALCULATE( MAX( date ) ) then you will get the same result as [Max Date]
This can be seen below. I've used ADDCOLUMNS as an example as it is also an iterator like FILTER
When context transition occurs it changes each row into a filter context. Since my dates are unique the Max Date for each row is equal to the date in the row itself. It may be clearer to see in this example using a simple SUM
Because rows 1 and 2 are duplicates the context of that calculation is "Give me the sales when Product = A and Sales = 200" since 2 rows satisfy the condition you can see we get 400 as a total. If the sales were different and it wasn't a duplicate you would get a different result since "Give me the sales when Product = A and Sales = 200" and "Give me the sales when Product = A and Sales = 300" are different.
brickanalyst this is just scratching the surface but when I started to understand context transition my DAX became so much more powerful. Funny I missed this was an interview question and one I've used in the past. Understanding this shows you have a deep knowledge of evaluation contexts.
Hope it helps to clarify,
Kris
Hey brickanalyst ,
Yeah this is a tricky one. When using the [Max Date] measure in another measure an implicit CALCULATE is wrapped around the function. Since FILTER is an iterator context transition occurs turning each row in the DatesTable into a filter context.
You would achieve the same results if you changed your second formula to read
FILTER(ALL(DatesTable), DatesTable[Date] <= CALCULATE( MAX(DatesTable[Date]) ) )
Check out context transition and implicit CALCULATE around measures for more info.
Hope it helps,
Kris