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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Matrix Background Color Based on Field Value: Can it be a Gradient?

Hi all, I've been trying to add conditional background colors to a Matrix visual.

 

This is what my visual looks like (Excel representation):

Excel representation of my dataExcel representation of my data

 

 

 

 

 

 

My dataset has the Business Divisions as coming from one Column and Revenue and Cost as being in the SAME "Values" column. The Cost-Income Ratio is a measure calculated by doing Cost/Revenue.

 

This is what my dataset looks like: 

Business DivisionYearTypeValue
Mangement Consulting2020Revenue100
Mangement Consulting2021Revenue105
Mangement Consulting2022Revenue120
Technology Consulting2020Revenue320
Technology Consulting2021Revenue290
Technology Consulting2022Revenue240
Tax Consulting2020Revenue80
Tax Consulting2021Revenue75
Tax Consulting2022Revenue60
Audit2020Revenue50
Audit2021Revenue60
Audit2022Revenue65
Mangement Consulting2020Cost80
Mangement Consulting2021Cost70
Mangement Consulting2022Cost85
Technology Consulting2020Cost140
Technology Consulting2021Cost190
Technology Consulting2022Cost270
Tax Consulting2020Cost40
Tax Consulting2021Cost45
Tax Consulting2022Cost55
Audit2020Cost90
Audit2021Cost40
Audit2022Cost45

 

I then used this measure to get the Cost-Income Ratio:

Cost/Income Ratio = 
VAR CostVal =
CALCULATE(
SUM('Data'[Value]),
FILTER('Data', CONTAINSSTRING('Data'[Type], "cost"))
)
VAR RevenueVal =
CALCULATE(
SUM('Data'[Value]),
REMOVEFILTERS('Data'[Type]),
-- This is a separate table with the headers names stored as a slicer and connected to 'Data' on the Type field. Did this because the "Ratio" field needs to be on the matrix, but is non-existent in the data.
ALL('Data_Headers'),
CONTAINSSTRING('Data'[Type], "revenue")
)
RETURN
IFERROR(CostVal/RevenueVal,0)

 

This table is used for the headers of the Matrix, with the Header Conenction field being 1-many with 'Data' Type field:

Header Name

Header Connection
RevenueRevenue
CostCost
RatioCost

 

I used a Switch measure on the Matrix to chose [Value] when the SELECTEDVALUE('Header Name') is not "Ratio", and to show the [CI Ratio] when the SELECTEDVALUE() contains "Ratio".

 

Finally, I used this measure to have the background be RED when the Cost-Income Ratio is above 1.0 and green when it is below:

Background Color: Ratio Table = 
SWITCH(
TRUE,
CONTAINSSTRING(SELECTEDVALUE('Data'[Header Name]), "Ratio"),
IF('Data'[Cost/Income Ratio] >= 1.0, "#ffc7ce", "#c6efce")
)

 

The issue is that a Cost-Income ratio of 90% is equally as green as a Cost-Income Ratio of 10%. However, 10% CI is way better than 90%. Same goes for CI Ratios over 100%. Is there a way to add a gradient to both sides of this? Where the larger the value, the deeper the hue (it's either better or worse as it moves away from 1.00 CI Ratio). I've been having trouble because since all of this is coming from 1 field/series, but I only want to show a gradient under the "Ratio" column, not under the "revenue" and "cost" column, it means that I have to return the background color as a field value. Is there some sort of way I can make this color field value be a function of what's in the 'Data' field?

 

I would appreciate any help on this. Thank you! 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Don't try to make Power BI behave like Excel. They have different strengths.

 

lbendlin_0-1673488769968.png

see attached.

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

Don't try to make Power BI behave like Excel. They have different strengths.

 

lbendlin_0-1673488769968.png

see attached.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.