Dynamically change colors in Power BI (+ Video)
Using Conditional formatting and a table
Using dynamically changing colors gives users more control over the look and feel of a Power BI report. This allows users to choose between a colorful layout or a more minimal black-and-white approach.
The advantage of the method I’m showing in this tutorial, which involves using a separate table and conditional formatting, is that it doesn’t require using bookmarks or editing JSON files, unlike many other solutions.
This method allows you to quickly and easily test different color schemes without adjusting each visual.
I added a video at the end of this article, if you prefer to click along, you can watch it!
What we will create:
- 3 buttons (this is the new slicer) with the 3 colors we choose.
- A header that changes the background and font color.
- A background that changes color.
- 1 visual that changes background, font, and border-color.
What you need:
- Power BI
- New Slicer visual
- HEX/color code you want to use
- Table (we will create this during the tutorial)
Step 1: Adding the color to a table
To begin, select the initial colors you’d like to use. Don’t worry if you change your mind later – you can always update them. You have two options for choosing colors: use the HEX code (I use a color picker extension in my browser to find the HEX code) or use the name of the color (though this option provides fewer choices).
Next, open Power BI Desktop and click on “Enter data.” To begin with, add just 3 columns. Feel free to copy this table
Theme | Main Color | Background Color |
Yellow | #eec85e | #e6ebee |
Green | #7EB2BC | #e6ebee |
Dark Blue | #373d4d | #8b909e |
Step 2: Creating the Slicer
Now, we can create the (new) slicer.
- Add the new slicer visual to the canvas in Power BI.
- Add the Main Hexa to the field.
- Go to the format pane and scroll down to “Buttons”.
- Select State: Default (if it is not selected yet)
- Open “Fill”. Here, you see the fx button. You can change something with conditional formatting whenever you see an fx button.
- Choose the format style: Field Value.
- Main Hexa is the field that the format should be based on
- Click on ok
It should look like this
Now, you can hide the value, change the shape, and change the color/transparency of the other states when you hover over them.
Step 3: Change the color of the background
I want to change the background color. Unfortunately, there is no fx button for the Power BI canvas to change the color dynamically.
What we can do is add a shape. In the format pane, you can see that the “fill” section is an fx button, which means we can use conditional formatting.
So, we are going to go to
- Shape Style
- Fill
- fx
- Field value = background HEXA
Click on one of the slicer buttons to see if it works. The background color should change according to the buttons you press.
Step 4: Adding a header (Background and font color)
Now I also want to add a header for my report. To do that, I chose another shape and add it to the top. Here I choose
- Shape style
- Fill
- Conditional formatting
and the main HEX code we created.
Now, I have already noticed that something is missing: a good border color for my slicer buttons. The dark blue button seems to have no border because it is almost the same color as the main color.
Step 4: Adding a dynamically changing border color
To do so I go to transform data and navigate to the table we created before.
When I click on the table, I go to the Applied steps and click on the configuration wheel:
Add the needed columns: I also added a column for the normal border colors I want to use in my visuals.
Theme | Main Color | Background Color | Border Slicer | Border |
Yellow | #eec85e | #e6ebee | #272A2F | #eec85e |
Green | #7EB2BC | #e6ebee | #272A2F | #7EB2BC |
Dark Blue | #373d4d | #8b909e | #ecebeb | #373d4d |
To change the border of the slicer buttons, click on the slicer visual -> format pane -> buttons -> State: Selected -> Change the border color.
Step 5: Adding a visual (background, font, border)
All these steps we have taken before, you can also apply in visuals. Let’s create a basic visual. To do so I just made up some data (feel free to copy it).
Now I add a Line and clustered column chart with year on the x axis, sales on the column y axis and target on the line y axis.
You can change the font, background, column and border color just like you did before. I prepared a new table with hex codes for the visual. Feel free to copy it.
The result could look like this:
Year | Sales | Target | Target hit |
2022 | 1000 | 800 | 1 |
2023 | 2000 | 2500 | 0 |
2024 | 3000 | 2800 | 1 |
2025 | 3500 | 3800 | 0 |
Theme | Main Color | Background Color | Border Slicer | Border | Font Header | Visual Background | Visual Border | Visual font | Neutral color |
Yellow | #eec85e | #e6ebee | #272A2F | #eec85e | #272A2F | #f6f5f3 | #eec85e | #6c5208 | #8c93a4 |
Green | #7EB2BC | #e6ebee | #272A2F | #7EB2BC | #272A2F | #ecf4f6 | #7EB2BC | #0b515f | #8c93a4 |
Dark Blue | #373d4d | #8b909e | #ecebeb | #373d4d | #ecebeb | #ECEEF3 | #373d4d | #373D4C | #8c93a4 |
Bonus: Add conditional formatting for the column
I often use conditional formatting when I have a bar and line chart. In this example I want to highlight the columns where a target was hit.
To do so I created a DAX measure based on the color table.
Replace the current column conditional formatting to see the result from the example.
DAX Measure
Column DAX = Switch(
True(),
MIN('Example Data'[Target hit]) = 1 ,
MIN ('Color Table'[Main Color]),
MIN('Color Table'[Neutral color]))
Limitations
- If you want to change the font color, you need to do it quite often (labels, title, x- and y-axis, etc.).
- Columns and row headers in a table & matrix can’t change color with conditional formatting.
- When there is more than 1 column in a column chart, conditional formatting disappears.
- The line color in a column & line chart can’t be changed.
- The background color of the canvas can’t be changed.