Sub topic 12
How DAX measures respond to filters
This chapter assumes you can build a report page with slicers and visuals, and that you know the difference between a column and a measure in a data model. You do not need advanced DAX. You do need to know that a measure is a formula that Power BI evaluates when a visual is drawn, not when data is loaded.
Why measures change with every visual
A slicer or a Filter pane entry does not edit your data. It builds a set of filters that Power BI applies to the model before it calculates each measure. This set is called the filter context. A measure is recalculated once per cell, bar, or card, and each one has its own filter context.
Consider this base measure:
Total Sales = SUM ( Sales[SalesAmount] )The formula never mentions a slicer. Even so, the same measure shows different numbers in different places:
- On a card, it sums all rows that survive the slicers, page filters, and report filters.
- In a bar chart with Product[Category] on the axis, each bar adds a filter for its own category on top of those.
- In a matrix with Category on rows and Year on columns, each cell has both filters.
Slicers, all three filter scopes (visual, page, report), and the row, column, or axis fields of a visual each add to the filter context. Filters on different columns combine with AND. Several values selected in one column combine with OR.
Relationships carry the filter. A slicer on Product[Category] filters the Product table. The relationship from Product to Sales passes that filter down to Sales, so SUM ( Sales[SalesAmount] ) only sees the matching rows. If the relationship is missing or points the wrong way, the measure ignores the slicer. The troubleshooting chapter returns to this.
Reading the current selection
Sometimes you want a title or card that describes what the reader selected. SELECTEDVALUE returns the value of a column when exactly one value is in the filter context. Otherwise it returns the alternate result you supply.
Selected Category =
SELECTEDVALUE ( Product[Category], "All categories" )The fallback text appears in two cases: when no category is selected, and when several are selected. A reader who picks Bikes and Clothing sees "All categories", which is misleading. If that matters, count the selection first:
Selection Label =
VAR CategoriesSelected = DISTINCTCOUNT ( Product[Category] )
VAR AllCategories =
CALCULATE ( DISTINCTCOUNT ( Product[Category] ), REMOVEFILTERS ( Product ) )
RETURN
SWITCH (
TRUE (),
CategoriesSelected = AllCategories, "All categories",
CategoriesSelected = 1, SELECTEDVALUE ( Product[Category] ),
"Multiple categories"
)DISTINCTCOUNT is evaluated in the current filter context, so it counts only the categories that survive. The CALCULATE with REMOVEFILTERS counts every category in the table.
Use a measure like this in a card, or in a visual title through conditional formatting of the title text (the fx button next to the title field).
Changing the filter context with CALCULATE
CALCULATE evaluates an expression in a modified filter context. Its filter arguments adjust the context that the slicers and visuals created. Three functions come up most often.
| Function | What it does to the filters | Typical use |
|---|---|---|
| `REMOVEFILTERS` | Removes all filters from a table or column | A fixed benchmark or grand total |
| `ALL` | Same effect inside `CALCULATE`, and also returns a table when used elsewhere | Older code, or when you need the table of values |
| `ALLSELECTED` | Removes the filters a visual adds, but keeps filters from slicers and the Filter pane | Percent of the visible total |
Ignoring a filter
This measure shows a number that does not respond to slicers on the Product table:
Total Sales All Products =
CALCULATE ( [Total Sales], REMOVEFILTERS ( Product ) )Place it next to [Total Sales] in a visual. When a reader selects Bikes, [Total Sales] drops to the Bikes figure while [Total Sales All Products] stays put. This is a common way to show a fixed benchmark.
REMOVEFILTERS ( Product ) removes filters on every column of the Product table. To ignore only one column, name it: REMOVEFILTERS ( Product[Category] ). A slicer on Product[Color] would then still apply.
Percent of the visible selection
Percent-of-total is the usual place where the wrong function gives a plausible but wrong answer.
Sales % of Selected Categories =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLSELECTED ( Product[Category] ) )
)Suppose a bar chart shows one bar per category and a slicer limits the report to Bikes, Clothing, and Accessories.
- The numerator is the sales for one bar's category.
ALLSELECTED ( Product[Category] )removes the filter that the bar itself adds, but keeps the slicer's three categories. The denominator is the sum for those three.- The three bars add up to 100%.
If you used ALL ( Product[Category] ) or REMOVEFILTERS ( Product[Category] ) instead, the slicer would be ignored too. The denominator would be the sales of every category, and the three bars would add up to less than 100%. Choose the function based on a question: should the denominator respect what the reader selected? If yes, use ALLSELECTED. If it should be fixed, use REMOVEFILTERS.
DIVIDE returns blank on division by zero by default, so empty selections do not produce errors.
Filters that measures cannot see
Not every setting changes the filter context in the way you expect. Keep these points in mind.
- Edit interactions. If a slicer's interaction with a visual is set to None, the visual receives no filter from it. The measure then behaves as if the slicer did not exist.
ALLSELECTEDalso ignores that slicer for the same reason. - Filter pane filters count as slicer-like.
ALLSELECTEDkeeps filters from the Filter pane and from slicers. A report-level filter excluding a category will therefore also shrink the denominator. - Visual-level Top N. A Top N filter on a visual limits the rows that appear.
ALLSELECTEDinside that visual treats the filtered set as the "selected" set, so a percentage can show 100% across the top five items only. - Measure filters. A filter on a measure in the Filter pane (for example,
Total Salesgreater than 1000) is applied after the measure is calculated for each item. It hides items but does not change the measure's own filter context. - Cross-highlighting. Selecting a bar in another visual highlights rather than filters when the interaction is set to highlight. Highlighting does not change the denominator in a percentage in the same way a filter does, which is a frequent reason two visuals disagree.
Building a measure that explains itself
When you test measures, put a few helper measures in a table visual next to the slicer. Seeing each piece makes the filter context concrete:
Categories In Context =
DISTINCTCOUNT ( Product[Category] )
Rows In Context =
COUNTROWS ( Sales )Change a slicer and watch the numbers move. Then add a visual field and see how each row differs. This is the fastest way to learn what ALLSELECTED and REMOVEFILTERS do in your own model.
Common mistakes
- Using
SELECTEDVALUEfor a multi-select slicer and expecting a list. It returns the fallback. UseCONCATENATEX ( VALUES ( Product[Category] ), Product[Category], ", " )when you want the names. - Using
ALLwhereALLSELECTEDwas intended, which makes percentages ignore the slicer. - Removing filters from the wrong table, such as the Date table when the slicer is on a Sales column. Name the table or column the slicer actually filters.
- Putting a slicer field from a table with no relationship to the fact table. The slicer changes nothing, and so does every measure.
Exercise
A page has a slicer on Product[Category] with Bikes and Clothing selected. A bar chart has Category on the axis and the measure DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], REMOVEFILTERS ( Product[Category] ) ) ). The two bars add up to 61%, not 100%. Explain why, and rewrite the measure so the bars add up to 100%.
Show answer
REMOVEFILTERS removes the slicer's filter as well as the bar's own filter, so the denominator is total sales for all categories, including the ones the reader did not select. The two selected categories make up only 61% of that. Replace REMOVEFILTERS with ALLSELECTED: DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], ALLSELECTED ( Product[Category] ) ) ). The denominator is then the sales of Bikes and Clothing only.
Exercise
A card uses SELECTEDVALUE ( Product[Category], "All categories" ). A reader selects Bikes and Clothing, and the card says "All categories". Why, and how would you show the names of the selected categories instead?
Show answer
SELECTEDVALUE returns its fallback unless exactly one value is in the filter context, and here there are two. Use CONCATENATEX ( VALUES ( Product[Category] ), Product[Category], ", " ) to join the selected names into one text value.