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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
EmmaLiuLiu
Frequent Visitor

How to identify a row based on another row?

Hello,

 

I got stuck on this column creation for a while. Really hope someone could help me out here. I even start to doubt if what I want to achieve is doable in power bi or not....

 

What I want to achieve:

  1. If it's a duplicate in column "key", the column "Quantity" has a positive value that has an offset negative value in another row, and the Plant codes are different. Then mark the negative quantity row "delete". Like the last two rows, I want to keep the last second one and delete the last one. So these two records indicate the same transaction. One receiving and one supplying. I only want to keep one.
  2. If the movement type "351" is a duplicate in column “key”, the plant codes are the same, then mark "delete" to the row that has a Posting Date that’s no later than the one in the row with movement type “352” . So movement 352 has a corresponding movement 351, which needs to be identified.

It can be in two columns since these are two different requests there. 

 

@muhammad_786_1 I couldn't upload files but I copied and pasted my sample file here. You can copy and paste over and import into your power bi desktop? Sorry for all the trouble. Thanks in advance!!!

 

Sample table:

PlantMaterialMovement typePurchase orderItemkeyPosting DateQuantity
64170-810-030-8053514000121095700-810-030-8054000121095703/13/20248.00
64170-810-030-8053524000121095700-810-030-8054000121095703/13/2024-1.00
64170-810-030-8053514000121095700-810-030-8054000121095708/8/20233.00
64170-810-034-5173514000121095900-810-034-5174000121095908/7/20231.00
66020-810-015-6543514000121902100-810-015-6544000121902109/5/20232.00
66020-810-015-6543514000121902100-810-015-65440001219021011/8/20233.00
66020-810-015-61935140001219021000-810-015-619400012190210010/3/20232.00
66020-810-018-91035140001219022100-810-018-910400012190221010/18/20234.00
66020-810-020-80735140001219022200-810-020-807400012190222010/18/20233.00
66020-810-018-90835140001219022300-810-018-908400012190223010/18/202312.00
66020-810-018-90835240001219022300-810-018-908400012190223010/19/2023-6.00
66020-810-018-90835140001219022300-810-018-908400012190223011/2/20236.00
64820-810-037-4713514000122926100-810-037-47140001229261010/9/20231.00
65090-810-037-4713514000122926100-810-037-47140001229261010/9/2023-1.00

 

What I want to achieve:

 

PlantMaterialMovement typePurchase orderItemkeyPosting DateQuantityTarget Column 1Target Column 2
64170-810-030-8053514000121095700-810-030-8054000121095703/13/20248.00 Delete
64170-810-030-8053524000121095700-810-030-8054000121095703/13/2024-1.00 Keep
64170-810-030-8053514000121095700-810-030-8054000121095708/8/20233.00 Keep
64170-810-034-5173514000121095900-810-034-5174000121095908/7/20231.00 Keep
66020-810-015-6543514000121902100-810-015-6544000121902109/5/20232.00 Keep
66020-810-015-6543514000121902100-810-015-65440001219021011/8/20233.00 Keep
66020-810-015-61935140001219021000-810-015-619400012190210010/3/20232.00 Keep
66020-810-018-91035140001219022100-810-018-910400012190221010/18/20234.00 Keep
66020-810-020-80735140001219022200-810-020-807400012190222010/18/20233.00 Keep
66020-810-018-90835140001219022300-810-018-908400012190223010/18/202312.00 Delete
66020-810-018-90835240001219022300-810-018-908400012190223010/19/2023-6.00 Keep
66020-810-018-90835140001219022300-810-018-908400012190223011/2/20236.00 Keep
64820-810-037-4713514000122926100-810-037-47140001229261010/9/20231.00Keep 
65090-810-037-4713514000122926100-810-037-47140001229261010/9/2023-1.00Delete 

 

Have been stuck here for 2 days now. Any help will be appreciated!

1 ACCEPTED SOLUTION

Hi @EmmaLiuLiu - Please check the below calculated column for Target column2

Target Column2 =
IF (
    [Movement type] = 351
    && COUNTROWS (
        FILTER (
            Matl,
            Matl[Key] = EARLIER ( Matl[Key] )
            && Matl[Movement type] = 352
            && Matl[Plant] = EARLIER ( Matl[Plant] )
            && Matl[Posting Date] = EARLIER ( Matl[Posting Date] ) // Same Posting Date
        )
    ) > 0,
    "Delete",
    "Keep"
)

rajendraongole1_0-1725890914032.png

 

Replace with your table name, Hope this works.

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

3 REPLIES 3
rajendraongole1
Super User
Super User

Hi @EmmaLiuLiu - Create a Calculated Column for Target Column 1 compare each row's Quantity with other rows having the same Key but different Plant values and opposite Quantity signs. replace the tablename as per your model.

rajendraongole1_0-1725855929522.png

Target_Column_1 =
VAR CurrentKey = 'matl'[key]
VAR CurrentQuantity = 'matl'[Quantity]
VAR CurrentPlant = 'matl'[Plant]
RETURN
    IF (
       
        COUNTROWS (
            FILTER (
                'matl',
                'matl'[key] = CurrentKey &&
                'matl'[Quantity] = -CurrentQuantity &&
                'matl'[Plant] <> CurrentPlant
            )
        ) > 0,
        "Delete",  // Mark the row for deletion
        "Keep"     // Otherwise, keep the row
    )
 
create another calculated column to get the Target column 2 this will mark rows with Movement Type 351 where there's a corresponding 352 row, and the 351 row's Posting Date
rajendraongole1_1-1725856024498.png
Target_Column_2 =
VAR CurrentKey = 'matl'[key]
VAR CurrentMovementType = 'matl'[Movement type]
VAR CurrentPostingDate = 'matl'[Posting Date]
RETURN
    IF (
        CurrentMovementType = 351 &&
        // Check if there's a corresponding 352 movement with the same key and later Posting Date
        COUNTROWS (
            FILTER (
                'matl',
                'matl'[key] = CurrentKey &&
                'matl'[Movement type] = 352 &&
                'matl'[Posting Date] >= CurrentPostingDate
            )
        ) > 0,
        "Delete",  // Mark the 351 row for deletion
        "Keep"     // Otherwise, keep the row
    )
 
you have two different target columns, you can use both to evaluate each row for deletion or retention based on condition. 
Hope this helps.
 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Hi @rajendraongole1 , thank you so much for your help! 

 

The first target column works. However, the second one doesn't work. I spent quite a long time myself to dig into it since my post was marked as spam. I figured one of my requests won't be solved using power dax. If two or more records have the same date and everything else is the same except the movement type (2 with 351 and 1 with 352) and or tracking info that I didn't include in the sample data, I only need to delete one of them. I need to go in there, read the tracking info and identify which record to be deleted. Is there a dax can do this job?

Hi @EmmaLiuLiu - Please check the below calculated column for Target column2

Target Column2 =
IF (
    [Movement type] = 351
    && COUNTROWS (
        FILTER (
            Matl,
            Matl[Key] = EARLIER ( Matl[Key] )
            && Matl[Movement type] = 352
            && Matl[Plant] = EARLIER ( Matl[Plant] )
            && Matl[Posting Date] = EARLIER ( Matl[Posting Date] ) // Same Posting Date
        )
    ) > 0,
    "Delete",
    "Keep"
)

rajendraongole1_0-1725890914032.png

 

Replace with your table name, Hope this works.

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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