Sub topic 2
Measures and Evaluation Context: Why a Measure Can Drive Formatting
This chapter explains why a DAX measure can decide the color of a cell, a bar, or a title. The rest of the tutorial depends on this model. If you know how a measure is evaluated, you can predict what color each part of a visual will get.
Prerequisites
This chapter assumes you can:
- Load data into Power BI Desktop and build table, matrix, and chart visuals.
- Write simple DAX measures such as
SUMandDIVIDE. - Find your way around the Format pane.
You do not need to know SWITCH or VAR yet. Later chapters cover them.
A measure is a formula, not a stored value
A column in a table stores one value per row. Power BI calculates it when the data refreshes, and the value stays fixed afterward.
A measure stores only a formula. Power BI runs it each time a visual needs a result. The same measure can return different results in different places in the same report.
Take this measure:
Total Sales = SUM ( Sales[Amount] )Put it in a matrix with Product[Category] on the rows. The matrix has one cell for each category, and Power BI evaluates Total Sales once for each cell. Each evaluation returns a different number. The total row is a further evaluation, with no category restriction.
This is the property that makes measure-driven formatting work. If a measure that returns a number gets evaluated once for each cell, then a measure that returns a color gets evaluated once for each cell too. Each cell can receive its own color.
Evaluation context
Evaluation context is the set of conditions that tell a measure which rows of the data to use. It has two parts.
- Filter context is the set of filters active for a calculation. A row header such as "Bikes", a slicer selection, a page filter, and a visual-level filter all add to it.
- Row context is the "current row" while DAX iterates over a table, for example inside a calculated column or an iterator such as
SUMX.
For formatting, filter context matters most. Suppose a matrix shows Product[Category] on rows and Calendar[Year] on columns. The cell for Bikes in 2025 has this filter context:
Product[Category] = "Bikes"Calendar[Year] = 2025- Any slicer or page filters that are also active.
SUM ( Sales[Amount] ) adds up only the rows of Sales that pass all of those filters. Power BI builds a new filter context for every cell and runs the measure again.
The two measures in the diagram are separate. They share the same filter context, so the color always describes the same slice of data as the number beside it.
A measure can return text
Measures are not limited to numbers. A measure can return text, a date, a Boolean, or a blank. A measure that returns the text "#2E7D32" is valid DAX:
Fixed Green = "#2E7D32"This measure returns the same text in every cell, so it is not useful on its own. It becomes useful when the text depends on the filter context. Here is the pattern used throughout this tutorial. The first measures calculate the numbers, and the last one turns the numbers into a color:
Total Sales = SUM ( Targets[Placeholder] )The line above is wrong on purpose, and it shows a common slip: the measure refers to a column that does not exist, so Power BI reports an error. The correct base measures use the real columns from the sales model:
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )DIVIDE returns a blank instead of an error when the target is zero or missing. This matters for formatting. A blank can be handled on purpose, while an error often breaks the format with no clear message.
Now the color measure:
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)For each cell, Power BI does the following:
- It builds the filter context for the cell.
- It evaluates
[Variance %]inside that context. - It compares the result against the thresholds.
- It returns a color string.
The visual reads that string and paints the cell. You will connect the measure to the visual in a later chapter. For now, the point is that the logic lives in the model, where every visual can reuse it.
Why this beats a per-visual rule
Rules set in the formatting dialog belong to one visual. If three visuals need the same thresholds, you set them three times, and a later change means three edits.
A color measure sits in the model. All visuals that use it share one definition. Change 0.05 to 0.10 in the measure, and every connected visual changes.
The measure can also use anything DAX can calculate. A dialog rule compares one field to fixed numbers. A measure can compare two measures, read a slicer, look at the previous year, or check whether the current row is a total.
| Feature | Dialog rule | Color measure |
|---|---|---|
| Where the logic lives | In one visual | In the model |
| Reuse across visuals | Copy by hand | Reference the same measure |
| Compare two measures | Limited | Any DAX expression |
| React to a slicer | Fixed values only | Yes |
| Control totals and levels | Little control | Full control |
Context transition and what the measure sees
A color measure depends on the context that its visual supplies. That has consequences.
The measure sees the cell, not the visual
Power BI evaluates the color measure once per formatted element: per cell, per bar, per data point. It does not see the other cells. A measure cannot say "make the largest cell red" unless it calculates the maximum itself, for example with MAXX over the same rows, using ALLSELECTED to define which rows count.
Totals have a different context
The total row of a matrix has no filter on Product[Category]. The measure runs there with all categories in scope. [Variance %] for the total is a real number, so it receives a real color. That color describes the overall result, which may not be what the reader expects next to the category rows. A later chapter shows how to test the level with ISINSCOPE and suppress the color on totals.
The same measure can behave differently in different visuals
Suppose a table has Product[Category] and Product[Name] on rows, and a card shows the same measure with no grouping at all. The card has an empty filter context. The table rows each carry a category and a name. The color measure returns different results in each place because each place supplies different filters. This is correct behavior, but it can surprise you when a card and a table disagree.
Slicers change the colors
A slicer adds to the filter context of every visual on the page. If a user selects one region, the sales and targets in every cell change, so the variance changes, and so do the colors. A fixed dialog rule does not change meaning, but a measure follows the data.
A test you can run before formatting anything
Check the measure's output before you connect it to any visual. Place it in a table next to the values it depends on:
- Add a table visual.
- Drag
Product[Category]into it. - Add
[Total Sales],[Sales Target],[Variance %], and[Variance Color].
Each row now shows the inputs and the resulting color string. You can see directly whether a green cell has a variance above 0.05. A blank in the color column means the measure returned a blank, which is a valid result that produces no color.
Common errors at this stage
- Measure returns a number instead of a color. If you write
v >= 0.05, 1by mistake, Power BI has nothing it can read as a color. The format does not apply. - Measure refers to a column instead of a measure. A bare
Sales[Amount]in a measure needs an aggregation such asSUM. Without one, the measure fails in a visual with no row context. - Color tested against the wrong measure. Testing
[Total Sales] >= 0instead of[Variance %] >= 0gives every cell green, because sales are almost always positive. - Mixed return types. If one
SWITCHbranch returns text and another returns a number, DAX may convert values or raise an error. Keep every branch a text color or a blank.
Check your understanding
Exercise
A matrix shows Category on rows. The Variance Color measure is applied to the Total Sales column. The Bikes row is green and the Accessories row is red. Explain why the same measure gives two different colors, and say what filter context the total row uses.
Show answer
Power BI evaluates the measure once per cell. The Bikes row has the filter Product[Category] = "Bikes" and the Accessories row has Product[Category] = "Accessories", so each calculates a different Variance % and falls into a different threshold. The total row has no filter on Category, so the measure runs over all categories and returns the color for the overall variance.
Exercise
You change a threshold in Variance Color from 0.05 to 0.10. Three visuals use the measure. How many visuals do you need to edit, and why?
Show answer
None of the visuals. The threshold lives in the measure in the model, and all three visuals read the color from it. They pick up the new result the next time they refresh.