Sub topic 2
Filter context: the rows a measure sees
This chapter assumes you can write simple measures with SUM and CALCULATE. It also assumes you know a star schema, where a fact table such as Sales relates to dimension tables such as Product and Customer.
What filter context is
Filter context is the set of filters that are active when DAX evaluates a measure. It decides which rows of each table the measure can see. A measure does not store a number. DAX recalculates it for every cell of a visual, and each cell has its own filter context.
Filters come from these places:
- Fields placed on the rows and columns of a visual.
- Slicers.
- Report, page, and visual filters.
- Filter arguments of CALCULATE.
Consider this measure:
Sales Quantity =
SUM ( Sales[Quantity] )Put Product[Category] on the rows of a matrix. The cell for the Accessories row is evaluated with the filter Product[Category] = "Accessories". The filter moves through the relationship from Product to Sales, so SUM adds up only the Sales rows for accessories.
| Matrix cell | Filter context | Rows of Sales the measure sees |
|---|---|---|
| Accessories row | Category = Accessories | Sales of accessories |
| Bikes row | Category = Bikes | Sales of bikes |
| Total row | No filter on Category | All Sales rows |
| Accessories row, Year slicer set to 2025 | Category = Accessories and Year = 2025 | Accessories sales in 2025 |
The total row is not a sum of the rows above it. It is the same measure evaluated with no filter on Category.
Filter context and row context
Row context and filter context are different mechanisms, and mixing them up is a common source of errors.
| Row | Filter context | Row context |
|---|---|---|
| What it does | Restricts which rows are visible | Points at one current row |
| Created by | Visual, slicers, CALCULATE | Calculated columns, iterators |
| Affects other tables | Yes, through relationships | No, unless you use RELATED or a context transition |
A row context does not filter anything. Inside an iterator, a column reference such as Sales[Quantity] reads the value of the current row. It does not restrict the other tables.
Why this matters for iterators
An iterator's table argument is evaluated in the current filter context. That is how the iterator knows which rows to visit.
Customers in Context =
COUNTROWS ( VALUES ( Customer[CustomerKey] ) )In a cell filtered to one country, VALUES returns only the customers from that country that have sales. In the grand total it returns every customer with sales. The same applies to SUMX ( Sales, ... ). The iterator visits only the Sales rows that survive the filters.
Each iterator in this tutorial begins this way. First the filter context decides which rows are visible. Then the iterator creates a row context for each of those rows.
Common mistakes
- Expecting a measure to see the whole table. It sees only the rows left by the filters in its cell.
- Expecting the total row to add up the rows above it. For non-additive measures such as averages and ranks, it will not.
- Expecting a filter on a fact table to filter a dimension. By default, filters move from the one side to the many side, so a filter on Sales does not reduce Product.
- Treating a row context as a filter. Without a context transition, a measure called inside an iterator ignores the current row. Later chapters cover this.
Exercise
A matrix shows Product[Category] on rows and the measure Sales Quantity = SUM ( Sales[Quantity] ). A slicer limits the report to Year = 2025. What filter context does the Bikes row have, and what does the total row have?
Show answer
The Bikes row has two filters: Category = Bikes and Year = 2025. The total row has only Year = 2025, because nothing filters Category there. The slicer applies to every cell.