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 reviews how CALCULATE works, because everything later in this tutorial depends on it. USERELATIONSHIP does nothing on its own. It only works as an argument of CALCULATE, and it only makes sense once you see what CALCULATE does to the filters around a measure.
The examples use the model from the rest of the tutorial. A Sales fact table has OrderDate and ShipDate columns. A 'Date' dimension has Date and Year columns. The active relationship connects Sales[OrderDate] to 'Date'[Date].
What filter context is
A filter context is the set of filters that are active when DAX evaluates a measure. Filters come from the report: a row or column in a matrix, a slicer, a visual-level filter, or a page filter. A cell in a matrix that shows 2024 on its row has a filter 'Date'[Year] = 2024 in its filter context.
Take this measure:
Sales Amount = SUM ( Sales[SalesAmount] )The measure does not mention a year. It sums whatever rows of Sales survive the current filter context. For the 2024 row of a matrix, the filter 'Date'[Year] = 2024 first restricts the Date table to 2024 dates. The active relationship then passes that restriction to Sales through Sales[OrderDate]. Only sales whose order date falls in 2024 remain, and SUM adds them up.
The measure never changes. Only the filter context around it changes from cell to cell. This is why one measure can serve a whole report.
What CALCULATE does
CALCULATE evaluates an expression in a filter context that you modify. The syntax is:
CALCULATE ( <expression>, <filter or modifier>, <filter or modifier>, ... )The first argument is the expression to evaluate. It is usually a measure or an aggregation. The remaining arguments describe how to change the filter context before evaluating it. Each one is either a filter that adds or replaces filters, or a modifier that changes how the model behaves during the calculation. USERELATIONSHIP is a modifier.
Here is a measure that always returns 2024 sales:
Sales 2024 =
CALCULATE (
[Sales Amount],
'Date'[Year] = 2024
)When this measure is evaluated in the 2023 row of a matrix, the cell does not show 2023 sales. CALCULATE replaces the existing filter on 'Date'[Year] with 'Date'[Year] = 2024, so the cell shows 2024 sales.
How a filter argument is interpreted
A Boolean filter such as 'Date'[Year] = 2024 is shorthand. DAX rewrites it as a table filter on one column:
Sales 2024 Explicit =
CALCULATE (
[Sales Amount],
FILTER ( ALL ( 'Date'[Year] ), 'Date'[Year] = 2024 )
)The ALL ( 'Date'[Year] ) part is what removes the existing year filter. FILTER then keeps only the value 2024. Two rules follow from this shorthand:
- A Boolean filter can reference only one column of one table. A condition that involves two columns, or a measure, needs an explicit
FILTER. - The replacement affects only the columns named in the filter. A filter on
'Date'[Year]replaces other filters on'Date'[Year]. It leaves filters on'Date'[MonthNumber]or on other tables alone.
Replace, remove, and keep
Most of the time, you want one of three behaviors. The measures below show them side by side.
Sales Amount = SUM ( Sales[SalesAmount] )
Sales 2024 =
CALCULATE ( [Sales Amount], 'Date'[Year] = 2024 )
Sales 2024 Kept =
CALCULATE ( [Sales Amount], KEEPFILTERS ( 'Date'[Year] = 2024 ) )
Sales All Dates =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( 'Date' ) )
Share of All Dates =
DIVIDE ( [Sales Amount], [Sales All Dates] )Suppose the model holds 400 of sales in 2023 and 600 in 2024, all by order date. A matrix with years on rows shows:
| Year | Sales Amount | Sales 2024 | Sales 2024 Kept | Sales All Dates | Share of All Dates |
|---|---|---|---|---|---|
| 2023 | 400 | 600 | (blank) | 1000 | 40% |
| 2024 | 600 | 600 | 600 | 1000 | 60% |
| Total | 1000 | 600 | 600 | 1000 | 100% |
Read the table column by column:
- Sales 2024 replaces the row's year filter. The 2023 row shows 600 because the filter 2023 was swapped for 2024.
- Sales 2024 Kept uses
KEEPFILTERS, which adds the new filter on top of the existing one instead of replacing it. On the 2023 row, the filter context requires both 2023 and 2024. No date satisfies both, so the result is blank. - Sales All Dates removes every filter on the
Datetable. Each row gets the grand total. This is the standard denominator for a percentage of total.
REMOVEFILTERS can only be used as a CALCULATE modifier. ALL does the same job in that position, but it can also return a table, so REMOVEFILTERS states the intent more clearly.
Relationships carry the changed filter to the fact table
Changing the filter on 'Date'[Year] has an effect on Sales only because a relationship connects the tables. CALCULATE changes the filters on columns. The model then propagates them along the active relationship, from the one side (Date) to the many side (Sales), as in any other report filter.
This matters for the rest of the tutorial. In Sales 2024, the phrase "2024" means "dates in 2024 as seen through Sales[OrderDate]", because the active relationship uses order date. The measure has no way to ask for ship date. Nothing you put in a filter argument on 'Date' can change which Sales column the Date table filters. The relationship decides that.
CALCULATE has a family of arguments that change the model's behavior instead of the column filters. USERELATIONSHIP tells the engine to use a different relationship for the duration of the calculation. CROSSFILTER changes the direction of a relationship, or turns it off. The next chapters cover USERELATIONSHIP.
Order of evaluation
Two details of the order help you predict results.
First, the filter arguments are evaluated in the original filter context, not in the new one. Arguments do not see each other's changes. A filter argument that calls a measure or a table function sees the report's filters as they were when CALCULATE started.
Second, after the arguments are evaluated, the engine applies relationship modifiers such as USERELATIONSHIP, then removes the filters that the arguments clear, and only then applies the new filters. The final filter context is the same however you list the arguments, so you can put them in the order that reads best. The one exception is nested CALCULATE calls. The inner call modifies the context that the outer call produced, so the inner arguments win when the two conflict.
Sales 2023 via Nested =
CALCULATE (
CALCULATE ( [Sales Amount], 'Date'[Year] = 2024 ),
'Date'[Year] = 2023
)The outer call sets the year to 2023. The inner call then replaces it with 2024, so this measure returns 2024 sales. Nesting is rarely needed. It is a reminder that the innermost CALCULATE has the last word.
Context transition in one paragraph
CALCULATE has a second job. When it runs inside a row context, for example inside an iterator such as SUMX or in a calculated column, it converts the current row into filters. This is called context transition. It is also why a measure reference such as [Sales Amount] behaves differently from a bare column reference inside an iterator. A measure reference is wrapped in an implicit CALCULATE.
Best Year Sales =
MAXX ( VALUES ( 'Date'[Year] ), [Sales Amount] )For each year in the iteration, the implicit CALCULATE turns that year into a filter on 'Date'[Year], and [Sales Amount] returns that year's sales. You do not need more than this for the rest of the tutorial. Keep in mind that USERELATIONSHIP inside such an iterator still works, because the relationship modifier applies to the CALCULATE that evaluates the measure.
Common errors
These errors appear in nearly every model that uses CALCULATE. They also show up when people first write USERELATIONSHIP measures.
| Symptom | Likely cause | Fix |
|---|---|---|
| Error: a function was used in a True/False expression that references a measure | A measure appears in a Boolean filter argument | Use `FILTER ( table, condition )` and put the measure in the condition |
| Error: a Boolean filter references more than one column | A condition such as `Sales[Qty] > 5 && 'Date'[Year] = 2024` | Split it into two filter arguments, or use `FILTER` |
| A slicer on the column seems ignored | The filter argument replaced the slicer's filter on that column | Use `KEEPFILTERS` if the slicer must still apply |
| Percent of total is always 100% | `REMOVEFILTERS` or `ALL` targets a table that the visual does not filter on | Remove filters from the table or column the visual actually uses |
| Result is the same on every row | The filter argument replaces the filter that creates the rows | Check which column forms the rows, and filter a different column |
The second row of the table is worth noticing for this tutorial. You cannot choose the date role by writing a Boolean filter on Sales[ShipDate] and hoping it behaves like a filter on the Date table. A filter on Sales[ShipDate] filters Sales directly. It does not drive the Date table, so the year columns, month names, and time intelligence functions on Date still reflect the original context.
Where this leads
You now have the vocabulary the later chapters use. A filter context is a set of column filters. CALCULATE replaces, removes, or keeps those filters, and applies modifiers. The active relationship then decides which fact table column the filtered dimension reaches.
The measure Sales 2024 answers "sales with an order date in 2024". Answering "sales with a ship date in 2024" needs the same year filter on Date, but carried through a different relationship. That is exactly what a relationship modifier inside CALCULATE provides.
Exercise
The matrix has years on rows. Predict the value on the 2023 row for each measure, using 400 for 2023 sales and 600 for 2024 sales: (a) CALCULATE ( [Sales Amount], 'Date'[Year] = 2024 ), (b) CALCULATE ( [Sales Amount], KEEPFILTERS ( 'Date'[Year] = 2024 ) ), (c) CALCULATE ( [Sales Amount], REMOVEFILTERS ( 'Date' ) ) when the grand total is 1000.
Show answer
(a) 600, because the filter argument replaces the row's year filter with 2024. (b) Blank, because KEEPFILTERS combines the row's filter (2023) with the new one (2024), and no date matches both. (c) 1000, because all filters on the Date table are removed, so every sales row is counted.
Exercise
A colleague writes Sales Ship 2024 = CALCULATE ( [Sales Amount], Sales[ShipDate] >= DATE ( 2024, 1, 1 ), Sales[ShipDate] <= DATE ( 2024, 12, 31 ) ) and says it gives sales by ship date for 2024. The measure runs without an error. Why might a matrix with 'Date'[Year] on rows still show misleading numbers, and which part of CALCULATE solves the underlying problem?
Show answer
The filter on Sales[ShipDate] filters the fact table directly, but the matrix rows come from 'Date'[Year], and the Date table is connected to Sales through the active order-date relationship. For each row, the order-date filter for that year is combined with the ship-date filter for 2024. The result shows only sales that were both ordered in the row's year and shipped in 2024, which is not sales by ship date per year. The fix is a relationship modifier, USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ), as an argument of CALCULATE. It makes the Date table filter Sales through ship date for that calculation.