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
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
Super User
Super User

@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 Specialist
Solution Specialist

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

View solution in original post

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 Specialist
Solution Specialist

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.

Alex87
Solution Specialist
Solution Specialist

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

ChiragGarg2512
Super User
Super User

@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 

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel

Power BI Monthly Update - May 2024

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