Sub topic 1
Where Conditional Formatting Lives in Power BI
Prerequisites
This chapter assumes you can do the following:
- Load data into Power BI Desktop and build table, matrix, and chart visuals.
- Write simple DAX measures such as
SUMandDIVIDE. - Find your way around the Format pane.
No DAX beyond that is needed here. Later chapters build the measures.
What conditional formatting does
Conditional formatting changes how a visual element looks based on data. A cell can turn red when sales miss a target. A bar can get darker as its value grows. The rule that decides the appearance can live in two places: inside the formatting dialog, or inside a DAX measure. This tutorial moves that rule into measures, so this chapter maps out where the options are and why the second place matters.
Where to find the options
In Power BI Desktop, select a visual and open the Format pane. For a table or matrix, expand Cell elements. Each element has a fx button next to it, and that button opens the conditional formatting dialog.
The elements you can format depend on the visual:
| Visual | Elements with an fx button |
|---|---|
| Table and matrix | Background color, font color, data bars, icons, web URL |
| Bar and column charts | Bar or column color, data label color |
| Line and area charts | Series line or fill color |
| Cards and other visuals | Title text, subtitle, and other text colors |
Which options you see depends on the Power BI version and the visual. If a property has no fx button, it cannot be formatted conditionally.
The three format styles
The dialog has a Format style list with three entries.
- Gradient blends between two or three colors across the range of values. Use it for heat-map effects.
- Rules applies a color when a value meets a condition, such as "greater than or equal to 100".
- Field value reads the color from a field you choose. That field is normally a measure that returns a color.
Gradient and Rules keep the logic inside the dialog. Field value moves it into the data model.
Why put the logic in a measure
Dialog-based formatting works for simple cases, but it has costs.
- Repetition. Every visual needs its own copy of the rules. If the target changes from 5% to 8%, you edit each visual by hand.
- Limited conditions. A rule compares one value to fixed numbers. It cannot easily say "green if above target and the region is not blank."
- Hidden logic. Rules live in the visual, so they do not show up when you review the model.
A measure fixes each of these. One measure can drive a table, a matrix, and a chart. The threshold is changed in one place. The logic is readable DAX that you can test and version like any other measure.
A first look at the pattern
The measure-driven approach has two parts. First, a measure computes the number to test. Second, a second measure turns that number into a color. This example shows the first part, which later chapters build on.
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )DIVIDE returns blank instead of an error when the target is zero or missing. That detail matters later, when blanks need to be handled on purpose.
The color measure and the steps to connect it to a visual come in later chapters. For now, remember the shape of the workflow: the fx button opens the dialog, Field value is selected, and a measure supplies the color.
Common mistakes at this stage
- Looking for the fx button on a property that does not support it. Check that the visual and property are formattable.
- Using a Rules format when the same logic is needed in several visuals. This leads to copies that drift apart.
- Expecting a measure to appear in the Field value list automatically. Only fields suitable for the format are offered, and the measure must return text or a number.
Exercise
A report has a table, a matrix, and a column chart. All three should show red when sales are below target and green otherwise, and the target may change next quarter. Which format style fits best, and why?
Show answer
Field value, using a DAX measure that returns the color. The logic is written once and shared by all three visuals, so a changed target is edited in one place. Rules would need the same condition copied into each visual.
Exercise
Name the three format styles in the conditional formatting dialog and say which one reads its color from a measure.
Show answer
Gradient, Rules, and Field value. Field value reads the color from a field, normally a measure that returns a color.