top of page
Millington Offices (10).png
Mara Pereira

How to change the colors of your visuals based on the selected measure

Updated: Dec 1, 2022

Have you ever wondered how people do the trick below?


Say no more!


I'm going to share with you all the tricks that you need to do to achieve something like this.


In my example, I created a report where you could switch the measure the charts were showing, and with that, the color of the charts and KPI cards changed to.


The first thing you need to do is create your base measures. In my example my base measures were:

Number of Orders = count(Amazon[Order ID])
Orders Value £ = SUM(Amazon[Amount])

I only used two measures, but you can have more measures if you want to.


The next step is to create a field parameter, that will allow you to switch between the two measures:


My parameter formula looks like this:

Parameter 1 = {
    ("Orders Value £", NAMEOF('Amazon'[Orders Value £]), 0),
    ("Number of Orders", NAMEOF('Amazon'[Number of Orders]), 1)
}

In the formula you can change the description that will show in your slicer (highlighted in green). You need to memorize the number for your measure (highlighted in pink) as we will be using it later:

> Orders value measure = 0

> Number of orders measure = 1



Next, we need to replace the values from our charts with this new parameter, so that when you select the measure using the slicer, this will be reflected on your charts:



Now, we are going to create a few measures that will help us with switching between different colors. First, I'm going to create a measure that will allow me to switch colors on my bar charts. The measure I used is below:

Status color = 
var selected_measure = SELECTEDVALUE('Parameter 1'[Parameter 1 Order])
return
if(selected_measure= 0, "#7FB7BE", "#7A6174")

So this measure will evaluate which measure we have selected in our measures slicer and then, based on the selection, it will retrieve different HEX color codes. If the measure is 0, which means we have selected the Number of orders, then it will retrieve the hex code #7FB7BE, if not (meaning that we have the Orders Value £ selected) then retrieve the hex code #7A6174.


After creating the new measure, we need to define the conditional formatting of our bar chart colors, so that the correct color is showed depending on the measure selection:



The only thing missing now is applying some conditional formatting on the KPI cards, so that we have the KPI card "highlighted" based on the measure selected in the slicer.

To do this, I created 2 extra measures. You can have everything in the same measure, but to keep it simple I decided to build separate measures, one to change the color of the Number of orders KPI card, and another one to change the color of the Order value KPI card.

The logic I applied is the same as the measure I built to change the colors of the bar charts:

Status color Number of Orders = 
var selected_measure = SELECTEDVALUE('Parameter 1'[Parameter 1 Order])
return
if(selected_measure= 1, "#7A6174", "#D5D8DC")
Status color orders value = 
var selected_measure = SELECTEDVALUE('Parameter 1'[Parameter 1 Order])
return
if(selected_measure= 0, "#7FB7BE", "#D5D8DC")

We can go to our formatting pane now and apply some conditional formatting over the background color of our KPI cards:


And that's it! Now we have our report changing colors depending on the metric that we have selected in our slicer!



Pro tip

You can do something similar to switch between light mode/dark mode on your report without leaving the same report page. However, this will be a significant amount of work as you have to apply loads of conditional formatting over your charts (title color, bars color etc).





Interested in learning more about Power BI Report Design? Then the Power BI Report Design Bootcamp is for you!


3,434 views1 comment

1 Comment


Guest
Sep 21, 2022

Thanks for sharing! Nice!

Like
bottom of page