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

Text based conditional formatting in Power BI

Updated: Dec 1, 2022

Have you ever wondered if you can apply conditional formatting based on a text field/measure instead of a numeric field/measure?

If your answer is yes, then this trick is for you!


The other day I was working with a customer who asked something that I had no idea how to build.


They wanted to apply conditional formatting over some of their visuals, but they wanted the conditional formatting applied over a text field and not over a numeric field or a measure.


Let me give you a practical example. Imagine I have a table with sales data. In my table I have sales by country, product, shipping status etc.

Now, imagine that you wanted to apply conditional formatting over the status field which contains the following categories:

> Pending

> Shipped

> Cancelled


But... You don't have an "ID" column for the status, something like:








So you don't have that "Status ID" column, only the status text. I know, you could easily create the status ID column either in the Query Editor or as a calculated DAX column, but for the purpose of this post, let's imagine you can't or don't want to do that.


If you try to apply conditional formatting, you have 3 options.


You can use Rules as your formatting style, but for this you need a numeric value:

So, this option doesn't really work if your field is a text field or if your measure retrieves a text value.


The second option is using Gradient as your formatting style:

Which will allow you to use a text column, but not a measure that returns a text value.

Also, you can only have 3 options for your colors (Minimum value color, Center and Maximum) and it doesn't allow you to define the color based on the text, as the only summarisations possible for a text field are "Count" or "Distinct Count". This means that the color formatting will be based on the count of your text field, not the text itself.


This leaves us with a final third option... The Field Value formatting style:

In this case, you can use both a text column and a measure that retrieves a text value.

This is the secret option to apply conditional formatting over a text field!


But... how do you actually do this?


To make this work, you need to have a mapping of the HEX color codes and the text field you want to use for your conditional formatting, something like this:








In this example, I want to see a different color based on if the Sales Order is Cancelled (grey), Pending (yellow) or Shipped (green).


You don't need to have this mapping in a table/column though, but this trick would still work if you had it as a separate dim table or a column.


The first thing I'm going to do, is to create a measure that will do this "mapping" for me:

Status color = 
var selected_status = SELECTEDVALUE(Amazon[Status L1])
return
if(selected_status= "Cancelled", "#D5D8DC",
    if(selected_status="Pending", "#F4D03F", 
        if(selected_status="Shipped", "#27AE60", "#FDFEFE")))

Let's analyse this measure in parts:

Status color = 
var selected_status = SELECTEDVALUE(Amazon[Status L1])
return
if(selected_status= "Cancelled", "#D5D8DC",
    if(selected_status="Pending", "#F4D03F", 
        if(selected_status="Shipped", "#27AE60", "#FDFEFE")))

This variable will check what is the order status that is selected and will retrieve that text value.



Status color = 
var selected_status = SELECTEDVALUE(Amazon[Status L1])
return
if(selected_status= "Cancelled", "#D5D8DC",
    if(selected_status="Pending", "#F4D03F", 
        if(selected_status="Shipped", "#27AE60", "#FDFEFE")))

The if statement is then going to apply the "color mapping" we defined earlier.

If the status is Cancelled then it will retrieve the gray hex code (#D5D8DC) if it's Pending it will retrieve the yellow hex code (#F4D03F) and so on.


I don't recommend you to do this if you have 20 different text categories you want to apply your conditional formatting on for obvious reasons... You will end up with an infinite if statement...


As you already have your "mapping" measure, now it's time to apply the conditional formatting. In your conditional formatting options, select the type of formatting you want, Background color or Font Color (this won't work for Icons or Web URL options):


In the Conditional Formatting window select the Format Style "Field value" and then select your newly created measure:

The rest of the fields can remain with their default options (Summarization - First & Apply to - Values only).


Believe it or not, this is all you have to do!

This will work for the matrix and table visuals, but also for bar charts and other visuals that allow you to apply conditional formatting.


Now let's see this trick in action with an example.


Imagine you have the following table, which has the orders with a few details for each order and you want the text that contains the Order ID to be colored based on the order status field, which is a text (but we already created the mapping measure so no issues here!):


Like I mentioned before, you can do this not only for a matrix/table visual, but also for other visuals like bar charts:



Happy conditional formatting days!



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


15,911 views6 comments

6 Comments


ABCD Index: Since you're interested in indexing, you may want to check if IJSRET or IJSET are already listed in the ABCD Index. Journals listed here are recognized for certain standards, which can enhance the visibility and credibility of your research.

Journal Standards: Both the International Journal of Scientific Research and Engineering Trends (IJSRET) and the International Journal of Scientific Engineering and Technology (IJSET) cater to scientific and engineering research. Verify the indexing and peer-review practices of these journals if your goal is publication in reputable, well-indexed journals.

To visit the website for the International Journal of Research in Tourism and Hospitality Sciences (IJRTSSH), you can go to ijrtssh.com. This should give you access to their publication guidelines, submission details, and other relevant information about the journal. If you…


Like

I’ve always been curious about cloud chasing, and this article made it sound fun. Definitely trying the vape juice brands in Canada.

Like

In Power BI, conditional formatting is a powerful tool that allows you to dynamically change the appearance of your visuals based on the values in your data. While it is commonly used for numerical data, you can also apply conditional formatting based on text values. This can be useful when you want to highlight or write my assignment certain categories or labels in your reports. Here's a step-by-step guide on how to apply text-based conditional formatting in Power BI:

Like

When working on complex tasks, students often seek management assignment help to better understand key concepts in business and organizational studies. To approach such assignments effectively, it’s crucial to break down the topic, focus on relevant case studies, and use clear, structured arguments. Resources like textbooks, online research, and group discussions can also aid in crafting well-informed responses. Reviewing management theories and their practical applications can further strengthen your understanding and approach to the assignment.

Like

Conditional formatting in Power BI is a valuable resource for anyone looking to enhance their data visualization skills. For students navigating the complexities of data analysis, mastering these techniques is essential but can be quite challenging. That's why seeking the best assignment help can make a significant difference, ensuring they grasp these concepts while keeping up with their academic requirements. With expert guidance, students can confidently apply these skills in real-world projects.

Edited
Like
bottom of page