Sub topic 10
Using Measure-Driven Formatting on Charts and Titles
This chapter extends the measure-driven approach beyond table cells. You will color chart series, data labels, and titles from DAX. It assumes you can write basic measures, know the Format pane, and have already used Format by field value on a table or matrix. The examples reuse the [Variance %] and [Variance Color] measures from earlier chapters.
Where else a measure can drive formatting
Any formatting property in the Format pane that shows an fx button can be bound to a field. Clicking fx opens the same dialog you used for table cells. Choose Format style: Field value, then pick a measure that returns a color.
Common places to use this:
- Bar and column fill color, per bar.
- Line color and marker color.
- Data label font color.
- Title and subtitle font color.
- Background color of a card or a visual.
- Shape fill color in a report page.
The measure does not know which property it feeds. It returns a text value, and Power BI applies that value to whatever property you bound it to. This is why one measure such as [Variance Color] can color a table cell, a bar, and a card at the same time.
Coloring bars and columns per category
Suppose you have a clustered column chart with Product[Category] on the X-axis and [Total Sales] as the value. You want each column colored by its variance against target.
- Select the chart and open the Format pane.
- Expand Columns (or Bars), then Colors.
- Click fx next to Default color.
- Set Format style to Field value.
- Under What field should we base this on?, choose
Variance Color. - Click OK.
Each column now takes the color the measure returns for its category. The measure is evaluated once per category because the category on the axis sets the filter context for that column.
The same measure works for a bar that is broken into a legend series, but the result is less predictable. If you put a field in the Legend well, Power BI colors by legend value and the fx option for the fill may be unavailable or ignored. For measure-driven fill, leave the Legend well empty and use the axis category alone.
Returning a neutral color for missing data
For charts, a blank usually looks like a visual that was forgotten. A visible neutral color communicates "no target" more clearly:
Variance Color (Chart) =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), "#9E9E9E",
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)This differs from the table version only in the second branch. In a table you may prefer no color for blanks. In a chart you usually want gray.
Data labels and line charts
Data label color is bound the same way: Data labels > Values > Color > fx. Use the same measure. A common pairing is a bar fill colored by status and a label that stays dark gray, so the number remains readable on any fill. Bind the label color only when you have a reason, such as highlighting only the below-target values.
Line charts have a limit. A line is one continuous shape, so Power BI cannot give each segment its own color from a measure. Field value formatting can color the line as a whole (when the chart has one series) or the markers (each point). If you need per-point status on a line chart, color the markers and keep the line neutral.
| Visual element | Per-point color from a measure? | Notes |
|---|---|---|
| Column or bar fill | Yes | Leave the Legend well empty. |
| Data label font | Yes | Same measure works. |
| Line segment | No | The line is one shape. |
| Line marker | Yes | Enable markers first. |
| Card value or title text | Yes, one value | Evaluated in the card's filter context. |
| Pie or donut slice | Depends on version | Check that **fx** appears on Slice colors. |
Dynamic titles that carry status
A title can be driven by a measure in two ways: the text can come from a measure, and the font color can come from a measure. Use both so the title says what is happening and looks like it.
First, create a text measure:
Title Text =
VAR v = [Variance %]
RETURN
IF (
ISBLANK ( v ),
"Sales vs target: no target set",
"Sales vs target: " & FORMAT ( v, "+0.0%;-0.0%;0.0%" )
)The FORMAT string has three sections separated by semicolons: positive, negative, and zero. This keeps the sign visible, so +3.2% and -4.1% read clearly.
Then bind it:
- Select the visual and open Format > General > Title.
- Click fx next to Title text.
- Set Format style to Field value and pick
Title Text. - Click fx next to the title Font color, choose Field value, and pick
Variance Color.
Because the title measures are evaluated at the visual level, they reflect the current slicers and filters. If you filter to one region, the title shows that region's variance. With no filter, the title shows the grand total's variance.
Cards, KPI tiles, and backgrounds
A card visual shows one value, so a color measure is evaluated once with the card's filters. Bind Callout value > Color or the card's Background to Variance Color. This gives a simple traffic-light tile.
For a rectangle shape used as a tile background, bind Style > Fill > Color to a measure the same way. Place the card on top of the shape and the two stay in sync, because they use the same measure and the same filters. This is the technique behind many dashboard KPI strips. Remember that the shape and card must be on a page with the same slicers applied, or they may differ if one is excluded by an interaction setting.
Reference lines and constant-colored elements
Not everything accepts a measure. Reference lines (such as an Analytics pane constant line) accept a value from a measure for the position through the fx button, but their color is a fixed choice. If you want a target line, add the target as its own series, such as a line on a combo chart using [Sales Target], and color that series with a fixed color. Do not expect the line color to follow variance.
Making chart colors accessible
Chart colors follow the same accessibility rules as table colors, with an added concern: bars are often adjacent. Two points matter.
- Do not rely on red versus green alone. About 1 in 12 men have a red-green color deficiency. Choose a pair such as blue and orange, or add a text or icon cue (see the SVG chapter).
- Keep contrast against the background. The hex values in this tutorial work on a white canvas. On a dark theme, check them again.
You can build a color-blind-safe variant by only changing the hex strings:
Variance Color (Safe) =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), "#9E9E9E",
v >= 0.05, "#0072B2",
v >= 0, "#E69F00",
"#D55E00"
)Because the logic is in one measure, switching a whole report to the safe palette means editing one place.
Troubleshooting chart-specific problems
When a chart does not pick up the color, check these in order:
- Is the fx option missing? The property may not support field value in your version, or a Legend field overrides it.
- Is the measure returning text? Put the measure in a table next to the axis category and read the raw string. It must look like
#2E7D32, not a number or an error. - Is everything the same color? The measure may be evaluated at the total level. Check that the axis category is in the visual, so each point has its own filter context.
- Are bars the default color? The measure returned blank. Look for missing targets or a mismatch between the
SalesandTargetsrelationships. - Does the color differ from the table? The chart and table may have different filters through Edit interactions or visual-level filters.
Summary of the pattern
The workflow is the same for every element:
- Write or reuse a measure that returns a hex string or text.
- Open fx on the property.
- Choose Field value and select the measure.
- Check the output in a table when the result looks wrong.
Titles use two measures, one for text and one for color. Charts need the axis category to give each point its own context.
Exercise
You color columns in a clustered column chart by [Variance Color], but every column shows the same red. The same measure colors a table correctly. Name two likely causes and how you would check each.
Show answer
First, the chart may not have the category field in the axis, or the measure is evaluated at an aggregate level, so every point shares one filter context. Check by adding the category and [Variance %] to a temporary table and confirming that the values differ per category. Second, the chart may carry a different filter than the table, through Edit interactions or a visual-level filter, so its variance is negative everywhere. Check by comparing the filters on both visuals, and the table's values with the chart's tooltip values.
Exercise
Write a title measure that reads "Sales are 4.0% above target" when [Variance %] is 0.04, "Sales are 4.0% below target" when it is -0.04, and "No target set" when it is blank.
Show answer
Use a variable and SWITCH. For example: Title Status = VAR v = [Variance %] RETURN SWITCH ( TRUE (), ISBLANK ( v ), "No target set", v >= 0, "Sales are " & FORMAT ( v, "0.0%" ) & " above target", "Sales are " & FORMAT ( ABS ( v ), "0.0%" ) & " below target" ) ABS removes the minus sign so the wording "below" does not double the negative.