A continuation of my question here: Excel, query: How to add specific info from query 2 into query 1 with lookup
I have 2 queries, "Overview" and "Data". And I want to add the information from "Data" in the "Overview" query on the correct place. The information of columns ITEM, ITEM 2 and Y/N needs to be added to the correct row, determined by the value in NAME.1 and NAME.2.
These are the two queries. (Slightly different from my last question)
Overview
| NAME.1 | NAME.2 | NUMBER | RANDOM |
|---|---|---|---|
| Name11 | 243324 | qwsa | |
| Name22 | 6747 | dsfsdf | |
| Name13 | 455 | yyu | |
| Name14 | 908098 | hfhn | |
| Name25 | 34 | ertew | |
| 132 | uil | ||
| Name17 | Name27 | 64 | tgvc |
| Name18 | Name28 | 876 | iorts |
Data
| ITEM | ITEM 2 | Y/N | NAME.1 | NAME.2 |
|---|---|---|---|---|
| 123123 | AA | Y | Name25 | |
| 234324 | BB | Y | Name11 | |
| 345345 | CC | N | Name17 | |
| 456456 | AA | Y | Name12 | Name22 |
| 567567 | AA | N | ||
| 678678 | DD | N | Name13 | Name23 |
| 789789 | DD | Y | Name16 | |
| 890890 | AA | N | Name14 |
I want to add information ITEM, ITEM 2 and Y/N from query "Data" to query "Overview" on the correct row. So, the first row of "Overview" will be:
| NAME.1 | NAME.2 | NUMBER | RANDOM | ITEM | ITEM 2 | Y/N |
|---|---|---|---|---|---|---|
| Name11 | 243324 | qwsa | 234324 | BB | Y |
The code I got from Ron in my previous question is:
let
Source = Excel.CurrentWorkbook(){[Name="Overview3"]}[Content],
#"Overview" = Table.TransformColumnTypes(Source,{
{"NAME.1", type text}, {"NAME.2", type text}, {"NUMBER", Int64.Type}, {"RANDOM", type text}}),
//Add index column to be able to go back to original table order
#"Added Index" = Table.AddIndexColumn(Overview, "Index", 0, 1, Int64.Type),
//Create a Name column that we will expand to all names in a single column
#"Added Custom" = Table.AddColumn(#"Added Index", "Name", each List.RemoveNulls({[NAME.1]} & {[NAME.2]})),
#"Expanded Name" = Table.ExpandListColumn(#"Added Custom", "Name"),
//Since Data table could also have Names in both NAME columns, we can unpivot (and delete the unneeded columns)
#"Unpivot Data" = Table.UnpivotOtherColumns(Table.RemoveColumns(Data3,"Y/N"),{"ITEM"},"Attribute","Name"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivot Data",{"Attribute"}),
//Now we can merge the tables and return the ITEM
#"Add Item" = Table.TransformColumns(
Table.NestedJoin(#"Expanded Name","Name",#"Removed Columns","Name","ITEM",JoinKind.LeftOuter),
{"ITEM", each try [ITEM]{0} otherwise null, type text}),
#"Removed Columns1" = Table.RemoveColumns(#"Add Item",{"Name"}),
//Group by Index to return initial table in order
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Index"}, {
//If two names in the same row of Overview have different ITEMs, we will returned a concatenated string
{"All", each Table.Distinct(Table.TransformColumns(_, {"ITEM", (c)=>Text.Combine(_[ITEM], ", ")})) ,
type table [NAME.1=nullable text, NAME.2=nullable text, NUMBER=nullable number, RANDOM=nullable text, Index=number, ITEM=text]}}),
#"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"Index"}),
#"Expanded All" = Table.ExpandTableColumn(#"Removed Columns2", "All", {"NAME.1", "NAME.2", "NUMBER", "RANDOM", "Index", "ITEM"}),
#"Removed Columns3" = Table.RemoveColumns(#"Expanded All",{"Index"})
in
#"Removed Columns3"
That worked for that question. However, I can't get it working when I try to add more columns in an efficient/clean way. (add all the columns at once instead of repeating the code 3 times. Repeating the code 3 times doesn't work so far, but I think I made a minor mistake somewhere. However, I think it should be possible to do it directly for 3 columns at once. In my actual file where I want to use it, I need to add multiple columns. So a more efficient method would be much better. Both for usage and for other people when someone wants to use/edit my file)
Additional information for reply on comment Ron Rosenfield:
When I use this code in my actual file, it works till #"Expanded Fields". However, it gives an error at #"Grouped Rows". It says: Expression.SyntaxError: Invalid identifier. And it points to [Sub-supplier]
#"Expanded Fields" = Table.ExpandRecordColumn(#"Removed Columns1", "Fields", {"Report Number", "Sub-supplier", "text text text text.", "text text text", "text text text text text", "text text"}),
#"Grouped Rows" = Table.Group(#"Expanded Fields", {"Index"}, {{"all",
each Table.TransformColumns(_, {
{"Report Number", (c)=>Text.Combine(_[Report Number], ", ")},
{"Sub-supplier",(c)=>Text.Combine(_[Sub-supplier], ", ")},
{"text text text text.",(c)=>Text.Combine(_[text text text text.], ", ")},
{"text text text",(c)=>Text.Combine(_[text text text], ", ")},
{"text text text text text",(c)=>Text.Combine(_[text text text text text], ", ")},
{"text text", (c)=>Text.Combine(_[#"text text"], ", ")}}
){0}}}),



#"Expanded All"step you can see a list of all the column names to be expanded{"NAME.1", "NAME.2", "NUMBER", "RANDOM", "Index", "ITEM"}. Just add your additional columns to the list. Or, like the other answer in your previous question, use the Power Query UI to expand the columns instead of copying pasting code.Overviewhas entries in bothNAME.1andNAME.2, butDataonly has an entry for one of them, you won't know which name it is matching. Is that an issue?#"Expanded All"