Sub topic 2
Filter context: the rows a measure sees
This chapter assumes you can build a star-schema model in Power BI or Analysis Services Tabular, with relationships between a fact table and dimension tables. It also assumes you can write simple measures with SUM and CALCULATE. Iterators come in later chapters. Here you learn the context they run inside.
What filter context is
A measure has no fixed answer. Sales Amount returns a different number in each cell of a report because each cell applies a different set of filters to the model. That set of filters is the filter context.
Filter context is a set of column filters. Each filter says which values of a column are visible. The engine applies these filters to the tables before the measure's expression runs. The expression then sees only the rows that survive.
The measure itself is never edited. Only the context changes.
The examples in this chapter use a small model. Product is a dimension table related one-to-many to the fact table Sales.
| Sales row | Product Name | Category | Quantity | Net Price | Line amount |
|---|---|---|---|---|---|
| 1 | Alpine | Bikes | 2 | 100 | 200 |
| 2 | Summit | Bikes | 1 | 300 | 300 |
| 3 | Shield | Helmets | 3 | 50 | 150 |
| 4 | Dome | Helmets | 2 | 40 | 80 |
The base measure is the weighted sum from the spine:
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)The measure refers to the whole Sales table, yet it returns 500 in the Bikes row of a matrix. The reason is that the Sales table the iterator receives has already been filtered by the context. The next sections show how that happens.
Where filter context comes from
Filters enter the context from several places:
- Rows and columns of a visual. In a matrix with
Product[Category]on rows, each row adds one filter, such asCategory = "Bikes". - Slicers. A slicer on
Product[Product Name]adds a filter to every visual on the page that it reaches. - Visual-level, page-level, and report-level filters. These work like slicers that the viewer cannot see.
- CALCULATE and CALCULATETABLE. DAX code can add, replace, or remove filters.
- Row-to-filter conversion. A row context can become a filter context. This is context transition, and it has its own chapter.
All of these combine. A cell in the Bikes row, with a slicer selecting Alpine and Shield, carries two filters: Category = "Bikes" and Product Name in {Alpine, Shield}. The filters are combined with AND. Only Alpine satisfies both, so the cell shows 200.
The total row
A total row has no filter from the row headers. It carries only the slicer and page filters. In the same report the total is 350. That is Alpine (200) plus Shield (150), because the slicer still applies.
A total is not the sum of the visible rows. The measure is evaluated again under a different context. For a plain sum the two happen to agree. For ratios, averages, and ranks they do not, and this causes many of the bugs in later chapters.
How filters travel through relationships
A filter on a column also filters other tables when a relationship connects them. In a star schema a filter on a dimension table flows to the fact table along the "one" to "many" direction.
Follow the Bikes cell with the slicer applied:
- The matrix row filters
Productto the Bikes products, Alpine and Summit. - The slicer filters
Productto Alpine and Shield. Combined with step 1, only Alpine remains. - The relationship passes that filter to
Sales. Only sales row 1 stays visible. - SUMX iterates over that one row and returns 2 × 100 = 200.
The filter does not flow the other way by default. Filtering Sales does not filter Product. For example, a filter on Sales[Quantity] leaves the product list untouched. The relationship's cross-filter direction controls this. Leave it as single-direction unless you have a specific reason to change it.
Reading the filter context from DAX
You can look at the context with measures that report what is visible. These help you debug.
Visible Sales Rows =
COUNTROWS ( Sales )Visible Products =
COUNTROWS ( VALUES ( Product[Product Name] ) )With the slicer on Alpine and Shield, Visible Sales Rows shows 1 in the Bikes row, 1 in the Helmets row, and 2 in the total. Visible Products shows the same pattern.
VALUES ( column ) returns the distinct values of a column that are visible in the current context. HASONEVALUE ( column ) returns TRUE when exactly one value is visible. You will use both for the ranking guard in a later chapter.
Changing the filter context with CALCULATE
CALCULATE evaluates an expression in a modified copy of the current filter context. Two rules matter here.
- A filter on a column replaces any existing filter on the same column.
- A filter on a column leaves filters on other columns in place.
Bikes Sales =
CALCULATE (
[Sales Amount],
Product[Category] = "Bikes"
)Take the matrix with Category on rows and the slicer on Alpine and Shield:
| Row | Sales Amount | Bikes Sales | Why Bikes Sales has this value |
|---|---|---|---|
| Bikes | 200 | 200 | Category is replaced by Bikes, which is the same filter. The slicer still leaves Alpine. |
| Helmets | 150 | 200 | The Helmets filter on `Category` is replaced by Bikes. The slicer still applies, so only Alpine shows. |
| Total | 350 | 200 | No row filter exists. Category = Bikes is added to the slicer filter. |
In the Helmets row, Bikes Sales shows Bikes data. The row's Category filter was overwritten.
To remove filters, use REMOVEFILTERS:
All Sales =
CALCULATE (
[Sales Amount],
REMOVEFILTERS ( Product )
)Share of All Sales =
DIVIDE ( [Sales Amount], [All Sales] )REMOVEFILTERS ( Product ) clears every filter on Product columns, including the slicer. All Sales returns 730 in every cell here. If you only wanted to ignore the matrix row but keep the slicer, you would remove only Product[Category] instead. The next sections on ALL and ALLSELECTED rely on this difference.
Filter context and iterators
An iterator such as SUMX takes a table argument. That argument is evaluated in the filter context that is active when the measure is called.
SUMX ( Sales, ... )iterates over the visible rows ofSales, not over every row stored in the model.SUMX ( FILTER ( Sales, Sales[Quantity] > 1 ), ... )starts from the visibleSalesrows and drops those where quantity is 1 or less. The context filters first, and FILTER filters further. FILTER cannot bring back a row the context removed.SUMX ( ALL ( Sales ), ... )ignores the filters onSalesand visits every row. Use it knowingly.
This explains why one measure can serve every cell of a report. The iterator visits whatever rows the context leaves it.
Common errors
- Assuming the total is the sum of the rows. The total row evaluates the measure under a different context. Check ratios and averages there.
- Filtering the fact table from the wrong side. A filter on
Sales[Quantity]does not reduceProduct. Put the filter on a dimension column when you want it to reach every related table. - Overwriting a filter by accident. A CALCULATE filter on
Product[Category]replaces the matrix row's category filter. This is whyBikes Salesshowed 200 in the Helmets row. - Forgetting the slicers.
REMOVEFILTERS ( Product )andALL ( Product )clear slicer filters onProduct. UseALLSELECTEDwhen the slicer should be kept, as the ranking chapters show. - Expecting a DAX filter on a column to add to the visual's filter. On the same column it replaces it. Wrap it in
KEEPFILTERSif you want an intersection.
Exercises
Exercise
A matrix has Product[Category] on rows and a slicer selecting only the product Summit. Using the sample data, what does Sales Amount show in the Bikes row, the Helmets row, and the total row?
Show answer
Bikes shows 300, because only Summit (1 × 300) survives both the row filter and the slicer. Helmets shows blank, because no Helmets product passes the slicer. The total shows 300, because it carries only the slicer filter and Summit is the only visible product.
Exercise
With the same slicer on Summit, the measure All Sales = CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) ) is added to the matrix. What does it return in the Helmets row, and why?
Show answer
It returns 730 in every row, including Helmets. REMOVEFILTERS ( Product ) clears all filters on Product columns, which includes both the Category filter from the row and the Product Name filter from the slicer. SUMX then visits all four Sales rows: 200 + 300 + 150 + 80 = 730.