Forum Discussion

crln-blue's avatar
crln-blue
Post Patron
2 years ago
Solved

Calculated Column that checks the previous row

Hello everyone, I'm trying to check the data of the previous row so I can compare it to the current row and apply some logic. I'm aware of the EARLIER and EARLIEST functions but somehow, it doesn't work as I expected them too. I wrapped them in a FILTER inside a CALCULATE function but it still doen't seem to work.

 

Below is my sample data:

Order IDOrder Item IDQty
100021000132
100021000232
100036000210
100044000128
100044000228
100044000328
100044000428
100044000528
100100000149
100100000249
1002220003108
100223000187
100223001087
100270000110
100272000116


Below is my expected result:

Order IDOrder Item IDQtyCalculated Col_Qty
10002100073232
100021000932 
10003600021010
10004400012828
100044000228 
100044000328 
100044000428 
100044000528 
10010000034949
100100000549 
1002220003108108
10022300058787
100223001087 
10027000011010
10027200011616

 

I can't really depend on the Order Item ID because sometimes, it doesn't start with "0001".

My goal is to check if the current data row of Order ID is the same with the previous row. If it is the same, set the Qty value as blank. If they are not the same, retain the Qty value.

 

Thank you!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi crln-blue ,

     

    First create an index column and count the number of occurrences of each Order ID.

    Index = RANKX(FILTER(ALL('Order'), 'Order'[Order ID] = EARLIER('Order'[Order ID])), 'Order'[Order Item ID],, ASC, Dense)

     

    Create a calculated column that sets the Qty value to blank if index is not equal to 1. If index = 1, Qty value is retained.

    Calculated Col_Qty = IF('Order'[Index] = 1,'Order'[Qty],BLANK())

     

    Drag columns to the report page for display.

     

    pbix file is attached.

     

    If you have any further questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi crln-blue ,

     

    First create an index column and count the number of occurrences of each Order ID.

    Index = RANKX(FILTER(ALL('Order'), 'Order'[Order ID] = EARLIER('Order'[Order ID])), 'Order'[Order Item ID],, ASC, Dense)

     

    Create a calculated column that sets the Qty value to blank if index is not equal to 1. If index = 1, Qty value is retained.

    Calculated Col_Qty = IF('Order'[Index] = 1,'Order'[Qty],BLANK())

     

    Drag columns to the report page for display.

     

    pbix file is attached.

     

    If you have any further questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!