Sub topic 11
DAX measures that adapt to context
This chapter covers measures that change their result based on what the reader has selected, which hierarchy level a visual is showing, or which item was passed through drill-through. It assumes you can write simple DAX measures, know what filter context is, and have a star-schema model with a date table. Earlier chapters covered hierarchies, drill-down, drill-through pages and tooltips. Here you make the measures behind those features respond to the reader's navigation.
Why measures need to adapt
A measure is evaluated once for every cell, bar or point in a visual. Each evaluation happens in a different filter context. Drilling changes that context:
- Drill-down adds a filter on the next hierarchy level. The visual now groups by Subcategory instead of Category.
- Drill-through applies the selected item as a page-level filter on the target page.
- A report page tooltip receives the filters of the hovered data point.
A plain measure such as SUM ( Sales[Amount] ) already responds to these filters. Sometimes you need more than that. You may want a page title that names the selected customer, a label that says which level is on screen, or a comparison that shows up only in a tooltip. These cases need measures that detect the context and react to it.
Reading the context: three useful functions
| Function | Question it answers | Typical use |
|---|---|---|
| `SELECTEDVALUE` | Is exactly one value of this column visible in the filter context? If so, what is it? | Dynamic titles, drill-through labels |
| `ISINSCOPE` | Is this column a grouping level in the current visual? | Level-aware measures in matrices and hierarchies |
| `ISFILTERED` | Is any filter applied directly to this column? | Detecting a slicer or drill-through filter |
HASONEVALUE and VALUES are related. SELECTEDVALUE ( col, alt ) is shorthand for checking HASONEVALUE ( col ) and then returning VALUES ( col ), or the alternate result if there is not exactly one value.
Responding to the drill-through value
When a reader drills through from a customer, the detail page is filtered to that customer. A title measure can show who it is:
Drill-through Title =
"Orders for " & SELECTEDVALUE ( Customer[Customer Name], "multiple customers" )Put this measure in a card or in a text box title (use the fx button on the title to bind it to the measure). Three outcomes are possible:
- One customer in filter context: the title reads "Orders for Contoso Ltd".
- Several customers, for example when the reader opens the page directly without drilling: the second argument is used, and the title reads "Orders for multiple customers".
- No customers (the filters exclude everything):
SELECTEDVALUEalso returns the alternate result.
The alternate result is a design decision. Without it, SELECTEDVALUE returns BLANK, and the title would read "Orders for " with nothing after it.
Edge case: a selection of two values
If the reader holds Ctrl and picks two customers before drilling through, the page is filtered to both. SELECTEDVALUE sees two values and returns the fallback. If you want to list them instead, use CONCATENATEX:
Selected Customers =
VAR Names =
CONCATENATEX (
VALUES ( Customer[Customer Name] ),
Customer[Customer Name],
", ",
Customer[Customer Name], ASC
)
RETURN
IF ( ISFILTERED ( Customer[Customer Name] ), Names, "All customers" )ISFILTERED stops the measure from listing every customer when nothing is filtered. Be careful with long lists. A title that holds hundreds of names is unreadable, so you may want to count the values and switch to "N customers selected" above some limit.
Responding to the hierarchy level
In a matrix or chart with a Category > Subcategory > Product hierarchy, one measure is evaluated at every level. ISINSCOPE tells you which level the current cell belongs to. Test the lowest level first:
Level Label =
IF (
ISINSCOPE ( Product[Product Name] ), "Product",
IF (
ISINSCOPE ( Product[Subcategory] ), "Subcategory",
IF ( ISINSCOPE ( Product[Category] ), "Category", "Total" )
)
)The order matters. When the visual shows Product rows, the Subcategory and Category columns are also in scope, because the product sits inside them. If you tested Category first, every row would be labeled "Category". Check from the most detailed level up to the total.
Using the level to change a calculation
The same pattern can change what a measure computes. This example shows a share of the parent level, so each row is a percentage of the group above it:
Share of Parent =
VAR _Sales = [Total Sales]
VAR _Parent =
SWITCH (
TRUE (),
ISINSCOPE ( Product[Product Name] ),
CALCULATE ( [Total Sales], ALLSELECTED ( Product[Product Name] ) ),
ISINSCOPE ( Product[Subcategory] ),
CALCULATE ( [Total Sales], ALLSELECTED ( Product[Subcategory] ) ),
ISINSCOPE ( Product[Category] ),
CALCULATE ( [Total Sales], ALLSELECTED ( Product[Category] ) )
)
RETURN
DIVIDE ( _Sales, _Parent )For the Product level, removing the filter on Product Name leaves the filters on Subcategory and Category in place, so the denominator is the subcategory total. The same logic applies one level up. At the grand total none of the branches match, SWITCH returns BLANK, and DIVIDE returns BLANK for that row. That is usually what you want, because the total has no parent.
ALLSELECTED respects slicers and page filters. ALL would ignore them and give a percentage of the whole model, which is not what the reader sees on screen.
Drill-down and `ISINSCOPE`
When the reader drills down in a chart, Power BI changes the visual so that it groups by the next level, and it filters to the item they chose. After a drill-down on "Bikes", the visual groups by Subcategory, and Category is filtered to Bikes. Both columns are in scope. ISINSCOPE ( Product[Subcategory] ) is true, so the label measure returns "Subcategory". Drill-up reverses this.
Note that ISINSCOPE is true for columns that group the visual. It is not true for columns that are only filtered. A slicer on Category does not put Category in scope. Use ISFILTERED for that case.
Responding to the hovered data point in a tooltip
A report page tooltip gets the filter context of the data point under the pointer. Measures on that page evaluate in that context, so they usually work without any special code. The extra step is to write measures that give useful comparisons:
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales PY], [Sales PY] )You can use these in two ways:
- Drop them into the Tooltips field well of a visual. The default tooltip then shows them under the standard values.
- Place them in visuals on a tooltip page.
Both rely on a marked date table. SAMEPERIODLASTYEAR needs a contiguous column of dates. If the hovered point is a month, it returns the same month in the prior year. If the point is a year, it returns the prior year. DIVIDE returns BLANK when there is no prior-year value, which avoids a divide-by-zero error.
Combining level and tooltip
A tooltip often has to say what it describes. Reuse the level measure and join it with the current value:
Tooltip Header =
[Level Label] & ": "
& COALESCE (
SELECTEDVALUE ( Product[Product Name] ),
SELECTEDVALUE ( Product[Subcategory] ),
SELECTEDVALUE ( Product[Category] ),
"All products"
)COALESCE returns the first argument that is not blank. At the Subcategory level, Product[Product Name] has many values, so the first call returns BLANK and the next one is tried. Hovering over a bar for "Mountain Bikes" gives "Subcategory: Mountain Bikes".
Common mistakes
- Wrong test order in
ISINSCOPE. Testing the top level first labels every row with the top level. Test the lowest level first. - No fallback in
SELECTEDVALUE. A BLANK result produces empty or truncated titles. Provide a clear alternate text. - Using
ISINSCOPEwhereISFILTEREDis needed. A slicer or drill-through filter does not bring a column into scope. If you need to detect a filter, useISFILTERED. - Using
ALLfor a parent total. It removes slicer filters, so percentages no longer match what is visible. UseALLSELECTEDor remove only specific columns. - Unmarked or incomplete date table. Time-intelligence measures such as
Sales PYreturn wrong or blank values. - Returning text from a measure used as a value. A measure that returns text cannot be summed in a chart axis. Keep text measures for titles, labels and tooltips.
Summary
SELECTEDVALUEgives you the single selected item, or a fallback you choose. It is the main tool for drill-through titles.ISINSCOPEtells you which hierarchy level a cell belongs to. Test from the lowest level upward.ISFILTEREDdetects filters that do not group the visual, such as slicers.- Tooltip pages and the Tooltips field well evaluate measures in the hovered point's context, so comparison measures like year over year fit naturally.
Exercise
A matrix shows Category > Subcategory > Product. Your measure tests ISINSCOPE ( Product[Category] ) first, then Subcategory, then Product, and returns "Category", "Subcategory" or "Product". Every row, including product rows, shows "Category". Why does this happen, and how do you fix it?
Show answer
Category is in scope on every row, because each product row sits inside its category. The first test is always true, so the other branches never run. Reorder the tests to check Product first, then Subcategory, then Category, and return "Total" as the final fallback.
Exercise
A drill-through page title uses "Orders for " & SELECTEDVALUE ( Customer[Customer Name] ). A reader opens the page from the page navigator, with no drill-through, and sees the title "Orders for ". What change gives a sensible title?
Show answer
Add an alternate result as the second argument, for example SELECTEDVALUE ( Customer[Customer Name], "all customers" ). When more than one customer is in filter context, the function returns that text instead of BLANK.
Exercise
You need a measure that returns TRUE when a slicer on Product[Category] is active, even though Category is not a grouping column in the visual. Should you use ISINSCOPE or ISFILTERED?
Show answer
Use ISFILTERED ( Product[Category] ). ISINSCOPE is true only when the column is a grouping level of the visual, and a slicer filter does not add Category as a grouping level.