Sub topic 1
How filter context shapes a measure's result
Every DAX measure returns a number that depends on where it is evaluated. The same formula gives different results in a card, in a matrix row, or under a slicer selection. The mechanism behind this is filter context. This chapter explains what filter context is, where it comes from, how relationships carry it between tables, and how to inspect it. The later chapters on CALCULATE, FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS all change filter context, so you need a precise model of it first.
Prerequisites
This chapter assumes you already have the following:
- Basic DAX measures such as SUM and COUNTROWS.
- Tables, columns, and relationships in a Power BI or Analysis Services data model.
- Familiarity with report visuals, slicers, and how they filter data.
The examples use a small star schema. Sales is a fact table with the columns Amount, Quantity, Net Price, and a key column for each dimension. 'Product' is a dimension table with the columns Color and Category. There is a one-to-many relationship from 'Product' to Sales, with a single filter direction from 'Product' to Sales.
What filter context is
A filter context is the set of filters that is active at the moment DAX evaluates an expression. You can think of it as a list of conditions such as 'Product'[Color] = "Red" or 'Product'[Category] IN {"Bikes", "Accessories"}. DAX applies these conditions to the model, and only the rows that survive are visible to the aggregation.
Consider this measure:
Total Sales = SUM ( Sales[Amount] )The formula never mentions a color, a category, or a year. It still returns different values in different places, because it is evaluated once per cell of a visual, and each cell has its own filter context. The measure itself stays the same. Only the context changes.
Where filters come from
Filter context is assembled from several sources. Knowing them lets you explain any number in a report.
| Source | Example | Effect on the filter context |
|---|---|---|
| Rows of a matrix or table | `'Product'[Category]` on rows | One filter per row, for example Category = Bikes |
| Columns of a matrix | `'Date'[Year]` on columns | One filter per column value |
| Axis or legend of a chart | Color on the legend | One filter per axis or legend value |
| Slicers | A slicer on `'Product'[Color]` | Filter on the selected values |
| Visual-level, page-level, and report-level filters | Filter pane settings | Filters on the chosen columns |
| Cross-highlighting and cross-filtering | Clicking a bar in another visual | Filters on the clicked values |
| CALCULATE and CALCULATETABLE | Filter arguments | Adds, replaces, or removes filters |
The first six sources are applied by the report. The last one is applied by your DAX code, and it is the subject of most of this tutorial.
Filters from different sources combine. If a slicer selects Color = Red and a matrix row shows Category = Bikes, the cell evaluates with both filters. Only rows that are red and in the Bikes category remain. Filters on different columns combine with AND. Several values selected on the same column, such as Red and Blue, combine with OR within that column.
A worked example
Suppose the model holds these four sales rows.
| Sales row | Product color | Product category | Amount |
|---|---|---|---|
| 1 | Red | Bikes | 500 |
| 2 | Blue | Bikes | 700 |
| 3 | Red | Accessories | 100 |
| 4 | Black | Accessories | 200 |
Now place [Total Sales] in a matrix with Category on the rows and no slicer selected.
| Category | Total Sales | Filter context for that cell |
|---|---|---|
| Bikes | 1200 | `'Product'[Category] = "Bikes"` |
| Accessories | 300 | `'Product'[Category] = "Accessories"` |
| Total | 1500 | No filter |
Each row evaluates the same SUM, but over a different set of rows. The Total row has no filter on Category, so it sees all four sales rows. It is not the sum of the displayed rows. It is a separate evaluation with an empty filter context. This detail matters later, because percent-of-total measures divide a filtered value by a value evaluated with a different context.
Now add a slicer with Color = Red. Every cell gains a second filter.
| Category | Total Sales | Filter context for that cell |
|---|---|---|
| Bikes | 500 | Category = Bikes, Color = Red |
| Accessories | 100 | Category = Accessories, Color = Red |
| Total | 600 | Color = Red |
The measure formula did not change. The slicer added a filter, so the result changed.
How a filter reaches the fact table
A filter on 'Product'[Color] is applied to the 'Product' table first. DAX then follows the relationship to Sales. Because the relationship is one-to-many from 'Product' to Sales, the filtered product rows filter the matching sales rows. SUM ( Sales[Amount] ) then sees only those sales rows.
The direction matters. Filters travel from the one side to the many side by default. A filter on Sales does not filter 'Product' unless the relationship uses bidirectional filtering. For example, a filter on a column in Sales does not reduce the list of colors visible in 'Product' in a default star schema. When a measure ignores a filter you expected it to respect, check the relationship direction first.
Filter context applies to columns, not whole tables
A filter is stored per column. When a slicer selects Red, the filter context records that 'Product'[Color] may only be Red. The other columns of 'Product' are not filtered directly. They are filtered indirectly, because the surviving rows are the only ones left, and any column then shows only the values that still exist.
This distinction becomes important when you remove filters. Clearing the filter on 'Product'[Color] is different from clearing every filter on the 'Product' table. The functions ALL, ALLEXCEPT, and REMOVEFILTERS let you choose either level, and later chapters cover the details.
Inspecting the current filter context
You can test what a measure sees by writing small diagnostic measures. These use only functions covered in the prerequisites, plus a few that report on filters.
Product Rows Visible = COUNTROWS ( 'Product' )Color Is Filtered = ISFILTERED ( 'Product'[Color] )Selected Colors =
CONCATENATEX (
VALUES ( 'Product'[Color] ),
'Product'[Color],
", ",
'Product'[Color], ASC
)Place these in a matrix next to [Total Sales]:
Product Rows Visiblecounts the product rows that survive the current filters. It shrinks as slicers narrow the selection.Color Is Filteredreturns TRUE when a filter is applied directly on the Color column, and FALSE when none is.Selected Colorslists the colors that remain visible. VALUES returns the distinct values that survive in the current filter context.
Use these when a number surprises you. They show what the engine sees instead of what you assume it sees.
Filter context and the grand total
The Total row of a table or matrix does not add up the rows above it. It evaluates the measure once, with fewer filters. For SUM this usually gives the same answer as adding the rows, so the difference goes unnoticed. For other measures it does not.
Product Count = COUNTROWS ( 'Product' )If a product could appear under two categories, the rows would add up to more than the total. For a distinct count, the same effect appears:
Customers = DISTINCTCOUNT ( Sales[CustomerKey] )A customer who bought in two categories is counted once in each category row. The Total row counts that customer once. The rows add up to more than the total, and that is correct. The Total row is a separate evaluation.
Common errors at this stage
- Believing a measure stores a value. A measure is a formula that runs again for every cell, with that cell's filter context.
- Expecting the Total row to be the sum of the visible rows. It is a fresh evaluation with fewer filters.
- Forgetting that filters flow along relationships in one direction. A filter on the many side does not reach the one side in the default setup.
- Treating filter context as a property of a table. It is applied per column, then spread through relationships.
- Reading a result without checking for filters from the filter pane, cross-highlighting, or hidden page-level filters.
What comes next
Filter context is the input to every measure. The next chapter looks at row context, which is a different mechanism that moves through the rows of a table inside an iterator. After that, CALCULATE shows how to change the filter context from inside a measure, and FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS give you the tools to shape it.
Exercise
A matrix shows Category on the rows, and a slicer selects Color = Red. Using the four-row sales example from this chapter (Red Bikes 500, Blue Bikes 700, Red Accessories 100, Black Accessories 200), what does [Total Sales] return in the Bikes row, and what filter context does that cell have?
Show answer
It returns 500. The cell has two filters: 'Product'[Category] = "Bikes" from the matrix row and 'Product'[Color] = "Red" from the slicer. Filters on different columns combine with AND, so only the red bike sale of 500 remains. The relationship from 'Product' to Sales passes both filters on to the sales rows.
Exercise
A matrix has Category on the rows. The measure Customers = DISTINCTCOUNT ( Sales[CustomerKey] ) shows 40 in the Bikes row and 25 in the Accessories row, but the Total row shows 52. Explain why the Total row is not 65.
Show answer
The Total row is not the sum of the rows above it. It is a separate evaluation with no filter on Category. A customer who bought both Bikes and Accessories is counted once in each category row but only once in the Total row. Here 13 customers bought in both categories, so 40 + 25 - 13 = 52.