Forum Discussion
New calculated column using column from another table
Hello everyone,
I am trying to create a new column in my primary table, using some conditions from another table, as well as the primary:
Primary Table:
| ID | Injury | Transported Boolean |
| 1 | A | Transported |
| 2 | B | Not Transported |
| 3 | B | Transported |
| 4 | A | Not Transported |
| 5 | A | Transported |
| 6 | C | Not Transported |
Secondary Table:
| ID | Type |
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | f |
The conditions being:
If (secondary['Type'] = A, or C, or D, or E OR primary['Injury'] = A ) AND primary['Transported Boolean] = Transported
then the new column named primary['Claimable MVA?'] = True, else False
| ID | Injury | Transported Boolean | Claimable MVA? |
| 1 | A | Transported | True |
| 2 | B | Not Transported | False |
| 3 | B | Transported | True |
| 4 | A | Not Transported | False |
| 5 | A | Transported | True |
| 6 | C | Not Transported | False |
Would anyone know how to do this?
Thank you for your time
Hi Anonymous ,
You need to merge Table2 into Table1 so it looks like this:
Then add a new custom column with the below code:
if (List.Contains({"a".."e"},[Type]) or [Injury] = "A") and [Transported Boolean] = "Transported" then true else falseNote that Power Query is case sensitive, so even though in your example above you said "secondary['Type'] = A, or C, or D, or E" your secondary table had a, b, c, d, e, f. They aren't the same to PQ. You can change everything to upper/lowercase with a transformation before you do the merge. Just be aware, a=a, but A<>a.
Then when done, keep all but the temproary Type column. End result:
Total code for the first table, and it assumes your Table2 is exactly as you have it above.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIE4pCixLzigvyiktQUpVidaCUjoJgTEPvllyigyxlD5dDFTaBmYdNjisMeM6CYMzY9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Injury = _t, #"Transported Boolean" = _t]), #"Merged Queries" = Table.NestedJoin(Source, {"ID"}, Table2, {"ID"}, "Table2", JoinKind.LeftOuter), #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Type"}, {"Type"}), #"Added Claimable MVA" = Table.AddColumn(#"Expanded Table2", "Claimable MVA", each if (List.Contains({"a".."e"},[Type]) or [Injury] = "A") and [Transported Boolean] = "Transported" then true else false), #"Removed Other Columns" = Table.SelectColumns(#"Added Claimable MVA",{"ID", "Injury", "Transported Boolean", "Claimable MVA"}), #"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Claimable MVA", type logical}}) in #"Changed Type"How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
7 Replies
- edhansCommunity Champion
Hi Anonymous ,
You need to merge Table2 into Table1 so it looks like this:
Then add a new custom column with the below code:
if (List.Contains({"a".."e"},[Type]) or [Injury] = "A") and [Transported Boolean] = "Transported" then true else falseNote that Power Query is case sensitive, so even though in your example above you said "secondary['Type'] = A, or C, or D, or E" your secondary table had a, b, c, d, e, f. They aren't the same to PQ. You can change everything to upper/lowercase with a transformation before you do the merge. Just be aware, a=a, but A<>a.
Then when done, keep all but the temproary Type column. End result:
Total code for the first table, and it assumes your Table2 is exactly as you have it above.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIE4pCixLzigvyiktQUpVidaCUjoJgTEPvllyigyxlD5dDFTaBmYdNjisMeM6CYMzY9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Injury = _t, #"Transported Boolean" = _t]), #"Merged Queries" = Table.NestedJoin(Source, {"ID"}, Table2, {"ID"}, "Table2", JoinKind.LeftOuter), #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Type"}, {"Type"}), #"Added Claimable MVA" = Table.AddColumn(#"Expanded Table2", "Claimable MVA", each if (List.Contains({"a".."e"},[Type]) or [Injury] = "A") and [Transported Boolean] = "Transported" then true else false), #"Removed Other Columns" = Table.SelectColumns(#"Added Claimable MVA",{"ID", "Injury", "Transported Boolean", "Claimable MVA"}), #"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Claimable MVA", type logical}}) in #"Changed Type"How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.- AnonymousNot applicable
Hello edhans ,
Thank you for this! Both of my tables have many more columns than the ones I have listed here, is that ok?
If i only want to merge 'Type' from Table 2, this is enough?
Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Type"}, {"Type"})- edhansCommunity Champion
Yes, when you expand, you expand only the columns you need. You can have 100 columns in Table 2 and only bring in 1, 2, 50, or all 100 columns. Your choice. I recommend you read this article by MS on Merging in Power Query if you want to get a more detailed understanding, or you are unable to see what my code sample is doing. It is for Excel, but Excel and Power BI Power Query are the same when it comes to merges.