Sub topic 5
Recap: CALCULATE changes filter context
This chapter assumes you can write simple DAX measures such as SUM, and that you know the star schema terms fact table, dimension table, and one-to-many relationship. It refreshes one idea you need before USERELATIONSHIP: CALCULATE changes the filter context that a measure is evaluated in.
Filter context in one paragraph
Filter context is the set of filters that apply when DAX evaluates a measure. A slicer, a row or column in a matrix, or a filter on a report page each add filters to it. The filters sit on columns, such as 'Date'[Year] = 2025. They reach the fact table through active relationships.
Take this measure:
Sales Amount = SUM ( Sales[SalesAmount] )The formula never changes. In a matrix with years on the rows, each row evaluates it in a different filter context, so each row shows a different total.
What CALCULATE does
CALCULATE evaluates an expression in a modified filter context. Its first argument is the expression. Every later argument is a filter modifier that changes the filter context before the expression runs.
Sales 2025 =
CALCULATE (
[Sales Amount],
'Date'[Year] = 2025
)This measure replaces any existing filter on 'Date'[Year] with 2025. The expression [Sales Amount] then runs in that new context. Filters on other columns, such as a product category from a slicer, stay in place.
The steps run in this order:
- DAX reads the current filter context from the visual.
CALCULATEapplies each filter argument. A filter on a column replaces an existing filter on the same column.- The expression is evaluated in the new context.
Modifiers are not only column filters
A filter argument that is a Boolean condition, such as 'Date'[Year] = 2025, is the most common modifier. CALCULATE also accepts other kinds:
| Modifier | What it does | Example |
|---|---|---|
| Column filter | Sets values on a column | `'Date'[Year] = 2025` |
| Table filter | Sets the filter from a table of values | `DATESYTD ( 'Date'[Date] )` |
| Filter removal | Clears existing filters | `ALL ( 'Date' )` |
| Relationship modifier | Changes which relationship carries filters | `USERELATIONSHIP ( ... )` |
USERELATIONSHIP belongs in the last row. It does not filter any column to a value. It changes how filters travel between two tables while the expression runs. That is why it can only appear as an argument of CALCULATE or CALCULATETABLE.
Why this matters for relationships
An active relationship decides how a filter on the dimension reaches the fact table. In the model used in this tutorial, Sales[OrderDate] connects to 'Date'[Date]. When a 'Date' filter is in the context, the fact table is filtered by order date.
Because CALCULATE can alter that propagation for one calculation, you can ask the same measure to use a different relationship without editing the model. The next chapters build on that.
Sales Amount by Ship Date =
CALCULATE (
[Sales Amount],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)The expression is still [Sales Amount]. Only the relationship that carries the 'Date' filter to Sales has changed.
Common errors to watch for
- Expecting a filter argument to add to an existing filter on the same column. It replaces it. To keep the existing filter, combine them, for example with
KEEPFILTERS. - Putting the new filter outside
CALCULATE. A measure such as[Sales Amount] * 2never changes filter context. OnlyCALCULATEdoes. - Forgetting that the expression is evaluated last. All modifiers apply first, then the expression runs once in the resulting context.
Exercise
A matrix shows Sales Amount by 'Date'[Year] for 2024 and 2025. A measure is defined as CALCULATE ( [Sales Amount], 'Date'[Year] = 2023 ). What does each row of the matrix show, and why?
Show answer
Every row shows the sales for 2023. CALCULATE replaces the filter that the matrix row put on 'Date'[Year] with the filter 'Date'[Year] = 2023, so the 2024 and 2025 rows both evaluate the expression in a 2023 context.
Exercise
Which category of CALCULATE argument is USERELATIONSHIP, and what does it change?
Show answer
It is a relationship modifier. It does not set a value on a column. It changes which relationship between two tables carries filters while the CALCULATE expression is evaluated.