Sub topic 2
Filter Context: Slicers, Visuals, and Filter Arguments
This chapter assumes you know what a one-to-many relationship is, can write simple aggregations such as SUM and COUNTROWS, and can tell a measure from a calculated column. It also assumes you can build a basic report visual in Power BI or a similar tool.
What filter context is
Filter context is the set of filters that is active when DAX evaluates an expression. It restricts which rows of each table the expression can see. A measure such as SUM ( Sales[Amount] ) has no filter written in it. It still returns different numbers in different cells of a report because each cell carries a different filter context.
Filter context does not point at a single row. It describes a subset of a table, which can hold many rows, one row, or none.
Where filters come from
Several sources add filters to the context at the same time. DAX combines them with AND, so a row must satisfy all of them to remain visible.
| Source | Example | Effect on the evaluation |
|---|---|---|
| Report visual rows and columns | `Product[Category]` on matrix rows | Each row shows one category |
| Slicer | `Calendar[Year]` set to 2025 | Only 2025 rows remain |
| Report, page, or visual filter | `Customer[Country]` is Italy | Only Italian customers remain |
| Filter argument in `CALCULATE` | `Product[Color] = "Red"` | Adds or replaces a filter on that column |
The first three sources are set by the person using the report. The last one is set by the author of the formula, and the next chapters cover it in detail. Here, only its basic behavior matters.
A worked example
Take this measure, which iterates over Sales and adds the product of quantity and price.
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )Suppose a matrix shows Product[Category] on its rows and a slicer limits Calendar[Year] to 2025. In the cell for the category Bikes, the filter context holds two filters: Category = Bikes and Year = 2025. SUMX iterates only the Sales rows that satisfy both, so the cell shows 2025 sales of bikes. The grand total row has no category filter, so it sums every category for 2025.
You wrote the measure once. The filter context changed the rows it read in each cell.
Filter arguments in CALCULATE
CALCULATE evaluates an expression in a modified filter context. Each filter argument is a condition on a column.
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )This filter argument does two things:
- It adds a filter on
Product[Color]equal to Red. - It removes any existing filter on
Product[Color]before adding the new one.
So if a slicer has selected Blue, Red Sales still returns red sales. The slicer's filter on that column is replaced, not combined. Filters on other columns, such as the year or the category, stay in place and combine with the new one.
Sometimes you want the new filter to respect the existing selection. KEEPFILTERS changes the behavior from replace to intersect.
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)With Blue selected in a slicer, this measure returns blank, because no row is both Blue and Red. With Red and Blue selected, it returns red sales. A later chapter covers KEEPFILTERS and the other filter modifiers in full.
Common mistakes
- Expecting a measure to ignore the slicer. A measure always runs in the filter context of its cell. To ignore a filter, you must remove it explicitly.
- Assuming a filter argument adds to an existing filter on the same column. By default it replaces it.
- Forgetting that filter context is a set of filters, not a row. You cannot read a column value from it as you would in a calculated column. Reading
Product[Color]directly in a measure fails unless a function such asSELECTEDVALUEreduces the filtered column to one value.
Exercise
A matrix shows Product[Category] on its rows. A slicer selects Color = Blue. A measure is defined as CALCULATE ( [Sales Amount], Product[Color] = "Red" ). What does the Bikes row show, and why?
Show answer
It shows the sales of red bikes. The CALCULATE filter argument replaces the slicer's existing filter on Product[Color], so Blue is discarded and Red is used. The filter on Category = Bikes comes from a different column, so it stays in place.
Exercise
Rewrite the measure from the previous exercise so that, with Blue selected in the slicer, it returns blank instead of red sales. Which function do you add?
Show answer
Wrap the filter in KEEPFILTERS: CALCULATE ( [Sales Amount], KEEPFILTERS ( Product[Color] = "Red" ) ). It intersects the Red filter with the slicer's Blue filter, which leaves no rows, so the result is blank.