Sub topic 12
How DAX measures respond to filters
This chapter assumes you can build a report page with a few visuals, add a slicer, and tell a column from a measure in the data model. You do not need to write DAX from scratch. You only need to read short formulas.
Measures are recalculated for every filter selection
A measure does not store a number. Power BI evaluates it each time a visual draws, using the filters in effect for that visual. Those filters come from slicers, the Filter pane, other visuals that cross-filter, and the row or column the value sits in. Together they are called the filter context.
Take this base measure:
Total Sales = SUM ( Sales[SalesAmount] )Place it in a card and it shows the total for the current selection. Place it in a table by category and each row shows its own total. Select "Bikes" in a slicer and the card shows only bike sales. The formula never changes. Only the filter context does.
Reporting the current selection
Sometimes you want to show the user what is selected, for example in a card or a dynamic title. SELECTEDVALUE returns the single value that is selected, or a fallback you choose.
Selected Category =
SELECTEDVALUE ( Product[Category], "All categories" )The fallback appears when no category is selected and also when several are selected. If you need to tell those cases apart, you need a different approach, such as counting the selected values.
Percent of the visible selection
A percentage needs a numerator and a denominator that respond to filters differently. The numerator follows the filter context of each row. The denominator must keep the slicer selections but ignore the row's own category.
Sales % of Selected Categories =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLSELECTED ( Product[Category] ) )
)CALCULATE evaluates a measure under a modified filter context. ALLSELECTED removes the filters that come from the visual itself, such as the row, and keeps the ones from slicers and filters outside it. With three categories selected, the three rows add up to 100%. DIVIDE returns blank instead of an error if the denominator is zero.
Ignoring filters on purpose
Sometimes a benchmark should stay fixed while everything else is filtered. REMOVEFILTERS clears filters on a table or column inside CALCULATE.
Total Sales All Products =
CALCULATE ( [Total Sales], REMOVEFILTERS ( Product ) )This ignores every filter on the Product table, including slicer selections. Place it beside [Total Sales] and you can compare a filtered value against the whole.
The next table compares the three behaviors.
| Measure pattern | Slicer selections | Row or column of the visual |
|---|---|---|
| `[Total Sales]` | Applied | Applied |
| `ALLSELECTED` inside `CALCULATE` | Kept | Removed |
| `REMOVEFILTERS` inside `CALCULATE` | Removed | Removed |
Common errors
- Expecting a measure to ignore a slicer. A plain measure always respects slicers. Use
REMOVEFILTERSonly when you want to override that. - Filtering a column from an unrelated table. If a slicer's table has no relationship to the table the measure sums, the slicer has no effect. Check the model relationships.
- Using
ALLSELECTEDwith no slicer. With nothing selected, it behaves like "all values", so the percentages are of the full total. - Reading
SELECTEDVALUEas "nothing selected". The fallback also appears when several values are selected.
Exercise
A table shows Total Sales for each of three categories. A slicer selects two of them. You want a column showing each visible row's share of the two selected categories, and another column showing a constant benchmark of all sales. Which functions would you use for each?
Show answer
Use CALCULATE with ALLSELECTED on Product[Category] as the denominator, divided into [Total Sales] with DIVIDE, so the two visible rows add up to 100%. Use CALCULATE with REMOVEFILTERS on the Product table for the benchmark, since it must ignore the slicer.
Exercise
A card uses SELECTEDVALUE ( Product[Category], "All categories" ) and shows "All categories" even though the user has picked two categories. Why?
Show answer
SELECTEDVALUE returns the fallback whenever the selection is not exactly one value. This covers both no selection and several selections.