Yes. The revised code below. Note that Item column is still required in this version.
Power Query:
let
Master = Table.PromoteHeaders(
Excel.Workbook(
File.Contents("C:\SomeFolder\Master.xlsx"), null, true
){[Item="Sheet1",Kind="Sheet"]}[Data]
),
Source = Folder.Files("C:\SomeFolder\Data"),
Files = Table.AddColumn(Source, "Data",
each Excel.Workbook([Content], true, true){[Item="Sheet1",Kind="Sheet"]}[Data]
),
Tables = Table.SelectColumns(Files,{"Data"}),
WithMaster = List.Combine({{Master}, Tables[Data]}),
Accumulate = List.Accumulate(
WithMaster,
[t = null, i = 0],
(state, current) => [
t = if state[i] = 0
then current
else
let
FileName = "File" & Text.From(state[i]),
Nest = Table.NestedJoin(state[t], {"Item"}, current, {"Item"}, FileName, JoinKind.LeftOuter),
Columns = Table.ColumnNames(current),
Result = Table.ExpandTableColumn(Nest, FileName, List.LastN(Columns, List.Count(Columns) - 1))
in
Result,
i = state[i] + 1
]
),
Result = Accumulate[t]
in
Result
You'll notice that I modified the let expression inside the List.Accumulator() function. The way I changed it is assuming all column names are unique. However, if for some reason, there are duplicate names then the new query will fail. In that case, the following could be also used. Basically, we create OldColumns and NewColumns lists dynamically. The NewColumns list is generated by adding the unique suffix to the OldColumns list values.
Power Query:
let
FileName = "File" & Text.From(state[i]),
Nest = Table.NestedJoin(state[t], {"Item"}, current, {"Item"}, FileName, JoinKind.LeftOuter),
Columns = Table.ColumnNames(current),
OldColumns = List.LastN(Columns, List.Count(Columns) - 1),
NewColumns = List.Transform(OldColumns, each _ & "-" & FileName),
Result = Table.ExpandTableColumn(Nest, FileName, OldColumns, NewColumns)
in
Result,
Note: The new versions work with any number of columns after the Item column in the data files.
Thank you, this is new territory for me so I am making this up as I go along. Some issues I hope you can help me with
I amended the formula a bit.
1) The master file headers aren't in the 1st row. it's in the second. So I added a step to promote the 1st row to header, don't know if this can be resolved by removing one row first and then start with Table.PromoteHeaders?
2) The source files that is in the folder. The headers are in the 4th row, but I need the header to concatenate the first three rows into the 4th row then use the 4th row as header. Dont know if this is even possible?
3)Column C in the source files have all the same header, I used the second part of your code from OldColumn to NewColumns, but it does not add the file name. It just says "file 1 and file 2"
This is the file names
4) Last point, it seems that some lines are duplicated. I can fix it by removing duplicates, but it seems that the duplicated line has its columns moved up. The division should only have one letter in it, not numbers, that number in division column is the vendor number. So as a fix i can just filter out the numbers, but can the code be amended to avoid this? I have double checked the source files does not have the numbers in the division, the source files does not have a column "division"
Sorry for all the questions. I believe when this is done and fixed I will have what I need to make it work.
Thank you for all you assistance Sir.
let
Master = Table.PromoteHeaders(
Excel.Workbook(
File.Contents("C:\Basjan\Reports\Plano vs Listing\Master\Plano vs Listing Master.xlsx"), null, true
){[Item="Sheet1",Kind="Sheet"]}[Data]
),
#"Promoted Headers" = Table.PromoteHeaders(Master, [PromoteAllScalars=true]),
Source = Folder.Files("C:\Basjan\Reports\Plano vs Listing\Abi"),
Files = Table.AddColumn(Source, "Data",
each Excel.Workbook([Content], true, true){[Item="Sheet1",Kind="Sheet"]}[Data]
),
Tables = Table.SelectColumns(Files,{"Data"}),
WithMaster = List.Combine({{#"Promoted Headers"}, Tables[Data]}),
Accumulate = List.Accumulate(
WithMaster,
[t = null, i = 0],
(state, current) => [
t = if state
= 0
then current
else
let
FileName = "File" & Text.From(state),
Nest = Table.NestedJoin(state[t], {"6 Article"}, current, {"6 Article"}, FileName, JoinKind.LeftOuter),
Columns = Table.ColumnNames(current),
OldColumns = List.LastN(Columns, List.Count(Columns) - 2),
NewColumns = List.Transform(OldColumns, each _ & "-" & FileName),
Result = Table.ExpandTableColumn(Nest, FileName, OldColumns, NewColumns)
in
Result,
i = state + 1
]
),
Result = Accumulate[t]
in
Result