Forum Discussion

brickanalyst's avatar
brickanalyst
Resolver I
2 years ago
Solved

what's the difference creating the filter as a variable or using inside of the filter ?

I have seen a question in one of the interviews below : Let's say we have a dax measure [Max Date]:= MAX(DatesTable[Date]) then I use it in the following below; FILTER(ALL(DatesTable), DatesT...
  • kriscoupe's avatar
    kriscoupe
    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