Sub topic 1
Where conditional formatting lives in Power BI
Conditional formatting changes how a visual looks based on the data it shows. Power BI puts these controls in several places, and the three format styles work differently. This chapter maps those places so you know where a measure that returns a color will plug in. Later chapters write the measures themselves.
Prerequisites
This chapter assumes you can do the following:
- Load data in Power BI Desktop and build table, matrix, and chart visuals.
- Write simple DAX measures such as
SUMandDIVIDE. - Find your way around the Format pane of a visual.
You don't need to know SWITCH, variables, or evaluation context yet. Those come in later chapters.
What conditional formatting is
Static formatting applies the same color to every value. Conditional formatting picks the color from the data, so a cell, bar, or label changes appearance when its underlying number changes. A sales table can show cells below target in red without anyone editing the report.
Power BI stores this setting on the visual, not in the model. Each visual holds its own formatting rules. If five visuals should share the same logic, you configure all five. Moving the logic into a DAX measure fixes this: you write the logic once, and every visual points at it.
The fx button
Almost every conditional formatting control starts at the same place, a small fx button next to a formatting property in the Format pane. Properties that can be driven by data show it. If a property has no fx button, you can't format it conditionally.
Clicking fx opens a dialog. The dialog has a Format style dropdown, and the style you choose decides which other options appear. Once a property has a rule applied, the fx button changes appearance. In current versions it is highlighted, which is a quick way to see which properties on a visual are data-driven.
Where you find the property depends on the visual.
Tables and matrices
Tables and matrices have the most options. In the Format pane, open Cell elements. Choose the column or measure to format from the Series dropdown. Then turn on one or more of these elements:
- Background color
- Font color
- Data bars
- Icons
- Web URL
Each toggle has its own fx button. You can also open the same dialog from the field well. Select the dropdown arrow on a field in the Values area and choose Conditional formatting.
In a matrix, you also choose which part of the matrix the formatting applies to. A matrix has value cells, subtotals, and grand totals, and a colored total can be misleading. A later chapter on totals and matrix levels covers this.
Charts
Charts use the same fx button on a different set of properties. A few examples:
- Columns or Bars > Colors on a bar or column chart sets the fill color of each bar.
- Markers or Lines on a line chart set point and line colors.
- Data labels can have their font color driven by data.
Not every chart property supports fx, and the list varies between visual types and Power BI versions. The practical test is whether the fx button is present.
Titles, cards, and other elements
Many visuals let you set text and color from data in places other than cells:
- A visual's Title text can be driven by a measure, so the title can say "Sales are 8% above target".
- The title's font color and background color can have fx buttons.
- A card's callout value color can be conditional.
- Shapes and buttons accept conditional fill colors, and buttons accept conditional text.
These all use the same dialog, so what you learn for table cells carries over.
The three format styles
For color properties, the Format style dropdown offers up to three choices. They differ in who decides the color. With Gradient and Rules, Power BI decides, using settings you type into the dialog. With Field value, your DAX decides.
| Format style | What you configure | What decides the color | Best for |
|---|---|---|---|
| Gradient | A minimum color, a maximum color, and optionally a center color | Power BI blends colors by where each value falls between the minimum and maximum | Showing intensity, such as a heat map |
| Rules | A list of conditions such as "is greater than or equal to 100", each with a color | Power BI checks the conditions in order and uses the color of the first one that is true | A few fixed thresholds on one field |
| Field value | A field that returns a color | Your measure or column returns the color as text, and Power BI paints it | Shared logic, complex conditions, any property |
Gradient
Gradient colors a value on a continuous scale. You pick the lowest and highest colors, and each value gets a shade between them. You can set the minimum and maximum to the lowest and highest values in the data, or to numbers you specify. Adding a center color gives you a diverging scale, such as red through white to green.
Rules
Rules are an ordered list of conditions. Each condition compares the value with a number, percent, or another field, and assigns a color. Power BI tests conditions in order. A common mistake is putting the broad condition first so it catches every value. You can also base a rule on a different field from the one you are formatting, which is useful for coloring one column according to another.
Field value
Field value tells Power BI to read the color from a field. You choose Field value as the format style, then pick a measure or column in What field should we base this on? That field must return a valid color as text, such as #2E7D32, or a color name. The color string is the output of your DAX, so every threshold, exception, and blank-handling rule lives in the model.
A minimal example, the same one used in the rest of this tutorial:
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )
Variance Color =
IF ( [Variance %] >= 0, "#2E7D32", "#C62828" )DIVIDE returns a blank instead of an error when the target is zero or missing. In Variance Color, a blank variance compares as 0, so those cells would come out green. That is a real problem, and the chapter on blanks fixes it. For now, notice that Variance Color is an ordinary measure whose result happens to be text.
Applying it takes four steps:
- Select the table visual and open Format > Cell elements.
- Choose the
Variance %column in Series and turn on Background color. - Click fx and set Format style to Field value.
- Pick
Variance Colorin What field should we base this on? and select OK.
The cells now take their color from the measure. The measure doesn't have to be in the visual. It only has to exist in the model.
Choosing a style
The decision usually comes down to how many places will use the logic and how complex it is.
Use Gradient or Rules when the logic is simple and lives on one visual. They need no DAX and are quick to set up.
Move to Field value when any of these apply:
- The same thresholds appear on several visuals, and you want one place to change them.
- The condition depends on more than one value, such as sales versus target and also a region flag.
- You need exact control over blanks, totals, or which matrix level gets a color.
- Users should be able to change thresholds, for example with a slicer.
- The color should depend on filters, since a measure is evaluated in the filter context of each cell.
Limits to know before you start
- Format style is per property. Setting a rule on font color doesn't affect background color. You configure each property separately.
- Field value needs valid color text. If the field returns something Power BI can't read as a color, the format silently doesn't appear. The chapter on troubleshooting covers how to find these cases.
- Gradient and Rules are not measures. You can't reuse them across visuals except by copying the visual or using Format Painter.
- Totals get formatted too unless you control them. Depending on the setting, a total row can receive a color that was meant for detail rows.
- Availability varies. Some properties support only some styles, and the Power BI service and Desktop update monthly. If an option is missing, check your Desktop version.
Where this tutorial goes next
The next chapters explain why a measure can produce a color for each cell (evaluation context), where Gradient and Rules fall short, how to write and apply color measures, and how to handle blanks, totals, parameters, chart elements, icons, and accessibility. The setup you just saw, a measure of text plus Field value, is the base for all of them.
Exercise
A report has a table, a matrix, and a bar chart that should all color sales red when they are below target and green otherwise. The target definition changes every quarter. Which format style should you use, and why?
Show answer
Field value, driven by a DAX measure that returns a hex color. The same measure can be selected on all three visuals, so a change to the target logic is made once in the measure. Rules would need the same condition entered separately on every visual.
Exercise
You turn on Background color for a column and choose Field value, but pick a measure that returns the number 1 or 0. Nothing is colored. What is the cause?
Show answer
A Field value format reads color text such as "#2E7D32" or a color name. A number isn't a valid color, so Power BI ignores it. The measure should return a text string that is a valid color.