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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Nested If to compare previous row value

Hello,
Can anyone help me with the logic for creating nested IF statement in powerbi to get the output like below?
I have already created an index based on date and time but I don't know how to keep the index in ascending order in the formula in order to avoid comparing values with different dates and time.
Appreciate your help!!
powerbi.PNG

The logic for output.
1 ) If height2 <> height 1 -> than result "D"
2)If height2 = height 1, then compare color, if color2 <> color 1 -> than result "B"
3)If height2 = height1 and color 2 =color1, then compare size, if size2 <> size 1 -> than result "A"
4)If everything are same than "M"

1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

The following is a measure I created. And you can reference  it to have a try.

Measure = 
var mi = MAX(Table1[Index])
var hei = CALCULATE(MAX(Table1[HEIGHT]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
var col = CALCULATE(MAX(Table1[COLOR]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
var size = CALCULATE(MAX(Table1[SIZE]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
return 
IF(mi -1 = 0,BLANK(),IF(MAX(Table1[HEIGHT]) = hei , IF(MAX(Table1[COLOR]) = col, IF(MAX(Table1[SIZE]) = size, "M","A"),"B"),"D"))

2.PNG

Best Regards,

Xue Ding

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

Best Regards,
Xue Ding
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
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

The following is a measure I created. And you can reference  it to have a try.

Measure = 
var mi = MAX(Table1[Index])
var hei = CALCULATE(MAX(Table1[HEIGHT]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
var col = CALCULATE(MAX(Table1[COLOR]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
var size = CALCULATE(MAX(Table1[SIZE]),FILTER(ALLEXCEPT(Table1,Table1[DATE]),Table1[Index] = mi -1))
return 
IF(mi -1 = 0,BLANK(),IF(MAX(Table1[HEIGHT]) = hei , IF(MAX(Table1[COLOR]) = col, IF(MAX(Table1[SIZE]) = size, "M","A"),"B"),"D"))

2.PNG

Best Regards,

Xue Ding

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

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Wkeith
Helper II
Helper II

New to dax and thought I would help you out. This formula is super long and I'm sure there is an easier and simplified way to write this formula but this got the results that you want.

 

Create a new calculated column with this following logic:

 

Column = IF(Sheet1[Index]=1,"", if(CALCULATE(max(Sheet1[Height]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1)))<>Sheet1[Height], "D",if(and(CALCULATE(max(Sheet1[Height]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1)))=Sheet1[Height],LOOKUPVALUE(Sheet1[Color],Sheet1[Index],CALCULATE(max(Sheet1[Index]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1))))<>Sheet1[Color]),"B",if(CALCULATE(max(Sheet1[Height]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1)))=Sheet1[Height] && LOOKUPVALUE(Sheet1[Color],Sheet1[Index],CALCULATE(max(Sheet1[Index]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1))))=Sheet1[Color] && LOOKUPVALUE(Sheet1[Size],Sheet1[Index],CALCULATE(max(Sheet1[Index]),filter(Sheet1,Sheet1[Index]<earlier(Sheet1[Index],1))))<>Sheet1[Size],"A","M"))))
 
 
 
Would love someone else to clean it up and teach me how to do it more efficiently! Hope this works.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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