Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago

Mergin a single column in table X with multiple columns table Y - Need help with M/SQL code

Hi all, 

 

I have two tables, X and Y.

 

Table X contains four different levels of code. 1 digit codes, 2 digit codes, 3 digit codes and 4 digit codes. (levels of company hierarchy) looks like this:

 

CodeName
1551LOL
12CAT
123DOG
1568RABBIT
10HORSE 
156DONKEY

 

Table Y contains the code from table X, but has no names. I need to merge the two tables together to bring the names into table Y. 

 

level 1level 2level 3level 4
2231531008
7252132568
4846898548
8999879875
9784584153

 

I could repeat using the merge function, this works, but I feel like it might not be very inneficient?

 

My source is SQL Database, and I use an import connection. Schould I do this in SQL, tailormade "M" code, or by using the "Merge Queries" button several times? 

 

What is most efficient and what would the SQL or M code look like? 

 

Thank you!

 

Br,

 

Morten

 

 

7 Replies

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

    Anonymous 

     

    Seems you can unpivot the levels in TableY and then merge with TableX.

    Then pivot back the levels

  • TeigeGao's avatar
    TeigeGao
    Icon for Solution Sage rankSolution Sage

    Hi Anonymous ,

    Could you please share us your expected result?

    Best Regards,

    Teige

    • Anonymous's avatar
      Anonymous
      Not applicable

      Just to clarify what I want: 

       

      I have table X and Y

       

       

      CodeName
      1551LOL
      12CAT
      123DOG
      1568RABBIT
      10HORSE 
      156DONKEY
      2SHARK
      7EAGLE
      4LEOPARD
      84SQUID
      689LADY BUG
      1551CHICKEN

      Y

      level 1level 2level 3level 4
      2121561568
      7101232568
      4846891551

      And I want table Z

      level 1Level 1 Namelevel 2Level 2 Namelevel 3Level 3 Namelevel 4Level 4 Name
      2SHARK12CAT156DONKEY1568RABBIT
      7EAGLE10HORSE123DOG2568CHICKEN
      4LEOPARD84SQUID689LADY BUG1551LOL

       

      I have found that I can use the merge function several times but I suspect that is not the best for performance but maybe I'm wrong? Do other people have suggestions for a more elegant solution? :)

      • Anonymous's avatar
        Anonymous
        Not applicable

        Try this Power Query

         

        let
        
        //Get X Table
        XTable = let
            Source = Sql.Database(<sqlserver>, "Sandbox"),
            dbo_X = Source{[Schema="dbo",Item="X"]}[Data]
        in
            dbo_X,
        
        //Get Y Table
        YTable = let
            Source = Sql.Database(<sqlserver>, "Sandbox"),
            dbo_Y = Source{[Schema="dbo",Item="Y"]}[Data]
        in
            dbo_Y,
        
        
        //Function for getting the name
        getName = (SearchCode as text, Masterlist as table) as text => 
        List.First(
                Table.Column(Table.SelectRows(XTable,each Record.Field(_,"Code")=SearchCode),"Name")
            ),
        
        Result = YTable,
        AddLevel1Names = Table.AddColumn(Result,"Level 1 Name", each getName([level 1],XTable)),
        AddLevel2Names = Table.AddColumn(AddLevel1Names,"Level 2 Name", each getName([level 2],XTable)),
        AddLevel3Names = Table.AddColumn(AddLevel2Names,"Level 3 Name", each getName([level 3],XTable)),
        AddLevel4Names = Table.AddColumn(AddLevel3Names,"Level 4 Name", each getName([level 4],XTable)),
        RearrangeColumns = Table.ReorderColumns(AddLevel4Names,{"level 1","Level 1 Name","level 2","Level 2 Name","level 3","Level 3 Name","level 4","Level 4 Name"})
        in
            RearrangeColumns

        Replace <sqlserver> with your server names.

  • Anonymous's avatar
    Anonymous
    Not applicable

    None of the codes in Table Y is there in Table X. Why?