Context-aware KPI model
Make the business contract explicit
An explicit DAX measure evaluates in filter context. Revenue and cost add across rows, profit is their difference, margin is a ratio of sums, budget is valid only at month/category or coarser, and closing stock is valid only when each selected product has one snapshot on the last selected date. A blank budget or stock value is not a zero. Use the supplied metric dictionary and reference measures in the lab.
Example 1 — Compare only compatible contexts
Create measures on Sales or in a dedicated measures table. Enter each definition individually. Use Month[MonthStart] and Category[CategoryID] for the overview.
Revenue = SUM(Sales[Revenue])Cost = SUM(Sales[Cost])Profit = [Revenue] - [Cost]Margin = DIVIDE([Profit], [Revenue])Budget Valid =IF(NOT ISFILTERED('Date') && NOT ISFILTERED(Product), SUM(Budget[Amount]), BLANK())Variance = IF(ISBLANK([Budget Valid]), BLANK(), COALESCE([Revenue], 0) - [Budget Valid])January: revenue 230, budget 240, variance -10. February: 240, 240, 0. Both: 470, 480, -10. Product A1: revenue 220 but budget and variance blank. ISFILTERED detects a direct filter on Date or Product, not the upstream Month or Category filter in this controlled model. This conservative guard suppresses even a directly filtered full day/product set. It is not a general allocation algorithm.
Example 2 — Closing stock with completeness
Use the last date in the selected Date context. Reapply that date while retaining category/product filters. The ETL must guarantee one snapshot per product/date and nonblank quantity before this count-based measure is valid.
Closing Stock =VAR EndDate = MAX('Date'[Date])VAR Expected = COUNTROWS(VALUES(Product[ProductID]))VAR Observed = CALCULATE(COUNTROWS(Inventory), REMOVEFILTERS('Date'), 'Date'[Date] = EndDate)RETURN IF(Expected > 0 && Observed = Expected, CALCULATE(SUM(Inventory[Units]), REMOVEFILTERS('Date'), 'Date'[Date] = EndDate), BLANK())January ends with 23 units (10+5+8). February A2 is missing, so all-category stock is blank; B1 has a real zero. January–February stock is blank because February is the closing month, not because January stock was lost. This model permits day selection on the detail page: a day with no snapshot returns blank, not the preceding snapshot.
Validate grouped totals
Category A revenue is 300, cost 190 and profit 110; B revenue is 170, cost 100 and profit 70. Combined margin is 180/470, about 38.30%, not the average of category margins. Category A budget is 300 and B budget 180. Category B February stock is 0, while category A February stock is blank. Use an explicit status label on the report. Clear slicers between tests; maintain the fixed Jan–Feb page filter.
Common mistakes
Do not average margin percentages, fill absent snapshots with zero, sum month-end stock over months, or report a repeated budget on every product line. Do not let fact fields be used as slicers: the budget guard only covers direct Date/Product filters and assumes the published dimension-only design. Outside the declared reporting months, do not claim the budget or stock is complete. The measures are staged examples, not native execution results.
Practice — Predict before selecting
Compute February category B revenue, budget, variance, margin and closing stock. Then select only product A2 across both months: predict revenue, budget and stock. Explain which result changes because of a missing row versus an invalid grain. Check the answer sheet only after recording the predictions. Sources: https://learn.microsoft.com/en-us/dax/isfiltered-function-dax; https://learn.microsoft.com/en-us/power-bi/guidance/dax-divide-function-operator