Sub topic 11
DAX measures that adapt to context
This chapter assumes you can build a basic Power BI report and write simple DAX measures such as SUM and CALCULATE. It also assumes you know that every visual evaluates a measure inside a filter context. Drill-down, drill-through, and tooltips all work by changing that filter context. A measure can read the new context and change what it returns.
Why measures need to react
A report title, a label, or a tooltip often has to say what the reader is looking at. A fixed title such as "Orders" does not change when the reader drills through to one customer. A measure can inspect the filter context and build text or numbers to match.
This chapter covers three patterns, one for each navigation feature:
SELECTEDVALUEfor drill-through pages.ISINSCOPEfor hierarchies and drill-down.- A comparison measure for tooltips.
Reading the drill-through selection
When a reader drills through from a customer, the drill-through page receives a filter on Customer[Customer Name]. SELECTEDVALUE returns the value if exactly one remains in the filter context. Otherwise it returns the fallback you supply.
Drill-through Title =
"Orders for " & SELECTEDVALUE ( Customer[Customer Name], "multiple customers" )Put this measure in a card or text box and use it as the page title. If the reader opens the page directly from the page tabs, no single customer is selected. The title reads "Orders for multiple customers", which is accurate and avoids a blank title.
Detecting the hierarchy level
In a visual with a hierarchy, the same measure runs at each level as the reader drills down. ISINSCOPE returns TRUE when a column is the level currently being grouped by in the visual. Test from the deepest level upward, because a deeper level in scope usually means the higher levels are also present.
Level Label =
IF (
ISINSCOPE ( Product[Product Name] ), "Product",
IF (
ISINSCOPE ( Product[Subcategory] ), "Subcategory",
IF ( ISINSCOPE ( Product[Category] ), "Category", "Total" )
)
)Use this pattern when a calculation should differ by level. For example, you can show a percent of parent at the product level and a plain value at the total.
The measure is not tied to drilling. It works in any matrix or table that uses these columns, whether the reader drills or expands.
A comparison measure for tooltips
Tooltips evaluate measures for the hovered data point. For a column in a bar chart for 2025, the tooltip runs in a filter context that includes the year 2025 and the category on that bar. A year-over-year measure uses that context to find the same period a year earlier.
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales PY], [Sales PY] )Add Sales YoY % to the Tooltips field well of a visual, or place it in a visual on a tooltip page. SAMEPERIODLASTYEAR needs a proper date table with continuous dates, marked as the date table, and the Date column must be in the filter context. If it is not, the measure returns blank.
DIVIDE returns blank when the previous year is zero or missing, so the tooltip does not show a division error.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Title always shows the fallback text | The page has no drill-through filter, or more than one value is selected |
| `Level Label` always returns "Total" | The measure is in a card, which has no grouping columns in scope |
| `Level Label` shows the wrong level | Levels are tested from the top down instead of the bottom up |
| `Sales PY` is blank | The date table has gaps or is not marked as a date table |
Applying the pattern
- Decide which question the reader asks at each step: which item, which level, or compared to what.
- Pick the function that reads that part of the context:
SELECTEDVALUE,ISINSCOPE, or a time-intelligence function. - Return a safe fallback so the visual stays readable when the context is not what you expected.
Exercise
A matrix shows Category, then Subcategory, then Product Name. You want a measure that returns "Category" at the top level and "Product" at the lowest level. Why must ISINSCOPE ( Product[Product Name] ) be tested before ISINSCOPE ( Product[Category] )?
Show answer
At the Product Name level, Category is also in scope because the hierarchy groups by all three columns. If Category were tested first, the measure would return "Category" at every level. Testing the deepest level first lets each row return the correct label.
Exercise
A drill-through page title built with SELECTEDVALUE ( Customer[Customer Name], "multiple customers" ) shows "multiple customers" after the reader drills through from a bar that represents two customers. Why, and what could you do?
Show answer
Two customer names are in the filter context, so SELECTEDVALUE cannot return a single value and returns the fallback. You could change the fallback to a more helpful message, or use CONCATENATEX over VALUES ( Customer[Customer Name] ) to list the selected customers.