Sub topic 4
Format by field value: connecting a measure to a visual
This chapter shows how a measure's result reaches a visual through Format by field value. It assumes you can build table and matrix visuals, write simple measures with SUM and DIVIDE, and find your way around the Format pane.
What the option does
Rules and gradient styles keep their logic inside the formatting dialog. Format by field value moves that logic into the data model. You pick a field, and Power BI reads that field's value for each cell and uses it as the color. The field is usually a measure, so the formula decides the color and the visual only displays it.
Because the measure is evaluated once per cell, each row or column in the visual gets its own result.
The measures to connect
The color measure needs a number to test. These base measures compute it. DIVIDE returns blank instead of an error when the target is zero or missing.
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )Next, a measure that returns a hex color string. It gives green at 5% or more above target, amber at or above target, and red below target. It gives no color when the variance is blank.
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)Chapter sections later in this tutorial explain the SWITCH pattern in detail. For now, treat the measure as a black box that returns one of three color strings, or blank.
Connecting the measure to a visual
The following steps apply the measure to the Total Sales column of a table.
- Add a table visual with Product[Category] and [Total Sales].
- Open the Format pane and expand Cell elements.
- Choose the series Total Sales, then turn on Background color.
- Select the fx button next to the toggle to open the dialog.
- Set Format style to Field value.
- Under What field should we base this on?, choose Variance Color.
- Select OK.
Each Total Sales cell now takes its background from the measure. Repeat with Font color if you want colored text.
How the value travels
The sequence matters when you debug. The measure runs in the filter context of the cell it colors. A cell for the Bikes category evaluates Variance % for Bikes only. The total row evaluates it for all categories together, which is why totals can get colors you did not intend. A later chapter covers that.
What the dialog accepts
Field value formatting expects text that Power BI can read as a color. The measure's return type is what matters most.
| Measure returns | Result |
|---|---|
| Hex string such as "#2E7D32" | Color applied |
| Named color such as "Green" | Color applied |
| BLANK() | No color, default formatting kept |
| A number or an unrecognized string | No color applied |
If the dialog does not list your measure, check that it is a measure or column of text type. A measure returning a number will not appear as a valid color source in some dialogs, or will silently do nothing.
Common errors
- Choosing the wrong field. Selecting Variance % instead of Variance Color applies a number as a color, so nothing changes.
- Missing the hash. "2E7D32" without "#" is not read as a color.
- Applying to the wrong series. In a table, formatting is set per column. In a matrix, it is set per value field.
- Expecting the measure to react to the wrong context. The cell's own filters apply, not the report page's overall totals.
Exercise
A table shows Total Sales by Category. You set Background color to Field value and pick the measure Variance %, which returns 0.08 for Bikes. The cell stays unformatted. Why, and what should you pick instead?
Show answer
Variance % returns a number, and Format by field value needs a text value that reads as a color, such as a hex string. Pick a measure like Variance Color that wraps the threshold logic and returns "#2E7D32", "#F9A825", or "#C62828".