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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
GMadd
Helper I
Helper I

Power BI Power Query Append Two table Distinct Values Only Based on two columns each row

I have two tables I need to append in excel files.

The first table called "Main" and the file name is called "Yearly Shipping". This file contains data for everything shipped year to date. 

The second table is called "MtoD" and the file name is called "Monthly Shipping".

The ERP I am using gives me a report daily that is always month to date. Meaning I will get all shipping data from August 1st through August 20th (today is August 20th). Tomorrow I will get the report and it will be from August 1st through August 21st and so on down the road. I want power query to remove duplicates based on two columns in each row. These two columns are called "Delivery" and "Material". If another row has the same delivery number and material number I do not want to add it to the table called "Main". I need to know how to write M-code to state I only want to see distinct rows and I am struggling on how to do this. Below is a sample of my "Main" table and "MtoD" table and what I want the "Main" tabl

 Main Report 
DeliveryAct. Gds Mvmnt DateMaterial
881375217/30/202423461BIO
881375227/30/202422852BIO
881375227/30/202423642BIO
8813053519-Aug-2436112CLX
8813053519-Aug-2436106CLX
8813601919-Aug-2429005CLX
8813601919-Aug-2412005CLX
8813601919-Aug-2450006ACL
8814287619-Aug-2423505PTM
8814287619-Aug-2423504PTM
8814287619-Aug-2423606PTM
   
 MtoD 
DeliveryAct. Gds Mvmnt DateMaterial
8813053519-Aug-2436112CLX
8813053519-Aug-2436106CLX
8813601919-Aug-2429005CLX
8813601919-Aug-2412005CLX
8813601919-Aug-2450006ACL
8814287619-Aug-2423505PTM
8814287619-Aug-2423504PTM
8814287619-Aug-2423606PTM
8813053519-Aug-2559232CLX
8813053519-Aug-2511022CSP
   
 End Result (Appended Table)
DeliveryAct. Gds Mvmnt DateMaterial
881375217/30/202423461BIO
881375227/30/202422852BIO
881375227/30/202423642BIO
8813053519-Aug-2436112CLX
8813053519-Aug-2436106CLX
8813601919-Aug-2429005CLX
8813601919-Aug-2412005CLX
8813601919-Aug-2450006ACL
8814287619-Aug-2423505PTM
8814287619-Aug-2423504PTM
8814287619-Aug-2423606PTM
8813053519-Aug-2559232CLX
8813053519-Aug-2511022CSP
1 ACCEPTED SOLUTION

No worries. You can change the line that is currently...

#"Appended Query" = Table.Combine({#"Changed Type", MtoD})

to

#"Appended Query" = Table.Distinct(Table.Combine({#"Changed Type", MtoD}))

The #"Changed Type" replaces the 'mainTable' in what I initially wrote while the MtoD replaces the 'mtdTable'.





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

Proud to be a Super User!





View solution in original post

6 REPLIES 6
dataflip
Advocate III
Advocate III

The most efficient way to handle this scenario is by using Power Query's anti-join logic to prevent duplicate records when appending the "MtoD" table to the "Main" table. Since both tables contain overlapping data based on the columns "Delivery" and "Material", the first step is to create a composite key in each table by combining these two columns (e.g., Delivery-Material).

This unique key allows you to easily identify duplicates. Once the keys are created, you can use a List.Contains function or a left anti-join to filter the "MtoD" table, keeping only the records that do not exist in the "Main" table.

After filtering, you append the new unique rows from "MtoD" to "Main" to create the final combined dataset. This approach ensures that only new shipping records are added each day, avoiding duplication of historical data. The solution is scalable, works well with daily ERP updates, and keeps your Year-to-Date (YTD) shipping data accurate and clean.

wckeneson
New Member

Here is the code that I was able to solve this problem with (Thank you COPILOT)
DistinctTable = DISTINCT( UNION( SELECTCOLUMNS(Table1, "ColumnName", Table1[Column1]), SELECTCOLUMNS(Table2, "ColumnName", Table2[Column2]) ) )

jgeddes
Super User
Super User

Does this work for you?

Table.Distinct(Table.Combine({mainTable, mtdTable}))

 





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

Proud to be a Super User!





Thanks for the help! I should have said I am very new to writing these formulas; with that said where do I add this into the append formlua below?

let
Source = Excel.Workbook(File.Contents("C:\Users\gmadden\Yearly Shipping.xlsx"), null, true),
Main_Sheet = Source{[Item="Main",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Main_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Delivery", Int64.Type}, {"Plant", type text}, {"Created On", type date}, {"Act. Gds Mvmnt Date", type date}, {"Goods Issue Date", type date}, {"Name of the ship-to party", type text}, {"Location of the ship-to party", type text}, {"Region", type text}, {"Postal Code", type text}, {"Incoterms", type text}, {"Material", type text}, {"Description", type text}, {"Actual delivery qty", Int64.Type}, {"Base Unit of Measure", type text}, {"Net weight", type number}, {"Weight Unit", type text}, {"Shipping Conditions", Int64.Type}, {"Route", type text}, {"Created By", type text}, {"Reference Document", Int64.Type}, {"Total Pallets", Int64.Type}, {"Total Weight", type number}, {"Ship-To Party", Int64.Type}, {"Purchase Order Number", type text}, {"TU partner", type text}, {"Otm Shipment Number", Int64.Type}, {"Sold-To Party", Int64.Type}, {"Name of sold-to party", type text}, {"Overall pick.status", type text}, {"Overall WM status", type text}, {"Container/Trailer/PRO Number", type text}, {"Pick up Appt Date", type date}, {"Pick up Appt Time", type datetime}, {"Rush Order Flag", type text}, {"Delay Reason Code - Header", type text}, {"Profit Center", Int64.Type}, {"Net Value", type number}, {"Time Created", type datetime}, {"Carrier Assigned Date", type date}}),
#"Appended Query" = Table.Combine({#"Changed Type", MtoD})
in
#"Appended Query"

No worries. You can change the line that is currently...

#"Appended Query" = Table.Combine({#"Changed Type", MtoD})

to

#"Appended Query" = Table.Distinct(Table.Combine({#"Changed Type", MtoD}))

The #"Changed Type" replaces the 'mainTable' in what I initially wrote while the MtoD replaces the 'mtdTable'.





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

Proud to be a Super User!





Thank you for your help! This worked perfectly.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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