Forum Discussion
Why does CALCULATE behave like this when using FILTER?
- 8 years ago
This is a lovely question
My article only covers the basics
Also see what happens with
Column = CALCULATE(SUM(Table1[earnings]),Table1)
Actually it has to do with the order in which arguments are evalauted.
Filter Parameter of the Calculate is exected firstPlease see this article
https://www.sqlbi.com/articles/order-of-evaluation-in-calculate-parameters/
So when you introduce Filter(Table1), it introduces an UnFiltered Table in a Calculated Column (ROW CONTEXT). So it has the impact of removing the FILTERS
When you useColumn = CALCULATE(SUM(Table1[earnings]),Table1[Year] = 2011
This is internally transformed by DAX Engine into following formula'See the article https://www.sqlbi.com/articles/filter-arguments-in-calculate/
Column = CALCULATE(SUM(Table1[earnings]),Filter(all(Table1[Year]),Table1[Year] = 2011))
So this formula retains all other filters except for Table1 Year which you modify to be 2011
Now if you want your second formula to give same results, you would have to use
Column = CALCULATE(SUM(Table1[earnings]),FILTER(RelatedTable(Table1), Table1[Year] = 2011))
This is a lovely question
My article only covers the basics
Also see what happens with
Column = CALCULATE(SUM(Table1[earnings]),Table1)
Actually it has to do with the order in which arguments are evalauted.
Filter Parameter of the Calculate is exected first
Please see this article
https://www.sqlbi.com/articles/order-of-evaluation-in-calculate-parameters/
So when you introduce Filter(Table1), it introduces an UnFiltered Table in a Calculated Column (ROW CONTEXT). So it has the impact of removing the FILTERS
When you use
Column = CALCULATE(SUM(Table1[earnings]),Table1[Year] = 2011
This is internally transformed by DAX Engine into following formula'
See the article https://www.sqlbi.com/articles/filter-arguments-in-calculate/
Column = CALCULATE(SUM(Table1[earnings]),Filter(all(Table1[Year]),Table1[Year] = 2011))
So this formula retains all other filters except for Table1 Year which you modify to be 2011
Now if you want your second formula to give same results, you would have to use
Column = CALCULATE(SUM(Table1[earnings]),FILTER(RelatedTable(Table1), Table1[Year] = 2011))