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.
I want to join records in Table A with records in Table B, based on conditions in Table B. How can this be done the easiest way, probably with a SelectRows statement?
Contents of Table A:
ID | Name | Coating | Usage |
10 | A | F-CT | Commercial |
11 | A | F-CT | Basic Seed |
12 | A | Commercial | |
13 | A | Basic Seed | |
14 | A | F+I-CT | Commercial |
15 | A | F+I-CT | Basic Seed |
16 | B | Commercial |
Contents of Table B:
Name | CoatingCondition | UsageCondition | Outcome |
A | O-1 | ||
A | = "F+I-CT" | O-2 | |
A | = "Basic Seed" | O-3 | |
A | <> "Basic Seed" | O-4 | |
B | O-5 |
The match must be on the Name and on the Condition-columns. Note: an empty condition means that condition is not relevant. So for example the first record in Table B will match with all records in Table A where Name = "A".
The expected results are the following:
ID | Name | Outcome |
10 | A | O-1 |
10 | A | O-4 |
11 | A | O-1 |
11 | A | O-3 |
12 | A | O-1 |
12 | A | O-4 |
13 | A | O-1 |
13 | A | O-3 |
14 | A | O-1 |
14 | A | O-2 |
14 | A | O-4 |
15 | A | O-1 |
15 | A | O-2 |
15 | A | O-3 |
16 | B | O-5 |
This is the basic code that I think I have to extend (based on PowerQuery(M)agic: Conditional Joins using Table.SelectRows() - P3 (powerpivotpro.com):
let
TableA = Excel.CurrentWorkbook(){[Name="tblRoute"]}[Content],
TableB = Excel.CurrentWorkbook(){[Name="tblBAAM"]}[Content],
TableA_TableB = Table.AddColumn(TableA, "Outcome", each Table.SelectRows(TableB, (Magic) => [RouteName]=Magic[RouteName])),
Result = Table.ExpandTableColumn(TableA_TableB, "Outcome", {"Outcome"})
in
Result
Solved! Go to Solution.
Hi @JVos
Try this
let
TableA = Excel.CurrentWorkbook(){[Name="tblRoute"]}[Content],
TableB = Excel.CurrentWorkbook(){[Name="tblBAAM"]}[Content],
TableA_TableB = Table.AddColumn(TableA, "Outcome", each Table.SelectRows(TableB,
(Magic) => Magic[Name]=[Name] and (if Magic[CoatingCondition] = null then true else Expression.Evaluate("[Coating]" & Magic[CoatingCondition], [_=_]))
and (if Magic[UsageCondition] = null then true else Expression.Evaluate("[Usage]" & Magic[UsageCondition], [_=_])))
),
Result = Table.ExpandTableColumn(TableA_TableB, "Outcome", {"Outcome"}),
#"Removed Columns" = Table.RemoveColumns(Result,{"Coating", "Usage"})
in
#"Removed Columns"
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
Hi @JVos
Try this
let
TableA = Excel.CurrentWorkbook(){[Name="tblRoute"]}[Content],
TableB = Excel.CurrentWorkbook(){[Name="tblBAAM"]}[Content],
TableA_TableB = Table.AddColumn(TableA, "Outcome", each Table.SelectRows(TableB,
(Magic) => Magic[Name]=[Name] and (if Magic[CoatingCondition] = null then true else Expression.Evaluate("[Coating]" & Magic[CoatingCondition], [_=_]))
and (if Magic[UsageCondition] = null then true else Expression.Evaluate("[Usage]" & Magic[UsageCondition], [_=_])))
),
Result = Table.ExpandTableColumn(TableA_TableB, "Outcome", {"Outcome"}),
#"Removed Columns" = Table.RemoveColumns(Result,{"Coating", "Usage"})
in
#"Removed Columns"
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
@AlB Great! Wonderful! With some minor changes in column names, it worked! Now I'm going to try to understand it...
JVos
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.