Forum Discussion

Jakez's avatar
Jakez
New Member
3 years ago
Solved

How to join two tables which are non unique

Hi All,

 

I have two tables as shown below and i want to merge them together to do some analysis in Powerbi, original source of the data is excel files. I want to join the table using two columns Dealer_id and Date.  

Table 1

Dealer_idDateL_idCountAlt_c
234401/01/2023232345333
234402/01/2023324234224
234403/01/20232242453456
234404/01/2023444242243

 

Table 2

Dealer_idDate Sales
234401/01/2023 4500
234402/01/2023 3499
234403/01/2023 2133
234404/01/2023 2344

 

Desired Output

Dealer_idDateL_idCountAlt_cSales
234401/01/20232323453334500
234402/01/20233242342243499
234403/01/202322424534562133
234404/01/20234442422432344

 

What i  really need from  is to get the Sales from table 2 into table 1,  i get the desired output in powerquery but when i apply and close Power query and when the data loads i get the following error "Column Dealer_id in table1 contains a duplicate value '2344' and this is not allowed for columns on the one side of a many to one relationship or for columns that are useed as primary key of a table"

 

PLease advise how i can solve this.

Thanks

  • Jakez 

    1. Disable "Auto Detect Relationship" under file settings.
    2. Before loading data make sure there are no existing relationships.
    3. You can join 2 table by using DealerID and Date as the Key
    let
        Source = 
            Table.NestedJoin (
                Table1,
                { "Dealer_id", "Date" },
                Table2,
                { "Dealer_id", "Date" },
                "Table2",
                JoinKind.LeftOuter
            ),
        ExtractedSales = Table.ExpandTableColumn ( Source, "Table2", { " Sales" }, { " Sales" } )
    in
        ExtractedSales

     

3 Replies

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    Jakez 

    1. Disable "Auto Detect Relationship" under file settings.
    2. Before loading data make sure there are no existing relationships.
    3. You can join 2 table by using DealerID and Date as the Key
    let
        Source = 
            Table.NestedJoin (
                Table1,
                { "Dealer_id", "Date" },
                Table2,
                { "Dealer_id", "Date" },
                "Table2",
                JoinKind.LeftOuter
            ),
        ExtractedSales = Table.ExpandTableColumn ( Source, "Table2", { " Sales" }, { " Sales" } )
    in
        ExtractedSales

     

    • Dikshant's avatar
      Dikshant
      New Member

      Works fine, Thanks Antriksh!

      For the uninititated, you have to create a new Query to paste the above code in Advanced editor.

      Also, remove the space before {" Sales"} to avoid error.

  • I cannot reproduce your problem with the information you have provided. How, exactly, did you merge the tables in Power Query?

     

    I merely selected to get data from Excel; selected the file and then the two tables.

    In PQ, I selected to merge the two tables to a new query, using a full outer join, and then extracting the Sales column.

     

    If you did something different, please tell us what you did, and supply the M-Code from the Advanced Editor.