cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
David01 Frequent Visitor
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 = REDConditional Example.PNGExample 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

Accepted Solutions
Community Support Team
Community Support Team

Re: Conditional formatting on Rows based on previous value

@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

2 REPLIES 2
Community Support Team
Community Support Team

Re: Conditional formatting on Rows based on previous value

@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

David01 Frequent Visitor
Frequent Visitor

Re: Conditional formatting on Rows based on previous value

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
Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 66 members 1,208 guests
Please welcome our newest community members: