Wiki Power BI and DAX
CALCULATE: changing the filter context
CALCULATE evaluates an expression in a modified filter context. A Boolean filter replaces whatever already existed on the same column.
CALCULATE ( expression, filter1, filter2, ... ) computes the expression after modifying the filter context with the given filters. It is the function that lets you ask "what if the filter were different?": revenue for one category only, revenue without the category filter, revenue for last year.
Each filter acts on the current context in one of these ways: a Boolean argument such as Product[Category] = "Bags" counts as FILTER ( ALL ( Product[Category] ), ... ), and REMOVEFILTERS clears filters on a column or table. That is why a Boolean filter replaces the existing filter on the same column, and does not touch the filters on other columns.
Bags Revenue =
CALCULATE ( [Revenue], Product[Category] = "Bags" )
Revenue Without Product Filter =
CALCULATE ( [Revenue], REMOVEFILTERS ( Product ) )
Share = DIVIDE ( [Revenue], [Revenue Without Product Filter] )In a visual with Product[Category] on the rows, the "Backpacks" row shows the revenue of Bags in Bags Revenue: the row's filter, on the Category column, was swapped. A filter on Calendar[Year], however, still applies, because it is a different column.
Pitfall
Expecting the CALCULATE filter to combine with what the user picked in the Product[Category] slicer. It does not combine: it replaces. The slicer no longer has any effect on this measure.
To keep the existing filter and intersect it with the new one, use KEEPFILTERS:
Filtered Bags Revenue =
CALCULATE ( [Revenue], KEEPFILTERS ( Product[Category] = "Bags" ) )KEEPFILTERS keeps the filter that already existed and intersects it with the new one. With "Backpacks" on the row, the intersection is empty and the measure returns blank, which is the expected behavior. Choose consciously between replacing and intersecting.
Sources
Shared knowledge from Genesis Innovation. Not a claim about the person behind this site.