Sub topic 7
CALCULATE: changing the filter context
This chapter assumes you know the difference between a measure and a calculated column. It also assumes you can read a simple filter context, such as a slicer selection, and can write a basic SUM or SUMX measure. Here you learn how CALCULATE changes the filter context before it evaluates an expression.
What CALCULATE does
CALCULATE evaluates an expression in a filter context that you modify. It takes an expression as its first argument. The remaining arguments are filters that change the context.
The evaluation runs in this order:
- CALCULATE reads the filter context that already exists, from slicers, visuals, or an outer CALCULATE.
- It applies each filter argument to that context.
- It evaluates the expression in the new context.
The original context is not changed. The new context exists only while the expression is evaluated.
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )The filter Product[Color] = "Red" is shorthand for a table of the single value "Red" in the Color column. CALCULATE puts that filter on Product[Color] and replaces any existing filter on that column. Then it evaluates [Sales Amount] in the new context.
Replacing an existing filter
Suppose a slicer selects Blue on Product[Color]. A visual then shows Red Sales. The result is still red sales. The filter argument replaced the slicer's filter on that column.
Filters on other columns stay in place. If the slicer also selects a year, Red Sales shows red sales for that year. Only the filter on the same column is replaced.
| Existing filter | CALCULATE filter | Result for that column |
|---|---|---|
| Color = Blue | Color = "Red" | Red only |
| Year = 2025 | Color = "Red" | Year 2025 stays, Color is Red |
| No filter | Color = "Red" | Red only |
Keeping the existing filter
Sometimes you want the new filter to narrow the selection instead of overriding it. KEEPFILTERS does this. It intersects the new filter with the existing filter on the column.
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)If the slicer selects Blue, this measure returns blank because no row is both Blue and Red. If the slicer selects Red and Blue, it returns red sales. If nothing is selected on Color, it returns red sales.
Filter arguments are evaluated in the outer context
Each filter argument is evaluated in the context that existed before CALCULATE started. The filters do not see one another. This matters when a filter argument is itself a table expression. It is computed first, from the original context, and the result is then applied.
Common errors
- Expecting CALCULATE to add to a filter on the same column. By default it replaces. Use KEEPFILTERS to intersect.
- Using a measure in a Boolean filter, such as
[Sales Amount] > 1000. A Boolean filter must reference a column. Use FILTER over a table for that case. - Assuming the filter argument changes the visual's filter. It only affects the expression inside this CALCULATE.
Exercise
A slicer selects Color = Blue. The measure CALCULATE ( [Sales Amount], Product[Color] = "Red" ) is shown in a visual. What does it return, and what would change if the filter were wrapped in KEEPFILTERS?
Show answer
Without KEEPFILTERS it returns red sales, because the filter argument replaces the slicer's filter on Product[Color]. With KEEPFILTERS the two filters are intersected, so no row is both Blue and Red and the measure returns blank.
Exercise
A page has a slicer on Year = 2025. A measure uses CALCULATE ( [Sales Amount], Product[Color] = "Red" ). Does the Year filter still apply?
Show answer
Yes. CALCULATE replaces filters only on the columns named in its filter arguments. The filter on Year is on a different column, so it stays in the new filter context.