Skip to content
Home » Power BI » Dynamically Change Bar or Column Chart Colors in Power BI Using DAX

Dynamically Change Bar or Column Chart Colors in Power BI Using DAX

Dynamically Change Bar or Column Chart Colors in Power BI Using DAX
5/5 - (1 vote)

In this blog, I will show you how to dynamically change the color of a bar or column chart based on conditions using DAX Measure.

For example, let’s suppose that if my sales are less than 500, I want the bar color to be red. If the sales are greater than 500, then the color should be orange, otherwise, it should be a different color. This means I want to change colors dynamically based on the data.

Generally, we set bar or column chart colors manually, but in this blog, I will show you how to apply conditional formatting to achieve this automatically.

As you can see in the screenshot below, I have a clustered column chart displaying category-wise sales, and all category sales columns have the same color.

Clustered column chart PBI
Clustered column chart PBI

If you check the formatting options for this chart, you will notice that you can only manually change the colors for each category.

Clustered Column Chart Formatting
Clustered Column Chart Formatting

We can achieve this using conditional formatting with rules, but my requirement is to change the colors dynamically based on sales using a DAX measure.

To achieve this, we need to write a DAX measure and then use it in conditional formatting to control the colors of the visual.

Now, let’s start the practical. First, you need to create a measure by right-clicking on the dataset name and selecting “New Measure.” After that, write the following DAX code.



Column color code =
SWITCH(
TRUE(),
SUM(Sales[Sales])<= 500, "#FF0000", --Red 
SUM(Sales[Sales]) > 500 && SUM(Sales[Sales]) <= 3000, "#FBB03B",-- Orange 
SUM(Sales[Sales]) > 3000, "#2C8838", --Green
"#666666"--Grey for Deafult
)

Now, select the chart and go to “Format Visual.” Under the Columns section, make sure “All” is selected in the Categories dropdown, then click on the Fx icon.

Conditional formatting in clustered column chart
Conditional formatting in clustered column chart

The Color Categories pop-up window will open. Then, select “Field value” in the Format style dropdown. Next, expand your dataset in the other dropdown, choose the measure, and click OK.

Color conditional formatting
Color conditional formatting

As you can see in the screenshot below, the chart colors have changed according to the defined conditions and color codes.

Result
Result

Thanks for reading this post! I hope you found it helpful. Feel free to share it with others or your teammates so they can benefit from it too. 😊

Loading

Leave a Reply

Discover more from Power BI Docs

Subscribe now to keep reading and get access to the full archive.

Continue reading