Sub topic 3
Filter context: evaluating over a filtered set of rows
This chapter assumes you can navigate Power BI Desktop, you know what a star-schema model is, and you can read basic DAX such as SUM and IF. It also assumes you have met row context, which evaluates a formula for one row at a time. Here the focus is the other kind of context, which measures depend on.
What filter context is
Filter context is the set of filters that decide which rows of the model are visible to a formula at the moment it is calculated. A measure does not look at one row. It looks at whatever rows survive the filters, then aggregates them.
The filters come from several places:
- Rows and columns of a table or matrix visual.
- The axis and legend of a chart.
- Slicers on the page.
- Filters in the Filters pane, at visual, page or report level.
- Cross-highlighting, when you click a bar in one visual and other visuals respond.
- Filter arguments inside
CALCULATE, which you will see later in this chapter.
You never write the filter context yourself in a simple report. The report layout and the user's selections build it. The measure is written once, and Power BI evaluates it again for every cell, bar or card.
A measure evaluated under different filters
Take the measure from earlier in this tutorial:
Total Sales = SUM(Sales[Line Amount])The formula never changes, but the result does. In a matrix with Product[Category] on the rows, the cell for "Bikes" is evaluated with a filter that keeps only Bikes products. The cell for "Clothing" is evaluated with a different filter. The grand total row is evaluated with no filter from the rows at all, so it sees every sale.
The same measure in a card visual, with a slicer set to the year 2025, sees only 2025 sales. Each of these is a separate evaluation of one formula under a different filter context.
How filters travel through relationships
Filter context is not limited to the table where the filter is applied. In a star schema, a filter on a dimension table flows to the fact table through the relationship.
Suppose Product has a one-to-many relationship with Sales, with Product on the one side. When a visual puts Product[Category] = "Bikes" into the filter context, the Product table is reduced to Bikes products. That filter then propagates along the relationship, and Sales is reduced to rows whose product is one of those Bikes products. SUM(Sales[Line Amount]) then adds only those rows.
The default direction matters. Filters flow from the one side to the many side. A filter on Sales does not, by default, reduce Product. This is why dimension tables work well as slicers and axes: they drive the fact table.
| Filter applied on | Reaches | Default behavior |
|---|---|---|
| `Product[Category]` (one side) | `Sales` (many side) | Yes, rows are filtered |
| `Sales[Quantity]` (many side) | `Product` (one side) | No, not by default |
| `Date[Year]` (one side) | `Sales` (many side) | Yes, rows are filtered |
A common error is putting a column from an unrelated table on a visual axis and then wondering why a measure returns the same number on every row. If no relationship connects the tables, no filter can travel, and the measure sees all rows each time.
Why calculated columns do not respond to filter context
A calculated column is computed during data refresh, before any visual exists. At that point there are no slicers and no rows on an axis. The formula runs with row context only, once per row, and the result is stored.
Consider this column:
Line Amount = Sales[Quantity] * Sales[Unit Price]It will hold the same value for a given row whether the user selects 2024 or 2025 in a slicer. The slicer changes which stored values a measure aggregates, but it cannot change a value that was already stored.
This is the sharpest practical difference between the two objects:
- A calculated column uses row context, and its value is fixed at refresh time.
- A measure uses filter context, and its value is recalculated every time the filters change.
A measure can read stored column values, as Total Sales reads Sales[Line Amount]. The column supplies per-row values. The measure supplies the filter-sensitive aggregation.
Row context does not filter
A frequent misunderstanding is that row context also filters the table. It does not. Row context points to a current row so you can read its column values. It does not remove any other rows from consideration.
This explains a common mistake. Suppose you write this calculated column on Sales:
Wrong Total = SUM(Sales[Line Amount])You might expect the sum for each row, or for each product. Instead, every row shows the same number: the sum of the whole column. SUM is an aggregation, so it looks at the filter context. During a calculated column refresh there is no filter context, so it sees every row. The row context identifies the current row but does not narrow what SUM aggregates.
Section "Context transition" in a later chapter shows how CALCULATE turns a row context into a filter context. For now, remember the rule: aggregation functions respond to filter context, and row context alone does not create one.
Changing the filter context with CALCULATE
CALCULATE evaluates an expression in a filter context that you modify. Its first argument is the expression, and the remaining arguments are filters.
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")Consider a matrix with Product[Category] on the rows. For the Bikes row, the existing filter context contains Category = Bikes. CALCULATE adds Color = "Red". The expression [Total Sales] is then evaluated with both filters, so it returns red bikes only. In the grand total row, it returns all red products.
Two behaviors are worth knowing:
- A filter argument on a column that is already filtered replaces the existing filter on that column. If a slicer selects
Color = Blueand the measure saysColor = "Red", the measure returns red sales, not an empty result. The slicer's filter onColoris overwritten. - A filter argument on a different column is added to the existing filters. The result is the intersection of all of them.
Note that [Total Sales] is a reference to a measure. Because the measure is already a formula, it is evaluated again inside the new context. This is one reason explicit measures are worth creating: they can be reused as building blocks.
Filter context in a nested measure
Measures can call other measures, and each call inherits the current filter context unless CALCULATE changes it.
Total Sales = SUM(Sales[Line Amount])
Sales Share of Category =
DIVIDE(
[Total Sales],
CALCULATE([Total Sales], ALL(Product[Product Name]))
)Here the first [Total Sales] uses the filter context as it is. The second removes the filter on Product[Product Name] with ALL, so it returns the total for the whole category. In a matrix with category and product on the rows, the result is each product's share of its category. ALL is one of several functions that modify filters, and later material on performance and decisions will not depend on them beyond this idea: you can widen, narrow or replace the filter context.
Reading filter context in practice
When a measure returns a surprising number, ask these questions in order:
- Which fields are on the visual's rows, columns, axis and legend?
- Which slicers and Filters pane settings apply to this visual?
- Does a relationship connect each filtering table to the table being aggregated, and does it point in the right direction?
- Does the measure contain
CALCULATEor another function that changes the filters? - Is the grand total behaving differently because it has fewer filters than the rows above it?
The last question catches many errors. A total row is not the sum of the rows above it. It is a separate evaluation with fewer filters. For additive measures such as Total Sales, the two agree. For ratios and distinct counts they often do not, which is the subject of a later chapter.
Common errors
- Expecting a calculated column to react to a slicer. The column is stored. Use a measure if the value must change with the user's selection.
- Using
SUMin a calculated column to mean "this row's total". It aggregates the whole column because no filter context exists at refresh time. - Relying on a filter that cannot travel. Without a relationship, or with the filter on the many side only, the filter never reaches the table you aggregate.
- Assuming
CALCULATEalways narrows. A filter on an already filtered column replaces the old filter, which can widen results. - Reading the grand total as a sum of the rows. It is evaluated separately.
Exercise
A matrix has Product[Category] on the rows. The measure Total Sales = SUM(Sales[Line Amount]) shows 500 for Bikes and 300 for Clothing. A calculated column on Sales named Category Total = SUM(Sales[Line Amount]) is added. What value does it show on a Bikes row, and why?
Show answer
It shows the sum of Line Amount across the entire Sales table (for example 800 if those are the only categories), not 500. A calculated column is computed at refresh time, when no visual or slicer filter context exists. SUM aggregates every row, and the row context only identifies the current row without filtering the table. The matrix does not change the stored value.
Exercise
A slicer selects Product[Color] = "Blue". The measure Red Sales = CALCULATE([Total Sales], Product[Color] = "Red") is placed on a card. What does the card show, and why?
Show answer
It shows the total sales for red products, not blank and not blue. The filter argument in CALCULATE is on the same column (Color) as the slicer, so it replaces the slicer's filter on that column. The measure evaluates with Color = "Red".
Exercise
Total Sales is correct for each category in a matrix, but a measure that shows the same number on every row appears when you put Customer[Region] on the rows. Both measures aggregate Sales. What is the most likely cause?
Show answer
There is no active relationship between Customer and Sales, or the relationship does not allow the Customer filter to reach Sales. Without a path, the filter on Region cannot propagate, so every row evaluates over all Sales rows. Check the Model view for the relationship and its direction.