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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
lukinfo
Frequent Visitor

Dynamic Conditional Formatting Based On Previous Year

Hi,

I don't know if this is possible, if so, how to do it? If last year's value is greater than next year's, I would like to change the next year's color to red. Just like in the attached screenshot. Please help. Thank you in advance!

 

my date

A.png

what I want to get

 

B.png

 

3 ACCEPTED SOLUTIONS
ChiragGarg2512
Solution Sage
Solution Sage

@lukinfo Try this measure:

 
color =
VAR _prev = CALCULATE([VALUE], PREVIOUSYEAR('Table (3)'[Year]))
var _curr = [VALUE]

return
SWITCH(TRUE(), _prev>_curr, "Red", "Green")

View solution in original post

Alex87
Solution Sage
Solution Sage

Hello,

You can achieve this using the following DAX measure

 

 

CF_FontValue = 

VAR _CurrentYearResult = SUM(MyTable[Value])
VAR _PrevPer = SELECTEDVALUE(Dates[CurrYearOffset]) -1
VAR _PreviousYearResult = 
        CALCULATE(
        SUM(MyTable[Value]), 
              FILTER(ALL(Dates),
               Dates[CurrYearOffset] = _PrevPer)
               )
VAR _Result = IF(_CurrentYearResult < _PreviousYearResult, "red", "black")

RETURN
_Result

 

 

 

On your matrix visual, right click on values measures and select Conditional Formatting / Font Color. Format style should be Field value based on the CF_FontValue measure. Apply on values and totals

 

Alex87_1-1715170564467.png

 

If it answers your need, please mark my reply as the solution. Thanks!

 

P.S If you are looking for the Dates calendar talble I used, you can find it here: 

Extended Date Table (Power Query M function) - Power Query / M Code Showcase - Enterprise DNA Forum




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




View solution in original post

Anonymous
Not applicable

Thanks solutions from @Alex87 and @ChiragGarg2512 , your solutions is great.

Hi, @lukinfo 

Based on the PBIX file you provided, I created a measure using the following DAX expression:

MEASURE =
VAR _curYear =
    SELECTEDVALUE ( Tabela[Date].[Rok] )
VAR _curValue =
    CALCULATE ( SUM ( Tabela[Value] ) )
VAR _lastyear =
    CALCULATE (
        SUM ( Tabela[Value] ),
        FILTER (
            ALLEXCEPT ( 'Tabela', 'Tabela'[Region] ),
            YEAR ( 'Tabela'[Date] ) = _curYear - 1
        )
    )
RETURN
    IF ( _lastyear > _curValue, "red" )

Using this metric in cell elements, the result is as follows:

vjianpengmsft_1-1715313307318.png

vjianpengmsft_0-1715313229738.png

I uploaded the PBIX file that I used this time.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

5 REPLIES 5
Alex87
Solution Sage
Solution Sage

you are not following the solution steps proposed. You do not have a date table in your model.  Add the date table as I recommend in my solution, adapt the formula accordingly and get rid of the Date hieararchy, and it will work.




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




Alex87
Solution Sage
Solution Sage

Hello,

You can achieve this using the following DAX measure

 

 

CF_FontValue = 

VAR _CurrentYearResult = SUM(MyTable[Value])
VAR _PrevPer = SELECTEDVALUE(Dates[CurrYearOffset]) -1
VAR _PreviousYearResult = 
        CALCULATE(
        SUM(MyTable[Value]), 
              FILTER(ALL(Dates),
               Dates[CurrYearOffset] = _PrevPer)
               )
VAR _Result = IF(_CurrentYearResult < _PreviousYearResult, "red", "black")

RETURN
_Result

 

 

 

On your matrix visual, right click on values measures and select Conditional Formatting / Font Color. Format style should be Field value based on the CF_FontValue measure. Apply on values and totals

 

Alex87_1-1715170564467.png

 

If it answers your need, please mark my reply as the solution. Thanks!

 

P.S If you are looking for the Dates calendar talble I used, you can find it here: 

Extended Date Table (Power Query M function) - Power Query / M Code Showcase - Enterprise DNA Forum




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




ChiragGarg2512
Solution Sage
Solution Sage

@lukinfo Try this measure:

 
color =
VAR _prev = CALCULATE([VALUE], PREVIOUSYEAR('Table (3)'[Year]))
var _curr = [VALUE]

return
SWITCH(TRUE(), _prev>_curr, "Red", "Green")

 This is my file. What am I doing wrong?

Conditional Formatting 1 

Anonymous
Not applicable

Thanks solutions from @Alex87 and @ChiragGarg2512 , your solutions is great.

Hi, @lukinfo 

Based on the PBIX file you provided, I created a measure using the following DAX expression:

MEASURE =
VAR _curYear =
    SELECTEDVALUE ( Tabela[Date].[Rok] )
VAR _curValue =
    CALCULATE ( SUM ( Tabela[Value] ) )
VAR _lastyear =
    CALCULATE (
        SUM ( Tabela[Value] ),
        FILTER (
            ALLEXCEPT ( 'Tabela', 'Tabela'[Region] ),
            YEAR ( 'Tabela'[Date] ) = _curYear - 1
        )
    )
RETURN
    IF ( _lastyear > _curValue, "red" )

Using this metric in cell elements, the result is as follows:

vjianpengmsft_1-1715313307318.png

vjianpengmsft_0-1715313229738.png

I uploaded the PBIX file that I used this time.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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