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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
David01
Frequent Visitor

Conditional formatting on Rows based on previous value

I'm hoping you may be able to assist me in working out how to conditional format a cell in a matrix based on the previous value. In the example if its > previous value = REDExample ResultExample Result

 

Below is the data example  I would like to apply the formatting to

 

  • The data can be sliced by year
  • The data dates are not sequential and will jump days.
  • Plant numbers are not sequential either

 

Plant11.02.201918.02.201904.03.201925.03.201901.04.201908.04.201915.04.201902.05.201906.05.201913.05.201903.06.201911.06.2019
111,76215,4516,6133,2585,2653,1612,54512,33512,3579,6525,7605,706
2            
31,1585,2401,4262706092,8132,8773,4723,4724,6353,9279,283
4305,266398,134258,834240,575285,684308,224232,881256,614254,044271,052338,340254,701
59,6491,8221,8292,3212,3212,3212,3213,3683,3683,4603,2453,245
67,1708,99810,93611,29532,24231,94231,94433,94834,07534,20933,79133,743
7            
825,63927,28419,40719,88129,68111,62023,28530,70431,93236,04676,77289,762

 

I have tried earlier ended up with a context error (EARLIER/EARLIEST refers to an earlier row context which doesn't exist.)

 

previous Val = 
CALCULATE (
    MAX (DATA[Value] ),
    FILTER (
        ALL ( DATA ),
        DATA[DateKey] = EARLIER (DATA[DateKey] )
            
    )
1 ACCEPTED SOLUTION
v-yuta-msft
Community Support
Community Support

@David01 ,

 

Suppose in your table there's same date value in column [DateKey], create a calculate column to give index in each plant based on [DateKey].

DateKey Index Ascend = 
COUNTROWS (
    FILTER (
        DATA,
        DATA[Plant] = EARLIER ( DATA[Plant] )
            && DATA[Index] <= EARLIER ( DATA[Index] )
            && DATA[DateKey] <= EARLIER ( DATA[DateKey] )
    )
)

Then create another color column named [If Red] like below.

If Red = 
VAR Current_Index = DATA[DateKey Index Ascend]
VAR Previous_Index = Current_Index - 1
VAR Current_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Current_Index))
VAR Previous_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Previous_Index))
RETURN
IF(Current_Index <> 1 && Current_Value > Previous_Value, "Red", BLANK())

Then you know which value should be colored in red.

Capture.PNG 

Finally create a matrix visual, in advanced conditional formatting pane, select "First Value".

Capture.PNG  

I haven't input all of your data, so the result will be like below.

1.PNG 

 

Community Support Team _ Jimmy Tao

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

3 REPLIES 3
v-yuta-msft
Community Support
Community Support

@David01 ,

 

Suppose in your table there's same date value in column [DateKey], create a calculate column to give index in each plant based on [DateKey].

DateKey Index Ascend = 
COUNTROWS (
    FILTER (
        DATA,
        DATA[Plant] = EARLIER ( DATA[Plant] )
            && DATA[Index] <= EARLIER ( DATA[Index] )
            && DATA[DateKey] <= EARLIER ( DATA[DateKey] )
    )
)

Then create another color column named [If Red] like below.

If Red = 
VAR Current_Index = DATA[DateKey Index Ascend]
VAR Previous_Index = Current_Index - 1
VAR Current_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Current_Index))
VAR Previous_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Previous_Index))
RETURN
IF(Current_Index <> 1 && Current_Value > Previous_Value, "Red", BLANK())

Then you know which value should be colored in red.

Capture.PNG 

Finally create a matrix visual, in advanced conditional formatting pane, select "First Value".

Capture.PNG  

I haven't input all of your data, so the result will be like below.

1.PNG 

 

Community Support Team _ Jimmy Tao

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

 

What would be the dax if you wanted to label each of the rows red if its greater then the previous DateKey index? rather then just looking at the most current, hope that makes sense!

Thanks for the reply @v-yuta-msft 

 

I created the indexs in TSQL then applied the dax as you had written but had to make one change being I had to add a second filter. (Changed DAX below)

 

This gave me what I needed thanks.

 

Figured out why I got the error when using earlier before I was trying to use it in a measure. I wanted the report to be more dynamic in being abled to be filtered dynamically.

 

The underlying data not provided had another level being product type. So for each plant & period there were many products.

 

I had to remove that level to get the inital report to work.

 

If Red = 
VAR Current_Index = DATA[DateKey Index Ascend]
VAR Previous_Index = Current_Index - 1
var plant_key = DATA[PlantKey]
VAR Current_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Current_Index),FILTER(DATA, DATA[PlantKey] = plant_key))
VAR Previous_Value = CALCULATE(MAX(DATA[Value]), FILTER(DATA, DATA[DateKey Index Ascend] = Previous_Index),FILTER(DATA, DATA[PlantKey] = plant_key))
RETURN
IF(Current_Index <> 1 && Current_Value > Previous_Value, "Red", BLANK())

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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