Reputation: 9664
In the CALCULATE function, how are filters applied when more than 1 filter expression is provided?
Example:
CALCULATE(expr, filter1, filter2)
The 1st filter takes the entire filter context (all the tables involves in the relation) and applies filter1 to it. Then it does the same for the 2nd filter separately. Then it intersects boths the above results and performs the calculation expression on the resultant dataset?
I am confused because if the above is true, then how does using VALUES override any filters applied (for example using ALL)?
Upvotes: 0
Views: 580
Reputation: 4877
CALCULATE
function is executed as a sequence of steps. A very good explanation of these steps can be found on dax.guide site page for CALCULATE
assuming to have a CALCULATE
expression like
CALCULATE( [Sales], 'Product'[Color] = "Red", 'Product'[Brand] = "Contoso" )
then the [Sales]
measure is evaluated in a filter that takes the products that are "Red"
and which brand is "Contoso"
. Both filters are evaluated in step #1 and later applied during step #5.
if instead the expression is
CALCULATE( [Sales], 'Product'[Color] = "Red", ALL( 'Product'[Brand] ) )
first the 'Product'[Color] = "Red"
filter is evaluated for step #1, then for step #4 the ALL
modifier is applied, removing any existing filter over 'Product'[Brand]
and later for step #5 the filter evaluated in step #1 over 'Product'[Color]
is applied
Since the filter is evaluated in step #1 and applied in step #5, it's possible to use VALUES
to save an existing filter to be applied after the step #4, where filters might be removed.
Assuming that a filter over 'Product'[Brand]
exists when this CALCULATE
is evaluated
CALCULATE( [Sales], ALL( 'Product' ), VALUES( 'Product'[Brand] ) )
what happens is that in step #1 VALUES( 'Product'[Brand] )
is evaluated, saving the existing filter over 'Product'[Brand]
, then in step #4 the ALL( 'Product' )
modifier removes any existing filter over the whole 'Product'
table and finally the step #5 applies back the filter over 'Product'[Brand]
that was evaluated in step #1
The ALL() VALUES()
pattern is explained in detail in this article Using ALLEXCEPT versus ALL and VALUES
Upvotes: 2
Reputation: 16908
You can try this below syntax-
total =
CALCULATE(
SUM(table_name[column_name]),
FILTER(
ALL(table_name),
table_name[filter_column_name1] = "ABC"
&& table_name[filter_column_name2] = 10
)
)
Upvotes: 0