Sub topic 1
Where Conditional Formatting Lives in Power BI
Prerequisites
You should know how to load data, create table, matrix, and chart visuals, write simple DAX measures, and use the Power BI Format pane. This chapter assumes you know that a measure is evaluated when a visual requests its value.
Conditional Formatting and Evaluation Context
Conditional formatting changes a visual property, such as a cell background, font color, data bar, or icon, based on data. In Power BI, these settings usually live in the visual's Format pane. Select a visual, find a property such as Background color, and choose the conditional formatting button.
A measure can drive this property because Power BI evaluates the measure separately for each data context. In a table, the context may represent one product row. In a matrix, it may represent a category and month. The measure can return a different color for each context.
flowchart TD
A[Visual row or cell] --> B[Evaluation context]
B --> C[DAX measure]
C --> D[Color value]
D --> E[Conditional formatting]caption: How a measure supplies a format value alt: A visual cell creates an evaluation context, a DAX measure returns a color, and conditional formatting applies it.
For example, this measure returns a color based on sales variance:
Variance Color =
VAR v = [Variance %]
RETURN
SWITCH (
TRUE (),
ISBLANK ( v ), BLANK (),
v >= 0.05, "#2E7D32",
v >= 0, "#F9A825",
"#C62828"
)The measure returns green for results at least 5% above target, amber for results from 0% through 4.99% above target, and red for results below target. A blank result receives no color.
Rules and Gradient Styles
Power BI also provides built-in conditional formatting styles:
- Rules apply fixed conditions, such as “less than zero.”
- Gradient maps low and high values to colors across a continuous scale.
- Color scale is useful for showing relative magnitude.
These options are quick to configure. They become less suitable when the logic must be reused across several visuals, ignore totals, handle missing targets, or use thresholds selected by a user. A DAX measure centralizes that logic.
Format by Field Value
To connect the measure to a visual:
- Select the visual.
- Open the Format pane and find the target property.
- Choose Conditional formatting.
- Set Format style to Field value.
- Select
Variance Coloras the field. - Apply the formatting.
Power BI then evaluates Variance Color in each cell's context and uses its result as the color.
Exercise
Which format style should you choose when a DAX measure returns the color for each cell?
Show answer
Choose Field value.