Sub topic 4
CALCULATE: evaluating an expression in a modified context
This chapter assumes you can write basic DAX measures, that your model is a star schema with one-to-many relationships, and that you know how slicers, rows, and columns in a report filter a measure. Here you learn what CALCULATE does and how it is built. Later chapters cover each kind of filter argument in detail.
What CALCULATE does
A measure never has a fixed value. It returns a result for the filter context it is evaluated in. Take this base measure:
Total Sales = SUM ( Sales[Amount] )In a matrix with Color on the rows, this measure returns one number per color. The formula is the same in every cell. Only the filter context changes.
CALCULATE lets you change that filter context on purpose. It copies the current filter context, applies your changes to the copy, and evaluates an expression in the modified copy. The report's own filters are not touched. Only that one expression sees the change.
Syntax
CALCULATE ( <expression>, <filter1>, <filter2>, ... )- The first argument is the expression to evaluate. It is usually a measure or an aggregation such as
SUM. - Every later argument is a filter argument. They are optional, and you can pass as many as you need.
A filter argument can take several forms:
| Form | Example | Effect |
|---|---|---|
| Boolean expression | `'Product'[Color] = "Red"` | Replaces the filter on that column |
| Table expression | `FILTER ( Sales, Sales[Quantity] > 2 )` | Filters by the rows the table returns |
| Filter modifier | `REMOVEFILTERS ( 'Product'[Category] )` | Removes filters |
| Relationship modifier | `USERELATIONSHIP ( ... )` | Changes which relationship is active |
A first example
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)The expression is [Total Sales]. The filter argument sets 'Product'[Color] to "Red". Suppose a slicer selects Blue. Inside this CALCULATE the Blue selection on Color is replaced by Red, so the measure returns red sales. Filters on other columns, such as Year or Country, stay as they were.
Evaluation of Red Sales for a cell where the slicer selects Blue goes like this:
Year stays at 2025. Only Color is replaced.
Why not just use a different formula?
You could try SUMIFS-style logic, but DAX has no such function over the model. CALCULATE gives you the same result and also follows relationships. A filter on 'Product'[Color] reaches Sales through the relationship, so you can filter a fact table by an attribute of a dimension table without writing a join.
The same measure also works at every level of a report. In a grand total, Color has no filter, and Red Sales still returns red sales. On a row for Blue, it returns red sales too, because the Blue filter is replaced.
Rules to remember
- CALCULATE changes the filter context only for its first argument. Measures outside it are not affected.
- A Boolean filter on a column replaces any existing filter on that same column. It does not replace filters on other columns.
- Multiple filter arguments combine with AND logic. For example, a Color filter and a Size filter both apply.
- CALCULATE also converts any row context that is active when it runs into an equivalent filter context. That behavior is called context transition and has its own chapter.
Common errors at this stage
- Expecting the filter to add to the selection. Writing
'Product'[Color] = "Red"when a slicer selects Blue does not return blank. It returns red sales. If you want the intersection, you needKEEPFILTERS, covered later. - Passing a bare column as the first argument. The first argument must be a scalar expression, so
CALCULATE ( Sales[Amount], ... )fails. Wrap the column in an aggregation:CALCULATE ( SUM ( Sales[Amount] ), ... ). - Using CALCULATE with no filters and expecting nothing to happen. With no filter arguments, CALCULATE still performs context transition if a row context exists.
Exercise
A slicer selects Year = 2025 and Color = Blue. What does CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ) return?
Show answer
Red sales for 2025. The Boolean filter replaces the Blue selection on Color, and the Year filter is untouched because the argument does not mention Year.
Exercise
Why does CALCULATE ( Sales[Amount], 'Product'[Color] = "Red" ) produce an error, and how do you fix it?
Show answer
The first argument must be a scalar expression, and a bare column reference has no aggregation. Fix it by aggregating, for example CALCULATE ( SUM ( Sales[Amount] ), 'Product'[Color] = "Red" ).