Forum Discussion
Need help joining X# tables from files with dynamic columns using FilingID column
- 1 year ago
Hi jrrs
Try this
let Source = SharePoint.Files("https://_.sharepoint.com", [ApiVersion = 15]), FilteredRows = Table.SelectRows(Source, each [Folder Path] = "https://_.sharepoint.com/sites/dev/ODC/MayRaw/Filing_Data_20250501_202505311/"), FilteredHiddenFiles = Table.SelectRows(FilteredRows, each [Attributes]?[Hidden]? <> true), InvokedTransform = Table.AddColumn(FilteredHiddenFiles, "Transform File", each #"Transform File"([Content])), RenamedColumns = Table.RenameColumns(InvokedTransform, {"Name", "Source.Name"}), RemovedOtherColumns = Table.SelectColumns(RenamedColumns, {"Source.Name", "Transform File"}), // Extract the list of tables TableList = RemovedOtherColumns[Transform File], // Merge all tables on FilingID using List.Accumulate MergedTable = List.Accumulate( TableList, TableList{0}, // Start with the first table (state, current) => Table.Join(state, "FilingID", current, "FilingID", JoinKind.FullOuter) ) in MergedTableI hope this helps, please give a thumbs up and mark as solved if it does, thanks!
- 1 year ago
Hi jrrs
I've recreated the issue with a list of tables. You can replace these with the end result of your other query but for testing this might be helpful:let
Table1 = Table.FromRows( //Generate fake data 1
{
{1, "afa", "UK"},
{2, "abc", "da"}
},
{"FilingID", "StuffA", "StuffB"}
),Table2 = Table.FromRows(//Generate fake data 2
{
{1, "x", "y"},
{3, "z", "w"}
},
{"FilingID", "StuffE", "StuffF"}
),
Table3 = Table.FromRows( //Generate fake data 3
{
{2, "x", "y"},
{1, "z", "w"}
},
{"FilingID", "StuffC", "StuffD"}
),
AllTables = {Table1, Table2, Table3} //Combine fake tables, replace this with the output of your Folder join
,FirstTable = List.First(AllTables), //freturn the first table everything else will join to
RemainingTables = List.Skip(AllTables, 1), //Ignore the first row of the table
IndexedTables = List.Zip({RemainingTables, List.Numbers(1, List.Count(RemainingTables))}), //Index the column names using a count
CombinedTable = List.Accumulate( //loop over the tables running a function on each
IndexedTables, //see above
FirstTable, //see above
(state, pair) => //two parameters to pass in
let
nextTable = pair{0}, //return name
index = pair{1}, //return the index number
renamedTable = Table.RenameColumns(nextTable, {"FilingID", "FilingID" & Text.From(index)}) //rename the column so no clash when merging (Filing1, Filing2 etc)
in
Table.Join(state, "FilingID", renamedTable, "FilingID" & Text.From(index), JoinKind.FullOuter) //Full outer join so every column frm every table returned
),
//optionally loop through and create a combined filing index column
//if every ID is represented in every file this is no nedded
FilingIDColumns = List.Select(Table.ColumnNames(CombinedTable), each Text.Contains(_, "FilingID")),// Use the above list of names to add a new column, remove the empty rows
AddCombinedColumn = Table.AddColumn(CombinedTable, "CombinedFilingID", each
List.RemoveNulls(List.Transform(FilingIDColumns, (col) => Record.Field(_, col)))
),// Take the list of column names and return a single column
FlattenedColumn = Table.TransformColumns(AddCombinedColumn, {"CombinedFilingID", each if List.Count(_) > 0 then _{0} else null})//Use the previously created list of names to delete the old columns
, DeleteColumns = Table.RemoveColumns(FlattenedColumn, FilingIDColumns),
//reorder to put the CombinedFilingID at the start but first remove the CombinedFilingID column then add it to the start
#"Reordered Columns" = Table.ReorderColumns(DeleteColumns, List.Combine({{"CombinedFilingID"}, List.RemoveItems(Table.ColumnNames( DeleteColumns),{"CombinedFilingID"})}))
in
#"Reordered Columns"
Hi jrrs
We can add a line to ensure FilingID isn't duplicated as your error points to this. Strange I know, but let's force the code to figure it out rather than checking through files to find the problem! If we add this before list.accumulate, we shouldn't bump into the value=3 error, but if we do, you'll need to change the type of your problem column to allow numbers and letters.
This is the line of code we will add, it will ensure all tables have only one FilingID column:
TableList = List.Transform(RemovedOtherColumns[Transform File], each Table.Distinct(_, {"FilingID"}))
When added to your code before list.accumulate, it will look like this:
let
Source = SharePoint.Files("https://_.sharepoint.com", [ApiVersion = 15]),
FilteredRows = Table.SelectRows(Source, each [Folder Path] = "https://_.sharepoint.com/sites/dev/ODC/MayRaw/Filing_Data_20250501_202505311/"),
FilteredHiddenFiles = Table.SelectRows(FilteredRows, each [Attributes]?[Hidden]? <> true),
InvokedTransform = Table.AddColumn(FilteredHiddenFiles, "Transform File", each #"Transform File"([Content])),
RenamedColumns = Table.RenameColumns(InvokedTransform, {"Name", "Source.Name"}),
RemovedOtherColumns = Table.SelectColumns(RenamedColumns, {"Source.Name", "Transform File"}),
// Extract and deduplicate each table in the list
TableList = List.Transform(RemovedOtherColumns[Transform File], each Table.Distinct(_, {"FilingID"})),
// Merge all tables on FilingID using List.Accumulate
MergedTable = List.Accumulate(
TableList,
TableList{0}, // Start with the first table
(state, current) => Table.Join(state, "FilingID", current, "FilingID", JoinKind.FullOuter)
)
in
MergedTable
Hope it works this time!
Thank you, wardy, but I am still receiving errors about duplicate column names even after your suggested changes. In samwise's suggestion, he had hardcoded tables and the result table was exactly what I am looking for, but as soon as I try to use it with the csv tables, I start getting the same duplicate column errors.
I'm not sure what to do to troubleshoot. Maybe it's in the custom function?
- jrrs1 year agoFrequent Visitor
Actually, I think that's exactly it. Right after I typed the previous response I went to check. I thought the function was the most basic of basic function to grab the table and return it, but the editor added a set column number to thqat function and the column count is supposed to b dynamic, so once I deleted that it stopped giving the duplicate column names error.
I believe the code is working correctly now, thank you for your help!