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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
AhmadJamil
Frequent Visitor

Merging records from two tables

Hi,

I am sure this question has been asked before and must have a solution, nevertheless, I am struggling so asking for help. I have two tables, like following, and want to merge them in a way that I get records from both sides and where the rows match I need them on one row and where no match the amount should be left blank/zero as MergedTable below:

Table1
ID1ID2ID3Amount
12120
12230
24140

 

Table2
ID1ID2ID3Amount
12130
12315
13450
35235

 

MergedTable
ID1ID2ID3Table1_AmountTable2_AmountVariance
1212030-10
12230 30
123 15-15
134 50-50
14140 40
152 35-35

 

Just to add that I tried Megeing Queries but couldn't find the desired result.

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Rename the amount column to Amount 1 in Table2.  Append both tables in the Query Editor to get 5 columns.  In your matrix visual, drag ID1, ID2 and ID3 columns.  Write these measures

Measure = sum(Data[Amount])

Measure1 = sum(Data[Amount1])

Variance = [Measure]-[Measure1]

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
v-kongfanf-msft
Community Support
Community Support

Hi @AhmadJamil ,

 

Did @Ashish_Mathur  reply solve your problem? If so, please mark it as the correct solution, and point out if the problem persists.

 

Best Regards,
Adamk Kong

 

Ashish_Mathur
Super User
Super User

Hi,

Rename the amount column to Amount 1 in Table2.  Append both tables in the Query Editor to get 5 columns.  In your matrix visual, drag ID1, ID2 and ID3 columns.  Write these measures

Measure = sum(Data[Amount])

Measure1 = sum(Data[Amount1])

Variance = [Measure]-[Measure1]

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashih, 
Thank you for your response. The technique seems working fine with the sample data, I will apply it on the actual tables and will let you know if I need more help.

Thanks
Ahmad

bhanu_gautam
Super User
Super User

@AhmadJamil , You can do it using Power Query

 


let
// Load Table1
Source1 = Table1,
// Load Table2
Source2 = Table2,
// Merge Tables
MergedTables = Table.NestedJoin(Source1, {"ID1", "ID2", "ID3"}, Source2, {"ID1", "ID2", "ID3"}, "Table2", JoinKind.FullOuter),
// Expand Merged Table
ExpandedTable = Table.ExpandTableColumn(MergedTables, "Table2", {"Amount"}, {"Table2_Amount"}),
// Rename Columns
RenamedColumns = Table.RenameColumns(ExpandedTable, {{"Amount", "Table1_Amount"}}),
// Replace Nulls with 0
ReplaceNulls1 = Table.ReplaceValue(RenamedColumns, null, 0, Replacer.ReplaceValue, {"Table1_Amount"}),
ReplaceNulls2 = Table.ReplaceValue(ReplaceNulls1, null, 0, Replacer.ReplaceValue, {"Table2_Amount"}),
// Add Variance Column
AddVariance = Table.AddColumn(ReplaceNulls2, "Variance", each [Table1_Amount] - [Table2_Amount])
in
AddVariance

 

And I have done it with sample date please find the attached PBIX




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi Bhanu_gautam, 
Thank you for the response. Following is the merged table in your file:

 

AhmadJamil_0-1719607662289.png

 

I was having the same issue that the IDs for one of the table go blank and that creates orphan records in relationships.

 

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.