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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
emohammad
Frequent Visitor

Merge two table with two columns OR condition

Hi,
I need to merge two tables with two columns using an OR condition on a power query. Below are the two sample tables and expected results. 
Can anyone suggest a way to perform a similar SQL query in the power query? 
 
select * from 
test1 t1
full join test2 t2
on t1.Employee_Id=t2.Employee_Id OR t1.Email=t2.Email
 
test1
Employee_IdEmailDepartmentCityManager
22221[email protected]Dept1City1Manager1
22222[email protected]Dept1City1Manager1
22223 Dept1City2Manager1
22224[email protected]Dept2City2Manager2
22225[email protected]Dept2City2Manager2
22226[email protected]Dept2City3Manager3
22227[email protected]Dept2City3Manager3

 

test2
Employee_IdEmailGradeExpertiseQualification
22221[email protected]Grade1Expertise1Qualification1
 [email protected]Grade1Expertise1Qualification1
22223[email protected]Grade1Expertise2Qualification1
22224 Grade2Expertise2Qualification2
22225[email protected]Grade2Expertise3Qualification3
22226[email protected]Grade3Expertise3Qualification3
22227[email protected]Grade3Expertise4Qualification3

 

 

Final Result
EMPLOYEE_IDEMAILDEPARTMENTCITYMANAGEREMPLOYEE_IDEMAILGRADEEXPERTISEQUALIFICATION
22221[email protected]Dept1City1Manager122221[email protected]Grade1Expertise1Qualification1
22222[email protected]Dept1City1Manager1 [email protected]Grade1Expertise1Qualification1
22223 Dept1City2Manager122223[email protected]Grade1Expertise2Qualification1
22224[email protected]Dept2City2Manager222224 Grade2Expertise2Qualification2
22225[email protected]Dept2City2Manager222225[email protected]Grade2Expertise3Qualification3
22226[email protected]Dept2City3Manager322226[email protected]Grade3Expertise3Qualification3
22227[email protected]Dept2City3Manager322227[email protected]Grade3Expertise4Qualification3
test1.pngtest2.pngFinal result.png
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @emohammad ,

 

Please try:

let
    test1 = #table(
        {"Employee_Id", "Email", "Department", "City", "Manager"},
        {
            {22221, "[email protected]", "Dept1", "City1", "Manager1"},
            {22222, "[email protected]", "Dept1", "City1", "Manager1"},
            {22223, null, "Dept1", "City2", "Manager1"},
            {22224, "[email protected]", "Dept2", "City2", "Manager2"},
            {22225, "[email protected]", "Dept2", "City2", "Manager2"},
            {22226, "[email protected]", "Dept2", "City3", "Manager3"},
            {22227, "[email protected]", "Dept2", "City3", "Manager3"}
        }
    ),
    test2 = #table(
        {"Employee_Id", "Email", "Grade", "Expertise", "Qualification"},
        {
            {22221, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {null, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {22223, "[email protected]", "Grade1", "Expertise2", "Qualification1"},
            {22224, null, "Grade2", "Expertise2", "Qualification2"},
            {22225, "[email protected]", "Grade2", "Expertise3", "Qualification3"},
            {22226, "[email protected]", "Grade3", "Expertise3", "Qualification3"},
            {22227, "[email protected]", "Grade3", "Expertise4", "Qualification3"}
        }
    ),
    MergedTables = Table.NestedJoin(test1, {"Employee_Id"}, test2, {"Employee_Id"}, "test2", JoinKind.LeftOuter),
    ExpandedTable = Table.ExpandTableColumn(MergedTables, "test2", {"Email"}, {"test2_Email"}),
    AddedColumn = Table.AddColumn(ExpandedTable, "Table1ID", each if [Email] = null then [test2_Email] else [Email]),
    MergedTables2 = Table.NestedJoin(test2, {"Employee_Id"}, test1, {"Employee_Id"}, "test1", JoinKind.LeftOuter),
    ExpandedTable2 = Table.ExpandTableColumn(MergedTables2, "test1", {"Email"}, {"test1_Email"}),
    AddedColumn2 = Table.AddColumn(ExpandedTable2, "Table2ID", each if [Email] = null then [test1_Email] else [Email]),
    JoinTables=Table.NestedJoin(AddedColumn,{"Table1ID"},AddedColumn2,{"Table2ID"},"NewTest2",JoinKind.FullOuter),
    #"Expanded NewTest2" = Table.ExpandTableColumn(JoinTables, "NewTest2", {"Employee_Id","Email","Grade", "Expertise", "Qualification"}, {"Test2.Employee_Id","Test2.Email","Grade", "Expertise", "Qualification"}),
    RemoveColumns = Table.RemoveColumns(#"Expanded NewTest2",{"test2_Email","Table1ID"}),
    #"Sorted Rows" = Table.Sort(RemoveColumns,{{"Employee_Id", Order.Ascending}})
in
    #"Sorted Rows"

vbofengmsft_0-1732243603170.png

Best Regards,

Bof

View solution in original post

6 REPLIES 6
wdx223_Daniel
Community Champion
Community Champion

wdx223_Daniel_0-1732086313855.png

 

Anonymous
Not applicable

Hi @emohammad ,

 

Since a table cannot have duplicate column names, I have merged the two tables based on your conditions and filled in the missing Employee_ID and Email. I have also removed the duplicate columns in the final result.

Please try:

let
    test1 = #table(
        {"Employee_Id", "Email", "Department", "City", "Manager"},
        {
            {22221, "[email protected]", "Dept1", "City1", "Manager1"},
            {22222, "[email protected]", "Dept1", "City1", "Manager1"},
            {22223, null, "Dept1", "City2", "Manager1"},
            {22224, "[email protected]", "Dept2", "City2", "Manager2"},
            {22225, "[email protected]", "Dept2", "City2", "Manager2"},
            {22226, "[email protected]", "Dept2", "City3", "Manager3"},
            {22227, "[email protected]", "Dept2", "City3", "Manager3"}
        }
    ),
    test2 = #table(
        {"Employee_Id", "Email", "Grade", "Expertise", "Qualification"},
        {
            {22221, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {null, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {22223, "[email protected]", "Grade1", "Expertise2", "Qualification1"},
            {22224, null, "Grade2", "Expertise2", "Qualification2"},
            {22225, "[email protected]", "Grade2", "Expertise3", "Qualification3"},
            {22226, "[email protected]", "Grade3", "Expertise3", "Qualification3"},
            {22227, "[email protected]", "Grade3", "Expertise4", "Qualification3"}
        }
    ),
    MergedTables = Table.NestedJoin(test1, {"Employee_Id"}, test2, {"Employee_Id"}, "test2", JoinKind.LeftOuter),
    ExpandedTable = Table.ExpandTableColumn(MergedTables, "test2", {"Email"}, {"test2_Email"}),
    AddedColumn = Table.AddColumn(ExpandedTable, "Filled_Email", each if [Email] = null then [test2_Email] else [Email]),
    NewTest1 = Table.RemoveColumns(AddedColumn, {"Email", "test2_Email"}),
    MergedTables2 = Table.NestedJoin(test2, {"Employee_Id"}, test1, {"Employee_Id"}, "test1", JoinKind.LeftOuter),
    ExpandedTable2 = Table.ExpandTableColumn(MergedTables2, "test1", {"Email"}, {"test1_Email"}),
    AddedColumn2 = Table.AddColumn(ExpandedTable2, "Filled_Email", each if [Email] = null then [test1_Email] else [Email]),
    NewTest2 = Table.RemoveColumns(AddedColumn2, {"Email", "test1_Email"}),
    JoinTables=Table.NestedJoin(NewTest1,{"Filled_Email"},NewTest2,{"Filled_Email"},"NewTest2",JoinKind.FullOuter),
    #"Expanded NewTest2" = Table.ExpandTableColumn(JoinTables, "NewTest2", {"Grade", "Expertise", "Qualification"}, {"Grade", "Expertise", "Qualification"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded NewTest2",{{"Filled_Email", "Email"}}),
    #"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"Employee_Id", Order.Ascending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Employee_Id", "Email", "Department", "City", "Manager", "Grade", "Expertise", "Qualification"})
in
    #"Reordered Columns"

vbofengmsft_0-1732084931376.png

 

 

Best Regards,

Bof

@Anonymous,

Thanks for your help. Can you please modify the above code to get the same result as shown in the final result table? Please show the duplicate columns and I want to see the gaps to highlight them in the reports.

 

 

Note:

Anonymous
Not applicable

Hi @emohammad ,

 

Please try:

let
    test1 = #table(
        {"Employee_Id", "Email", "Department", "City", "Manager"},
        {
            {22221, "[email protected]", "Dept1", "City1", "Manager1"},
            {22222, "[email protected]", "Dept1", "City1", "Manager1"},
            {22223, null, "Dept1", "City2", "Manager1"},
            {22224, "[email protected]", "Dept2", "City2", "Manager2"},
            {22225, "[email protected]", "Dept2", "City2", "Manager2"},
            {22226, "[email protected]", "Dept2", "City3", "Manager3"},
            {22227, "[email protected]", "Dept2", "City3", "Manager3"}
        }
    ),
    test2 = #table(
        {"Employee_Id", "Email", "Grade", "Expertise", "Qualification"},
        {
            {22221, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {null, "[email protected]", "Grade1", "Expertise1", "Qualification1"},
            {22223, "[email protected]", "Grade1", "Expertise2", "Qualification1"},
            {22224, null, "Grade2", "Expertise2", "Qualification2"},
            {22225, "[email protected]", "Grade2", "Expertise3", "Qualification3"},
            {22226, "[email protected]", "Grade3", "Expertise3", "Qualification3"},
            {22227, "[email protected]", "Grade3", "Expertise4", "Qualification3"}
        }
    ),
    MergedTables = Table.NestedJoin(test1, {"Employee_Id"}, test2, {"Employee_Id"}, "test2", JoinKind.LeftOuter),
    ExpandedTable = Table.ExpandTableColumn(MergedTables, "test2", {"Email"}, {"test2_Email"}),
    AddedColumn = Table.AddColumn(ExpandedTable, "Table1ID", each if [Email] = null then [test2_Email] else [Email]),
    MergedTables2 = Table.NestedJoin(test2, {"Employee_Id"}, test1, {"Employee_Id"}, "test1", JoinKind.LeftOuter),
    ExpandedTable2 = Table.ExpandTableColumn(MergedTables2, "test1", {"Email"}, {"test1_Email"}),
    AddedColumn2 = Table.AddColumn(ExpandedTable2, "Table2ID", each if [Email] = null then [test1_Email] else [Email]),
    JoinTables=Table.NestedJoin(AddedColumn,{"Table1ID"},AddedColumn2,{"Table2ID"},"NewTest2",JoinKind.FullOuter),
    #"Expanded NewTest2" = Table.ExpandTableColumn(JoinTables, "NewTest2", {"Employee_Id","Email","Grade", "Expertise", "Qualification"}, {"Test2.Employee_Id","Test2.Email","Grade", "Expertise", "Qualification"}),
    RemoveColumns = Table.RemoveColumns(#"Expanded NewTest2",{"test2_Email","Table1ID"}),
    #"Sorted Rows" = Table.Sort(RemoveColumns,{{"Employee_Id", Order.Ascending}})
in
    #"Sorted Rows"

vbofengmsft_0-1732243603170.png

Best Regards,

Bof

Thanks, @v-bofeng-msft. This worked.

If thare are nulls in the test1.Employee_Id, I need to add a step to remove duplicates. 

Omid_Motamedise
Super User
Super User

Just apply two seperate merges for each column and then combine the results


If my answer helped solve your issue, please consider marking it as the accepted solution.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.