Sub topic 4
Format by field value: connecting a measure to a visual
This chapter shows how a DAX measure reaches a visual through the Format by field value option. It assumes you can build table, matrix, and chart visuals in Power BI Desktop and find your way around the Format pane. It also assumes you can write simple measures with SUM and DIVIDE. Rules, gradients, and the details of color strings and SWITCH(TRUE()) get their own chapters. Here the goal is the connection itself: what you click, what the visual does with the measure, and what goes wrong.
The idea: the visual asks the measure for a color
Normally you give a visual a rule such as "red if below 0". Power BI stores that rule in the report and applies it to each cell.
With field value formatting you store no rule in the visual. You point the format at a measure instead. For every cell, bar, or marker, Power BI evaluates that measure in the cell's own context. It then uses the text the measure returns as the color.
This has three consequences:
- The logic lives in the semantic model. One measure can format many visuals, and you change a threshold in one place.
- The logic can use anything DAX can: other measures, variables,
SWITCH, slicer selections. - The visual has no knowledge of the logic. If the measure returns something Power BI cannot read as a color, the visual silently shows no formatting.
The next diagram shows what happens for each cell.
Set up the example
You need a measure that decides the color, and the measures it depends on. The example below compares sales with a target. 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] )Now add the measure that returns a color. It returns green at 5% or more above target, amber at or above target, and red below target. It returns blank when there is no variance to judge.
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)The next chapters explain this logic line by line. For now, treat it as a measure whose output is one of three hex strings or blank.
Connect the measure to a visual
These steps use a matrix that shows Product[Category] on rows and [Total Sales] and [Variance %] as values. A table works the same way.
- Select the visual, open the Format pane, and expand Cell elements.
- Turn on Background color (or Font color). A toggle named for the element appears with an fx button next to it.
- Select fx. A dialog titled for the element opens.
- Set Format style to Field value.
- Under What field should we base this on?, choose
Variance Color. The dropdown lists measures and columns from your model. - Under Apply to, choose Values only, Values and totals, or Totals only.
- Select OK.
The format applies to the series you chose in the Series dropdown of Cell elements. In a matrix with several value fields, you pick the field in that dropdown, such as Variance %. Each value field has its own formatting settings. You can point Total Sales at the same color measure and get the same colors on a different column.
The color measure does not need to appear in the visual. It is evaluated behind the scenes. It does not add a column, and it is not affected by which fields are on the visual unless its DAX depends on them. Its filter context still comes from the rows and columns in the visual.
Other places to find the same dialog
Field value appears wherever a format has an fx button:
| Visual | Where to look | What it colors |
|---|---|---|
| Table, matrix | Cell elements: Background color, Font color | Cells |
| Table, matrix | Cell elements: Data bars (bar color) | Bars inside cells |
| Column, bar, line charts | Columns, Bars, or Lines: Color | Each category's bar or line |
| Card, KPI | Callout value: Color | The number |
| Any visual | Title: Text color, Background | Title and background |
The chart and title cases come later in the tutorial. The mechanism is the same. The dialog asks for a field, and the field returns a color for each data point.
What the measure must return
Field value formatting reads the measure's result as text. These values are valid:
- A hex string with the leading
#, such as"#2E7D32". Six digits (#RRGGBB) are safest, though some visuals also accept eight digits with an alpha value. - A named color, such as
"red"or"green". - An
rgb()string, such as"rgb(46,125,50)".
Compare two versions of the same idea. The first is correct. The second is not.
Variance Color Named =
IF ( [Variance %] >= 0, "green", "red" )
Variance Color Broken =
IF ( [Variance %] >= 0, 1, 0 )The second measure returns numbers. A number is not a color, so Power BI ignores it. You will not see an error, only unformatted cells. Missing the # in a hex string fails the same way.
Set the data type of the color measure to Text in the Measure tools ribbon. Power BI lets you pick it in the dialog either way, but a text type makes mistakes easier to spot. Keep the measure out of the way of report users by hiding it in the model view, or put it in a folder named something like Formatting.
Measures versus columns as the field
The dialog accepts both measures and columns. They behave differently.
| Source | Evaluated | Typical use |
|---|---|---|
| Measure | Per cell, in the cell's filter context | Thresholds that depend on aggregated values |
| Column | Per row of the model, then summarized | Fixed colors per category, such as brand colors |
A column such as Product[BrandColor] holds one hex string per row. When a visual groups by category, there may be several rows behind one cell, so the dialog asks for a Summarization (First, Last, Min, or Max). If every row in a category holds the same color, any of them works. If they differ, you get a result that depends on the summary you chose, which is rarely what you meant.
For anything that depends on a calculated number, use a measure. A column cannot see the filter context of the visual.
Apply to: values, totals, or both
For tables and matrices, the Apply to setting controls which rows receive the format.
- Values only formats the body cells.
- Values and totals also formats total and subtotal rows.
- Totals only formats only those rows.
The measure runs in the total's context too. At the total row there is no single category, so [Variance %] becomes the variance for everything in scope. That can turn red or green and look like a judgment about a single row when it is not. If the total should stay uncolored, choose Values only. Matrix levels need more control, because a subtotal on one level is still a "value" on another. The ISINSCOPE pattern in a later chapter handles that inside the measure.
Test the measure before blaming the visual
When a format does not show, the measure is almost always the cause. A fast check is to show its output as a column.
- Add
Variance Colorto the same table or matrix as a value. It appears as text. - Read what each row returns. A hex string means the measure works. Blank means it returned
BLANK(). A number orTruemeans the logic returns the wrong type. - Remove the column again when you are done, or keep it in a separate debug page.
This check separates two questions: "Does the measure return the right text?" and "Does the visual read the text?" If the text looks right and the cell is still uncolored, check three things:
- The fx dialog points at the right measure and the right series.
- The Background color or Font color toggle is on.
- Apply to includes the rows you are looking at.
Common mistakes
- Returning a number or Boolean. The format shows nothing. Return text.
- Forgetting
#in hex."2E7D32"is not accepted. Write"#2E7D32". - Choosing a column when you needed a measure. The color then ignores slicers and visual grouping.
- Selecting the wrong series. In a matrix, the format is set per value field. Check the Series dropdown.
- Leaving Apply to at Values and totals by accident. Totals get colors you did not intend.
- Expecting an error message. Power BI does not warn about invalid colors. It just skips them.
Summary
Format by field value hands the color decision to a measure. You enable the element, open fx, set Format style to Field value, and pick the measure. For each cell, Power BI evaluates the measure in that cell's context and applies the returned text as a color. If the text is blank or invalid, the cell stays as it was. When something looks wrong, display the measure's output in the visual and read it directly.
Exercise
A matrix shows Total Sales by Category. You created a measure Flag = IF ( [Variance %] < 0, "#C62828" ) and set the Background color of Total Sales to Format by field value with Flag. Categories below target turn red, but categories above target look the same as before, and so does the total row. Is this a bug, and how would you change the setup so the total row stays plain regardless of its variance?
Show answer
It is not a bug. When the condition is false, IF without a third argument returns BLANK, and a blank gives no color, so those cells keep the default look. That is a reasonable design if you only want to flag problems. To keep the total row plain, open the fx dialog and set Apply to Values only. The measure still runs at the total, but the visual does not use its result there.