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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
itsmeanuj
Helper IV
Helper IV

If condition to compare 2 rows and 2 columns and create a flag in 3rd column.

Hi,

I have 3 columns sorted on Product and Order#. I want to create a flag (4th column) based on this condition which works in excel like this -> =IF(AND(A2=A1,B2=B1,C2<>C1),1,0). I am not sure how to implement this I Power Query Editor in PBI on large set of data. Can someone please help?

 

itsmeanuj_0-1634885756491.png

 

Thanks,

Anuj

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

Hi, @itsmeanuj 

Could you please tell me whether your problem has been solved?

You can also try a formula as below:

flag =
VAR current_index = 'Table'[Index]
VAR pre_product =
    CALCULATE (
        MAX ( 'Table'[Product] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
VAR pre_Order =
    CALCULATE (
        MAX ( 'Table'[Order#] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
VAR pre_lot =
    CALCULATE (
        MAX ( 'Table'[LOT] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
RETURN
    IF ( [Product] = pre_product && [Order#] = pre_Order && [Lot] <> pre_lot, 1, 0 )

Best Regards,
Community Support Team _ Eason

View solution in original post

2 REPLIES 2
v-easonf-msft
Community Support
Community Support

Hi, @itsmeanuj 

Could you please tell me whether your problem has been solved?

You can also try a formula as below:

flag =
VAR current_index = 'Table'[Index]
VAR pre_product =
    CALCULATE (
        MAX ( 'Table'[Product] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
VAR pre_Order =
    CALCULATE (
        MAX ( 'Table'[Order#] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
VAR pre_lot =
    CALCULATE (
        MAX ( 'Table'[LOT] ),
        FILTER ( 'Table', 'Table'[Index] = current_index - 1 )
    )
RETURN
    IF ( [Product] = pre_product && [Order#] = pre_Order && [Lot] <> pre_lot, 1, 0 )

Best Regards,
Community Support Team _ Eason

amitchandak
Super User
Super User

@itsmeanuj , for that you need an index column https://stackoverflow.com/questions/45715963/creating-an-index-column-for-power-bi

then new column in DAX

= var _prd = maxx(filter(Table,[Index] =earlier([Index]) -1) , [Product])

var _ord = maxx(filter(Table,[Index] =earlier([Index]) -1) , [Order#])

var _Lot= maxx(filter(Table,[Index] =earlier([Index]) -1) , [Lot])

 

retrun

if([Product]=_prd && [Order #] =_ord && [Lot] <> _lot,1,0)

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors
Top Kudoed Authors