Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply

Conditional formatting of a cell in a row of the matrix

Hi all,

 

I can't figure out why my measure doesn't work properly. I'm using a measure for background conditional formatting of a cell based on certain conditions (I'm comparing values between W0, W1, and W2). If it's the lowest it should be red, highest - green, in the middle - yellow.

 

This is my DAX code

 

Formatting_Value1 =
VAR ValueW0 = CALCULATE(SUM(Data[Value1]),Data[date]="W0")
VAR ValueW1 = CALCULATE(SUM(Data[Value1]),Data,Data[date]="W1")
VAR ValueW2 = CALCULATE(SUM(Data[Value1]),Data,Data[date]="W2")

RETURN

SWITCH(TRUE(),
ValueW0 < ValueW1 && ValueW0 < ValueW2 && SELECTEDVALUE(Data[date])="W0", "red",
ValueW0 <= ValueW1 && ValueW0 >= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 >= ValueW1 && ValueW0 <= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 > ValueW1 && ValueW0 > ValueW2 && SELECTEDVALUE(Data[date])="W0", "green", BLANK())
 
The logic says it should execute only if all conditions are met, but it seems it takes into account only SELECTEDVALUE(Data[date])="W0" because the color is always green, which is not correct. Please help to fix this or suggest another logic.
 
powerbiuser444_0-1627856740129.png

 

I'm attaching pbix  in case if it's more helpful 

 

 

1 ACCEPTED SOLUTION

@powerbiuser444 Weird, I just fixed it but had to use ALL:

Formatting_Value1 = 
VAR ValueW0 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W0")
VAR ValueW1 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W1")
VAR ValueW2 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W2")

RETURN
SWITCH(TRUE(),
ValueW0 < ValueW1 && ValueW0 < ValueW2 && SELECTEDVALUE(Data[date])="W0", "red",
ValueW0 <= ValueW1 && ValueW0 >= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 >= ValueW1 && ValueW0 <= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 > ValueW1 && ValueW0 > ValueW2 && SELECTEDVALUE(Data[date])="W0", "green", BLANK())


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5

I fixed myself, it should be in the brackets

SWITCH(TRUE(),
ValueW0 < ValueW1 && ValueW0 < ValueW2 && SELECTEDVALUE(Data[date])="W0", "red",
( ValueW0 <= ValueW1 && ValueW0 >= ValueW2 ) && SELECTEDVALUE(Data[date])="W0", "yellow",
( ValueW0 >= ValueW1 && ValueW0 <= ValueW2 ) && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 > ValueW1 && ValueW0 > ValueW2 && SELECTEDVALUE(Data[date])="W0", "green", BLANK())

@powerbiuser444 Weird, I just fixed it but had to use ALL:

Formatting_Value1 = 
VAR ValueW0 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W0")
VAR ValueW1 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W1")
VAR ValueW2 = CALCULATE(SUM(Data[Value1]),ALL(Data),Data[date]="W2")

RETURN
SWITCH(TRUE(),
ValueW0 < ValueW1 && ValueW0 < ValueW2 && SELECTEDVALUE(Data[date])="W0", "red",
ValueW0 <= ValueW1 && ValueW0 >= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 >= ValueW1 && ValueW0 <= ValueW2 && SELECTEDVALUE(Data[date])="W0", "yellow",
ValueW0 > ValueW1 && ValueW0 > ValueW2 && SELECTEDVALUE(Data[date])="W0", "green", BLANK())


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler actually you are right, thanks a lot

Hi @Greg_Deckler I changed the link, please check now

Greg_Deckler
Super User
Super User

@powerbiuser444 Couldn't access that link. First thing to try, in your measure try returning ValueW1 for example and return that as a value in your matrix versus background color and see if it shows up correctly in the W0 column. That way at least you will know if the calculation for that variable is correct or not within the filter context provided by the visual.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.