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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Conditional Formatting on Matrix Row level based on formula

Hi,

 

Need a bit of dax help from the wizards. 

 

I'm trying to create a colour scale where the colour of a cell in a matrix table depends on the % increase/decrease from the previous value (in row).

Please note this is in Customer > Database level

 

So in the below table, Customer 1, DB1 would show a 200% increase from 2022-05 (10) to 2022-06 (30)

 

customerdatabase2022-052022-062022-07
Customer 1DB1103020
 DB23050100
Customer 2DB15155
 DB2101010

 

The colour scale should be something along the lines of:

<-25% decrease : Colour 1

>-25% - 0% : Colour 2

>0%: Colour 3

 

Hope thats enough to make sense!

1 ACCEPTED SOLUTION
MartijnW
Frequent Visitor

Hi Majad,

 

This is an interesting one that I had to play around with a little. Anyone who can optimize this solution, please do.

 

Basically you create a measure in DAX to calculate the percentage between this month and last month first, then you base your conditional formatting on a field like so:


conditional formatting previous month.png

 

Here is the DAX measure I made to do this:

Change  % = 

VAR cumulativeMinusOne = CALCULATE( SUM('Table'[Value]), PARALLELPERIOD('Calendar'[Date],-1,Month), 'Calendar'[Date] <= MAX('Table'[Year-Month]))

VAR cumulativeMinusTwo = CALCULATE( SUM('Table'[Value]), PARALLELPERIOD('Calendar'[Date],-2,Month), 'Calendar'[Date] <= MAX('Table'[Year-Month]))

VAR valueLastMonth = cumulativeMinusOne - cumulativeMinusTwo
VAR valueThisMonth = SUM('Table'[Value])
VAR changePercentage = DIVIDE( valueThisMonth, valueLastMonth, BLANK() )

RETURN IF( DIVIDE( valueThisMonth, valueLastMonth, BLANK()) <> BLANK(),  DIVIDE( valueThisMonth, valueLastMonth, BLANK()) -1, BLANK() )

View solution in original post

6 REPLIES 6
MartijnW
Frequent Visitor

Hi all,

 

As I suspected, the above example is far more complex than it needs to be. Please see this link for the usage of the function DATEADD(). Far more comprehensive.

 

https://dax.guide/dateadd/

 

You can quite easily replace the first three variables with this one function, that is meant for exactly this purpose.

 

Martijn

MartijnW
Frequent Visitor

Hi Majad,

 

This is an interesting one that I had to play around with a little. Anyone who can optimize this solution, please do.

 

Basically you create a measure in DAX to calculate the percentage between this month and last month first, then you base your conditional formatting on a field like so:


conditional formatting previous month.png

 

Here is the DAX measure I made to do this:

Change  % = 

VAR cumulativeMinusOne = CALCULATE( SUM('Table'[Value]), PARALLELPERIOD('Calendar'[Date],-1,Month), 'Calendar'[Date] <= MAX('Table'[Year-Month]))

VAR cumulativeMinusTwo = CALCULATE( SUM('Table'[Value]), PARALLELPERIOD('Calendar'[Date],-2,Month), 'Calendar'[Date] <= MAX('Table'[Year-Month]))

VAR valueLastMonth = cumulativeMinusOne - cumulativeMinusTwo
VAR valueThisMonth = SUM('Table'[Value])
VAR changePercentage = DIVIDE( valueThisMonth, valueLastMonth, BLANK() )

RETURN IF( DIVIDE( valueThisMonth, valueLastMonth, BLANK()) <> BLANK(),  DIVIDE( valueThisMonth, valueLastMonth, BLANK()) -1, BLANK() )
Anonymous
Not applicable

Hi, thanks for the comprehensive response! I feel like this could work...just need a bit of help with the fields

 

i have...

 

table[value]

calendar[date] - from date dimension

 

however i dont have year-month in the table, only in the date dimension. I only have a date field in the table. Will we need to adjust?

 

edit: ok i created year-month in the table however i got the following response when creating the conditional formatting

 

Majad_Chowdhury_0-1660126636292.png

 

Hi Majad,

You need to make sure that the year-month column is of type Date.

 

Make a connection between this year-month column and calendar[date].

Anonymous
Not applicable

this worked, thank you, thank you, thank you!

Hi Majad,

 

Glad I could help!

 

Martijn

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.