Sub topic 10
Using measure-driven formatting on charts and titles
This chapter assumes you can build table, matrix, and chart visuals in Power BI Desktop. It also assumes you can write simple DAX measures, and that you have used "Format by field value" on a table cell. Here you reuse the same color measure on chart series, data labels, and titles.
Which elements accept a measure
Anywhere the Format pane shows an fx button next to a color, you can choose Format style: Field value and pick a measure. The measure must return a color text such as #2E7D32. This works the same way as in a table. Only the element you format changes.
Common places to use it:
- Bars or columns in a bar or column chart.
- Markers and lines in a line chart.
- Data labels and their text color.
- Title and subtitle text color.
- Visual background and border.
Some elements only take a fixed color. If the fx button is missing, that element cannot be driven by a measure.
Coloring bars by variance
Use the measures from the earlier chapters. The base measures and the color measure are:
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)To apply it to a clustered column chart with Product[Category] on the axis and [Total Sales] as the value:
- Select the chart and open Format visual.
- Expand Columns and then Colors.
- Select the fx button next to Color.
- Set Format style to Field value.
- Under What field should we base this on?, choose
Variance Color. - Select OK.
Each bar now takes the color the measure returns for its category. The measure is evaluated once per bar, with the filter context of that category, which is the same logic as a table row.
Formatting a title
A title color works the same way. Open Title, then Text, then the fx button next to Font color, and choose a measure. Because a title belongs to the whole visual, the measure is evaluated with no category filter. It sees the total across all rows that the page and slicers allow.
That makes it useful as a headline signal. If Variance Color is applied to the title, the title turns green, amber, or red based on overall performance.
Using a different measure for labels
You can drive data labels with a separate measure. For example, you may want labels to stay neutral unless a bar misses the target:
Label Color =
IF ( [Variance %] < 0, "#C62828", "#404040" )Apply it under Data labels, Values, Color, with Field value. Keeping label logic in its own measure lets you change it without touching the bar colors.
Common problems on charts
- All bars are one color. The measure may be evaluated without the axis category in scope. Check that the category field is on the axis and not only in a tooltip.
- A series color is ignored. If the chart has a Legend field, the series are colored by legend value. Remove the legend or use the fx button on the specific series options.
- No color at all. The measure returned blank or text that is not a valid color. Add it to a table next to the category and read its output.
Exercise
A column chart shows Total Sales by Category. You apply Variance Color to the chart title and every column stays the default blue. Why, and what do you change?
Show answer
The title option only affects the title, and it evaluates the measure once for the whole visual. To color each column, open Columns, then Colors, select fx, choose Field value, and select Variance Color there.
Exercise
You want data labels to turn red only when a category is below target and stay dark gray otherwise. Which kind of measure do you write, and where do you apply it?
Show answer
Write a measure that returns a hex string, such as IF ( [Variance %] < 0, "#C62828", "#404040" ). Apply it under Data labels, Values, Color, using Format style Field value.