Sub topic 2
Measures and evaluation context: why a measure can drive formatting
This chapter assumes you can load data into Power BI Desktop, build table and matrix visuals, and write simple measures with SUM and DIVIDE. It explains why a measure can decide the color of a cell. The next chapters show how to connect that measure to a visual.
A measure is evaluated once per cell
A measure does not store a value. Power BI runs its formula each time a visual needs a result. In a matrix, that means one run for every cell, every row total, and every column total.
The formula stays the same each time. What changes is the evaluation context, which is the set of filters that apply to that one run. A row for the category "Bikes" adds a filter on Product[Category]. A slicer on year adds a filter on the date. The measure sees the combined filters and calculates a result for that cell only.
This is why one measure can give a different answer in every cell without any change to its code.
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )In the "Bikes" row, [Total Sales] adds up only the Bikes sales. [Variance %] then compares that sum to the Bikes target. In the grand total row, no category filter applies, so every product is included.
From a number to a color
A color is only a value, such as the text "#2E7D32". If a measure can return a number for each cell, it can return a text string for each cell in the same way. The context still decides which string comes back.
Format by field value, covered in a later chapter, takes that string and paints the cell with it. The measure does the thinking. The visual only displays the result.
Variance Color =
VAR v = [Variance %]
RETURN
IF ( v >= 0, "#2E7D32", "#C62828" )Here v is calculated in the cell's own context. A Bikes row that beats its target gets green. A Clothing row that misses its target gets red. Both rows use the same measure.
Why this is more flexible than the dialog
The rules dialog stores a few fixed ranges for one field. A measure can use any logic DAX can express. It can read other measures, compare against a target, check a slicer choice, or test where in the matrix the cell sits. You write the logic once and reuse it in many visuals.
| Approach | Where the logic lives | Typical limit |
|---|---|---|
| Rules in the dialog | In each visual's settings | Fixed ranges, repeated per visual |
| Measure that returns a color | In the data model | Needs DAX, but reusable and testable |
Common errors at this stage
- Expecting one color for the whole visual. A measure returns a value per cell. If every cell looks the same, check whether the measure ignores the row context, for example by using a fixed number.
- Mixing up columns and measures. A column is calculated once per row at refresh time. A measure is calculated at query time in the visual's context. Colors that depend on the current slicer or matrix level need a measure.
- Forgetting totals. The total row has its own context with no category filter. The measure runs there too and may return a color you did not intend. A later chapter shows how to control this.
Exercise
A matrix shows sales by product category. Your color measure returns "#2E7D32" for the Bikes row and "#C62828" for the Clothing row. Why can the same measure return two different colors, and what does the grand total row use?
Show answer
The measure is evaluated separately for each cell, and each evaluation has its own filter context. The Bikes row filters to Bikes and the Clothing row filters to Clothing, so the variance differs and so does the color. The grand total row has no category filter, so it is evaluated over all products and gets a color based on the overall variance.