Forum Discussion
Create a merge column by comparing 3 conditions to a 2nd table (Excel IFS/INDEX(MATCH) to PQ)
- 1 year ago
I took a stab at this with the below dummy data. The approach is to merge on the columns you are interested in, then, rather than expand on the nested joins, perform a check against the merge column and perhaps pull out the related ID. When multiple rows are joined, we just take the first row and ignore any others, in line with MATCH's behavior (with 0 in last arg) in Excel.
TracerTbl
Email Address UserID [email protected] 1 [email protected] 2 [email protected] null [email protected] 4 [email protected] null [email protected] 99 PSTable
ID Email 1 Email 2 1 [email protected] [email protected] 2 null [email protected] 3 [email protected] [email protected] 4 [email protected] null 5 [email protected] [email protected] 6 null [email protected] Power query (M) in advanced editor:
let <...> #"Removed Columns1" = <...>, MergeOnID = Table.NestedJoin( #"Removed Columns1", {"UserID"}, PSTable, {"ID"}, "ID_Check", JoinKind.LeftOuter ), MergeOnEmail1 = Table.NestedJoin( MergeOnID, {"Email Address"}, PSTable, {"Email 1"}, "Email1_Check", JoinKind.LeftOuter ), MergeOnEmail2 = Table.NestedJoin( MergeOnEmail1, {"Email Address"}, PSTable, {"Email 2"}, "Email2_Check", JoinKind.LeftOuter ), //For each merged column, perform a check calculation MergeCheckTransforms = Table.TransformColumns( MergeOnEmail2, { //Gives true/false on whether TracerTbl[ID] was found in PSTable {"ID_Check", each not Table.IsEmpty(_), type logical}, //If no matches found on Email1, return null, otherwise return the ID of first row matched {"Email1_Check", each if Table.IsEmpty(_) then null else Table.First(_)[ID], Int64.Type}, //If no matches found on Email2, return null, otherwise return the ID of first row matched {"Email2_Check", each if Table.IsEmpty(_) then null else Table.First(_)[ID], Int64.Type} } ), //Assign validated ID based on matches, with precedent order of: ID, Email1, Email2. //If no matches at all, return null AddValidatedId = Table.AddColumn( MergeCheckTransforms, "Validated ID", each if [ID_Check] then [UserID] else if [Email1_Check] <> null then [Email1_Check] else if [Email2_Check] <> null then [Email2_Check] else null, Int64.Type ) in AddValidatedIdHere is the output. You can remove the Check columns or perform whatever other transforms as desired.
Email Address UserID ID_Check Email1_Check Email2_Check Validated ID [email protected] 1 TRUE null null 1 [email protected] 2 TRUE null null 2 [email protected] null FALSE 3 6 3 [email protected] 4 TRUE 4 null 4 [email protected] null FALSE null 5 5 [email protected] 99 FALSE null null null Edit: Including screenshot of output as it's a little more readable:
I haven't used PBI before, but I only have limited access to it. I know I don't have access to DAX. Once I finished this project, I was going to start exploring what I can do in PBI. What I am currently working on will combine 5 reports to create a CSV to import to a different program, so I didn't think PBI was the best avenue, but then again, I only know it for creating dashboards. 😕
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.
Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523