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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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 7
Anonymous
Not applicable

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

TeigeGao
Solution Sage
Solution Sage

Hi @Anonymous ,

Could you please share us your expected result?

Best Regards,

Teige

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
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
Not applicable

Thank you! Will try it out as soon as possible and then I'll get back to you asap

Anonymous
Not applicable

Actually, you could have imported X and Y tables and add the following calculated columns to your Y table. It is much simpler.

 

Level 1 Name = LOOKUPVALUE(X[Name],X[Code],Y[level 1])
Level 2 Name = LOOKUPVALUE(X[Name],X[Code],Y[level 2])
Level 3 Name = LOOKUPVALUE(X[Name],X[Code],Y[level 3])
Level 4 Name = LOOKUPVALUE(X[Name],X[Code],Y[level 4])
Zubair_Muhammad
Community Champion
Community Champion

@Anonymous 

 

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

Then pivot back the levels


Regards
Zubair

Please try my custom visuals

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.