Sub topic 4
CALCULATE: evaluating an expression in a modified context
This chapter assumes you can write basic DAX measures and calculated columns. It also assumes you work with a star-schema model with one-to-many relationships, and that you know how slicers, rows and columns in a report filter a measure. Earlier chapters covered filter context, how filters flow through relationships, and row context. Here we put them to work with CALCULATE, the function that changes filter context.
What CALCULATE does
A measure such as [Total Sales] has no fixed answer. Its result depends on the filter context it is evaluated in. In a matrix, each cell supplies its own filter context from the row, column, slicer and page filters.
CALCULATE evaluates an expression in a filter context that you modify. It takes the current filter context, applies your changes, and then evaluates the expression in the result.
Total Sales = SUM ( Sales[Amount] )This is the base measure used throughout the chapter. On its own it responds only to the filters that the report supplies.
The syntax is:
CALCULATE ( <expression>, <filter1>, <filter2>, ... )- The first argument is the expression to evaluate. It is usually a measure or an aggregation.
- Every other argument is a filter argument. Each one describes a change to the filter context.
- The filters are optional.
CALCULATE ( [Total Sales] )with no filters is legal, though it matters for context transition, covered later.
CALCULATE does not "add a filter after the fact" to a result. It builds a new filter context first, then evaluates the expression in that context.
Three kinds of filter argument
CALCULATE accepts three kinds of argument after the expression. Later chapters treat each in detail, so this section only names them and shows how to recognize them.
| Kind | Example | Typical effect |
|---|---|---|
| Boolean expression | `'Product'[Color] = "Red"` | Replaces the filter on that one column |
| Table expression | `FILTER ( Sales, Sales[Quantity] > 2 )` | Filters using the rows of a table |
| Filter modifier | `REMOVEFILTERS ( 'Product'[Category] )` | Removes or changes how filters apply |
You can mix these in one call. Separate each filter argument with a comma.
A first example
The next measure shows red sales only.
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)The Boolean argument 'Product'[Color] = "Red" is a filter on the Color column. Because the filter is on the same column that may already be filtered, the new filter replaces any existing filter on 'Product'[Color]. If a slicer selects Blue, Red Sales still returns red sales. Filters on other columns, such as 'Product'[Category] or 'Date'[Year], stay in place.
This replacement rule is the most common source of surprise with CALCULATE. The next chapter examines it in full. For now, remember that a filter argument on a column normally overrides the existing filter on that column.
Boolean arguments are shorthand
A Boolean filter argument is not evaluated row by row on the model. DAX rewrites it as a table expression over one column. The Red Sales filter behaves like this:
Red Sales Expanded =
CALCULATE (
[Total Sales],
FILTER (
ALL ( 'Product'[Color] ),
'Product'[Color] = "Red"
)
)ALL ( 'Product'[Color] ) returns every distinct color, ignoring any current filter on the column. FILTER keeps only the red row. That one-row table becomes the new filter on Color.
This expansion explains the replacement behavior. The table of allowed colors is built from all colors, not from the currently selected ones, so the old selection is gone.
Boolean arguments have limits:
- They can reference only one column, such as
'Product'[Color]. - They cannot contain a measure or a function that returns a table.
- They cannot use a column from a different table in the same argument.
For conditions that need more than that, use a table argument such as FILTER.
What the filter arguments do to the context
Each argument changes the filter context in one of these ways:
- It replaces the existing filter on the columns it names (Boolean and most table arguments).
- It removes filters (
REMOVEFILTERS,ALL,ALLEXCEPT). - It intersects with existing filters (
KEEPFILTERS). - It changes how relationships or filters propagate (
USERELATIONSHIP,CROSSFILTER).
When several filter arguments are present, they combine with a logical AND. A row of Sales must satisfy all of them to be counted.
Red Sales 2024 =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Date'[Year] = 2024
)Each argument targets a different column, so each one replaces only the filter on its own column. The result is red sales in 2024, regardless of which color or year the report has selected.
Filters propagate through relationships
A filter that CALCULATE places on 'Product'[Color] does not stay on the Product table. It follows the one-to-many relationship from Product to Sales, so Total Sales sees only red products. This is the same propagation that a slicer uses. CALCULATE creates filters, and the relationships carry them to the fact table.
If a filter is placed on a table that cannot reach the table being aggregated, it has no effect on the result. In a star schema, filters flow from the dimension (one side) to the fact (many side).
The expression is evaluated last
The first argument is evaluated after the filter arguments have been applied. This is easy to forget when the first argument contains other logic.
Red Sales Ratio =
DIVIDE (
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ),
[Total Sales]
)Here the two uses of [Total Sales] give different values. The one inside CALCULATE sees the red filter. The one in the denominator sees the original filter context. A modified context applies only to the expression inside the CALCULATE call that created it.
Nesting CALCULATE
CALCULATE calls can be nested. The inner call starts from the context that the outer call produced, then modifies it further.
Red Sales in 2024 Nested =
CALCULATE (
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ),
'Date'[Year] = 2024
)The outer call sets the year. The inner call then sets the color. The result equals Red Sales 2024 above. In practice you rarely write nesting like this on purpose. It appears most often when one measure refers to another measure that itself uses CALCULATE.
Red Sales 2024 via Measure =
CALCULATE (
[Red Sales],
'Date'[Year] = 2024
)This works because [Red Sales] expands to its own CALCULATE. The year filter from the outer call reaches the inner one, and the inner call adds the color filter. Referencing measures from other measures is a normal way to build up logic.
Reading a CALCULATE expression
Use this routine when you must predict a result:
- Identify the current filter context for the cell: row headers, column headers, slicers and page filters.
- List each filter argument and the columns it targets.
- Decide what each argument does: replace, remove, intersect, or change a relationship.
- Form the new filter context.
- Evaluate the first argument in it.
Try it on Red Sales in a matrix with 'Product'[Category] on rows and a slicer set to Blue. For the Bikes row, the starting context is Category = Bikes and Color = Blue. The Boolean argument replaces Color, giving Category = Bikes and Color = Red. The measure returns red bike sales.
Common mistakes at this stage
- Expecting a filter to add to an existing selection on the same column. A Boolean argument replaces it. Use
KEEPFILTERSwhen you want an intersection. - Putting a measure inside a Boolean argument. A Boolean argument cannot reference a measure. Compute the value in a variable first, or use a table argument.
- Forgetting that the filter is applied to a column, not a table. Filtering
'Product'[Color]affectsProductrows. It affectsSalesonly because of the relationship. - Assuming the first argument runs before the filters. It runs after them.
Summary of the working model
CALCULATEcreates a modified copy of the current filter context and evaluates its first argument there.- Boolean arguments replace the filter on the named column.
- Several filter arguments combine with AND.
- Relationships carry the new filters from dimension tables to the fact table.
- The modified context applies only inside the
CALCULATEcall.
Exercise
A matrix shows 'Product'[Category] on rows. A slicer selects Color = Blue. The measure Red Sales = CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ) is placed in the matrix. What does the Bikes row show, and why?
Show answer
It shows the total sales of red bikes. The slicer's filter on Color is replaced by the Boolean argument, because both target 'Product'[Color]. The row's filter on Category = Bikes is on a different column, so it stays. The new context is Category = Bikes and Color = Red.
Exercise
In Red Sales Ratio = DIVIDE ( CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ), [Total Sales] ), which part sees the red filter?
Show answer
Only the numerator. The red filter exists only inside the CALCULATE call that created it. The denominator [Total Sales] is evaluated in the original filter context.
Exercise
Write a measure that returns sales for Category = "Bikes" in the year 2024, ignoring any selection the report makes on Category or Year. Then say whether the order of the two filter arguments matters.
Show answer
Sales Bikes 2024 = CALCULATE ( [Total Sales], 'Product'[Category] = "Bikes", 'Date'[Year] = 2024 ). The order does not matter. Each argument replaces the filter on its own column, and the arguments combine with AND.