Power BI technique · built-in features only
How to colour bars and table cells from a DAX measure in Power BI
By Nawaf Sharaf · Founder & Reliability Engineer · Updated
When to use it. Use a colour measure when the rule involves more than one threshold or more than one measure (variance and confidence), or when several visuals must colour the same way. Simple gradients can use the built-in Gradient style.
Worked example
Worked example · Ostrava Home · sales vs target %, bars and cells coloured by one measure
Bar chart · Colors → fx → Field value
- Brno Centrum+8.3%
- Plzeň+2.1%
- Olomouc−1.2%
- Liberec−4.7%
- Zlín−9.6%
- Pardubice (opened Sep)+0.0%
no target yet
Table · Background color → Field value
| Store | vs Target | Contrast |
|---|---|---|
| Brno Centrum | +8.3% | 5.0:1 |
| Plzeň | +2.1% | 5.0:1 |
| Olomouc | −1.2% | 5.0:1 |
| Liberec | −4.7% | 5.0:1 |
| Zlín | −9.6% | 6.5:1 |
| Pardubice (opened Sep) | (blank) | 2.5:1 |
Ostrava Home is a fictional furniture chain with six stores. One Variance Colour measure feeds both visuals: the bar chart's column colours and the table's cell backgrounds. Brno Centrum and Plzeň are ahead of target and green, Olomouc and Liberec are within five percent and amber, Zlín is red, and Pardubice opened in September with no target, so it gets the neutral grey.
The contrast column measures white text on each colour. Green and amber both reach 5.0:1 and red 6.5:1, comfortably above the 4.5:1 that WCAG asks of normal-size text. Grey only reaches 2.5:1: the neutral colour that works as a bar fails as a cell background under white text. Either darken the grey or switch the font colour to dark for blank rows, with a second Field value rule on Font color.
Steps
- 1
Write the colour measure
Return a hex string. Keep the palette in one place so every visual agrees.
Variance Colour = VAR v = [Sales vs Target %] RETURN SWITCH ( TRUE (), ISBLANK ( v ), "#9CA3AF", v >= 0, "#15803D", v >= -0.05, "#B45309", "#B91C1C" ) - 2
Colour a bar or column chart
Select the chart → Format visual → Columns (or Bars) → Colors → fx. Format style: Field value; What field should we base this on: Variance Colour.
- 3
Colour table cells
Select the table → in the Values well, open the field's menu → Conditional formatting → Background color. Format style: Field value, then the measure. Pair it with a contrasting font colour.
- 4
Check contrast and colour blindness
Text on coloured backgrounds needs enough contrast; red and green side by side also need a second cue (an icon or a sign).
How it works
With Format style set to Field value, Power BI evaluates the chosen measure once per data point (each bar, each cell) in that point's filter context and uses the returned text as the colour. The measure can therefore look at anything: a variance, a confidence flag, a target that only exists for some rows.
Because the colour lives in DAX, it is the same in every visual that uses the measure, and it is independent of the report theme. That independence is a strength for status colours that must mean the same thing everywhere, and a weakness for decorative colours, which should follow the theme so a rebrand needs one change.
The measure returns text, so it can also return a colour from a column (a Status table with a Colour column) by using SELECTEDVALUE on that column; the palette then lives in the model where it is easy to edit.
Variations
- Use the same measure for the font colour of a KPI card's callout value, so the card and the chart agree.
- Return a gradient step from a lookup of thresholds instead of three fixed colours when the scale has more than three bands.
Check that it works
Read the measure's output before you wire it to a colour: put it in a table next to the variance it tests. Every row should show a hex code you expect, and the store with no target should show the neutral one rather than an empty cell. An empty result makes Power BI fall back to the default colour, which looks like a real state.
Then open the chart and the table side by side and check each store has the same colour in both. If one visual disagrees, it is using a different measure or a rule-based format rather than Field value.
Check contrast wherever the colour sits behind text. The worked example shows why: the grey that reads fine as a bar fails under white text in a cell. Fix it with a second Field value rule on Font color that returns a dark ink for light fills.
Finally, open the report with a different theme applied. Status colours from the measure should not change; everything decorative should, which confirms the two are kept apart.
When the palette changes, edit the hex codes in one place. If the colours live in a small Status table with a Colour column, a designer can update them without touching DAX, and every visual that uses the measure picks up the change on the next refresh.
Common mistakes
- Colour measures are evaluated per data point; on a chart with a legend, colour by field value isn't available for every series setup.
- Hex strings must include the #; names like "red" also work but are hard to keep consistent.
- Theme colours change when the theme changes; hex in a measure doesn't. Keep the hex in one measure or a colour table.
- Test the palette in a colour-blindness simulator; red and green bars next to each other need the sign of the number as a second cue.
FAQ
Can the colour come from a column in a table?
Yes. Field value accepts a column with hex codes, for example a Status table with a Colour column.
Is there a palette that works for colour-blind readers?
Pick from contrast-checked themes with colour-blind-safe data colours, and add a second cue such as an icon or sign.
Sources
- Microsoft Learn: Apply conditional table formatting
- Microsoft Learn: Apply conditional formatting to visuals (colour by field value)
- DAX reference: SWITCH
- DAX reference: SELECTEDVALUE
Menu names follow Power BI Desktop as documented on Microsoft Learn; they can move between releases. The companies and figures in the worked example are invented.
Related
Assets and guides that go with this technique.