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.
Hi,
I have two tables that I'd like to merge:
Table 1: Complaints, employee ID
Table 2: Employee ID, Emp first name, Emp last name
I'd like to join table1 with a subquery of table2 with power query interface in PBI.
The reason I want to do it is first, get the details of employees who appear on the first table only (Table2 includes all the emp) and to get a table with distinct values (each emp will appear once) in order to reduce the size of the final table created from the merge.
My SQL query would look like this:
SELECT *
FROM Table1 Right outer join (SELECT DISTINCT EmpID FROM Table2)
On Table1 .EmpID = Table2 .EmpID
Thank you!
Solved! Go to Solution.
I don't see why not, it's M code, you can pretty much do whatever you want. See my technique here:
https://social.technet.microsoft.com/wiki/contents/articles/32915.power-bi-merge-query-with-m.aspx
Well, if you don't want to write M code to keep it all in one query, you could simply create a new reference query to Table1. Right click Table1 query and choose "Reference". Then in that query right-click the EmpID column and choose to remove duplicates. Then just use that table in your merge query. Or you could just write those steps in M and keep it all in a single query without creating the table, but however you want to do it. For example something like this would probably work:
= Table.NestedJoin(Table.Distinct(Table1,{"EmpID"}),{"EmpID"},Table2,{"EmpID"},"Table2",JoinKind.LeftOuter)
Have you looked at Merge queries?
Yes, I merged them but it doesn't allow you to merge a table to a subquery of another table.
I don't see why not, it's M code, you can pretty much do whatever you want. See my technique here:
https://social.technet.microsoft.com/wiki/contents/articles/32915.power-bi-merge-query-with-m.aspx
Thank you for your help.
I know it is possible, I just don't know how to include the Distinct part in the query.
This is the query I use now to join the tables:
= Table.NestedJoin(Table1,{"EmpID"},Table2,{"EmpID"},"Table2",JoinKind.LeftOuter)
How would you modify the query to include only Distinct EmpID from Table1?
Well, if you don't want to write M code to keep it all in one query, you could simply create a new reference query to Table1. Right click Table1 query and choose "Reference". Then in that query right-click the EmpID column and choose to remove duplicates. Then just use that table in your merge query. Or you could just write those steps in M and keep it all in a single query without creating the table, but however you want to do it. For example something like this would probably work:
= Table.NestedJoin(Table.Distinct(Table1,{"EmpID"}),{"EmpID"},Table2,{"EmpID"},"Table2",JoinKind.LeftOuter)
Hey Greg,
How can we join via nestedjoin on multiple condition but with or conditrion.
Example: Select A.id ,A.Name B.Name,B.Course from A, B where (A.id=B.id Or A.Name = B.Name).
I was trying to do it using Merge query , I am able to select multiple column when joining from but A and B but I think it is taking And operator.
Hi @Greg_Deckler,
It doesn't really work so I'll try to write the full M code using the article you attached.
Thank you!
could you provide a visual example on which we could work?
Did I answer your question correctly? Mark my answer as a solution!
Proud to be a Datanaut!
OK, @ImkeF might be able to help as well, she is an M guru.
I believe the the equivalent M-syntax to the SQL-syntax you've mentioned in your original post would be this:
= Table.NestedJoin(Table1,,{"EmpID"},Table.Distinct(Table2, {"EmpID"}),{"EmpID"},"Table2",JoinKind.LeftOuter)
So @Greg_Deckler syntax was correct, just that it referred to the wrong table, as far as I can see.
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Hi @LivioLanzo and @ImkeF, thanks for your help!
I want to join Distinct values from the first table:
EmpID | ComID |
101 | 1 |
102 | 2 |
103 | 3 |
101 | 4 |
With values from the second table:
EmpRefNumber | FirstName | LastName |
105 | Ben | B |
101 | Ryan | G |
102 | James | H |
200 | Rose | F |
and keep only values from the first table (Left join).
The join should be based on EmpID and EmpRefNumber
Thank you!
You can do it like this:
let Source = Table.NestedJoin( Table.Distinct( Table.SelectColumns( Table1, {"EmpID"} ) ), {"EmpID"}, Table2, {"EmpRefNumber"}, "JoinedTable", JoinKind.LeftOuter ), ExpandedJoinedTable = Table.ExpandTableColumn(Source, "JoinedTable", {"FirstName", "LastName"}, {"FirstName", "LastName"}) in ExpandedJoinedTable
Or in DAX:
Table = NATURALLEFTOUTERJOIN ( DISTINCT ( SELECTCOLUMNS ( Table1, "EmpID", [EmpID] + 0 ) ), SELECTCOLUMNS ( Table2, "EmpID", Table2[EmpRefNumber] + 0, "FirstName", table2[FirstName], "LastName", table2[LastName] ) )
Did I answer your question correctly? Mark my answer as a solution!
Proud to be a Datanaut!
Hi,
What result are you expecting?
Hi @Ashish_Mathur,
Table1.EmpID | Table2.EmpRefNumber | Table2.FirstName | Table2.LastName |
101 | 101 | Ryan | G |
102 | 102 | James | H |
103 | NULL | NULL | NULL |
With distinct values on Table1.EmpID column
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
108 | |
105 | |
88 | |
74 | |
66 |
User | Count |
---|---|
126 | |
111 | |
100 | |
83 | |
71 |