+ Reply to Thread
Results 1 to 5 of 5

Merge 2 tables without creating duplicates with Power Query

  1. #1
    Forum Contributor
    Join Date
    12-03-2020
    Location
    australia
    MS-Off Ver
    365
    Posts
    112

    Merge 2 tables without creating duplicates with Power Query

    Hi,

    I have two tables (table 1 and table 2). I would like to use table 1 and merge it with table 2.

    Table 1 and 2 have Emp Id in common.

    I would like to use Power Query to use the Emp Id from table 1 and extract only the relevant information from table 2 that links to Emp Id from table.

    I've tried to merge using inner join and left join but I keep on getting duplicates.

    I've attached my workbook with the desired results in yellow.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Merge 2 tables without creating duplicates with Power Query

    If I understand correct, you want to Left Outer join Table 1 and Table 2 on both the Emp Id and Dept Columns.

    To do this, hold the control key and click 'Emp ID' in Table 1, 'Emp ID' in Table 2, 'Dept' in Table 1 then 'Dept' in Table 2. You should then see small numbers appearing at on the column headers to show what is being matched to what.

    Remember to expand the 'Table2' columns in Power Query editor to show attributes 4,5 and 6.
    <<< If you have valued anyone's contributions in this thread, please click * to thank them for their efforts

  3. #3
    Forum Contributor
    Join Date
    12-03-2020
    Location
    australia
    MS-Off Ver
    365
    Posts
    112

    Re: Merge 2 tables without creating duplicates with Power Query

    Is it possible to do it just with the Emp ID? Also I did it with my original data set, it still duplicates the rows.
    Last edited by bob112233; 04-05-2022 at 11:08 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Merge 2 tables without creating duplicates with Power Query

    If you do it with just the EmpID, it will duplicate unless they are matched on a unique basis between the tables. For example, just EmpID 1 will match all the following rows when you expand to show attributes 4,5 and 6:

    1 - Finance > 1 - Finance
    1 - Finance > 1 - Data
    1 - Finance > 1 - Tax
    1 - Finance > 1 - Legal
    1 - Data > 1 - Finance
    1 - Data > 1 - Data
    1 - Data > 1 - Tax
    1 - Data > 1 - Legal
    1 - Tax > 1 - Finance
    1 - Tax > 1 - Data
    1 - Tax > 1 - Tax
    1 - Tax > 1 - Legal

    From your example, it looks like you are expecting only the following matches though

    1 - Finance > 1 - Finance
    1 - Data > 1 - Data
    1 - Tax > 1 - Tax

    In which case, you need to match on two columns to make them match on a unique basis. Using my approach on your sample data it matched your expected output exactly. Perhaps there are other differences in your original dataset?

  5. #5
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,213

    Re: Merge 2 tables without creating duplicates with Power Query

    Please try

    LeftOuter

    PHP Code: 
    let
        Source 
    Table.NestedJoin(Table1, {"Emp Id""Dept"},Table2, {"Emp Id""Dept"}, "T"JoinKind.LeftOuter),
        
    Expanded Table.ExpandTableColumn(Source"T",  List.Skip(List.Union(List.Transform(Source[T],Table.ColumnNames)),2))
    in
        Expanded 
    or FullOuter

    PHP Code: 
    let
        Source 
    Table.Combine({Table1,Table2}),
        
    Unpivoted Table.UnpivotOtherColumns(Source, {"Emp Id""Dept"}, "Attribute""Value"),
        
    Pivoted Table.Pivot(Unpivoted, List.Distinct(Unpivoted[Attribute]), "Attribute""Value", List.Sum)
    in
        Pivoted 
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Merge Pro tip in Power Query
    By hudson andrew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2021, 03:53 AM
  2. [SOLVED] Power Query - Append and merge tables
    By DystopiaRelic in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-13-2021, 02:42 PM
  3. Replies: 10
    Last Post: 06-25-2020, 12:19 AM
  4. Power query merge (many to many relationship) and remove duplicates
    By richardbreakey in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-14-2020, 06:07 PM
  5. new entry in merge power query
    By rosscortb in forum Excel General
    Replies: 1
    Last Post: 04-24-2020, 05:02 AM
  6. Replies: 31
    Last Post: 02-14-2020, 09:13 AM
  7. Power Query: Updating a query based on changing tables
    By cheesehead101 in forum Excel General
    Replies: 1
    Last Post: 12-16-2019, 06:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1