Forum Discussion
combine csv files with inconsistent columns and missing columns
- 1 year ago
Hi omrangassan, this will do the job, just change folder address in Source step.
Without CSV FileNames:
let Source = Folder.Files("c:\Users\YourUser\finbox\"), ColNames = List.Buffer(Text.Split("a263ff10 href,d6d4b13f,f397a0c8,b343055e href,b343055e,b343055e href (2),b343055e (2),b343055e href (3),b343055e (3),b343055e href (4),b343055e (4),b343055e href (5),b343055e (5),b343055e href (6),b343055e (6),b343055e href (7),b343055e (7),b343055e href (8),b343055e (8),b343055e href (9),b343055e (9),b343055e href (10),b343055e (10),b343055e href (11),b343055e (11),b343055e href (12),b343055e (12),b343055e href (13),b343055e (13),b343055e href (14),b343055e (14),b343055e href (15),b343055e (15),b343055e href (16),b343055e (16),b343055e href (17),b343055e (17),b343055e href (18),b343055e (18),b343055e href (19),b343055e (19),b343055e href (20),b343055e (20),b343055e href (21),b343055e (21),b343055e href (22),b343055e (22),b343055e href (23),b343055e (23),b343055e href (24),b343055e (24),b343055e href (25)", ",")), Combined = Table.Combine(Table.AddColumn(Source, "T", each Table.SelectColumns(Table.PromoteHeaders(Csv.Document([Content],[Delimiter=",", Columns=52, Encoding=1250, QuoteStyle=QuoteStyle.None])), ColNames, MissingField.UseNull))[T]) in CombinedIf you want to preserve CSV FileNames, use this query:
let Source = Folder.Files("c:\Users\YourUser\finbox\"), ColNames = List.Buffer(Text.Split("a263ff10 href,d6d4b13f,f397a0c8,b343055e href,b343055e,b343055e href (2),b343055e (2),b343055e href (3),b343055e (3),b343055e href (4),b343055e (4),b343055e href (5),b343055e (5),b343055e href (6),b343055e (6),b343055e href (7),b343055e (7),b343055e href (8),b343055e (8),b343055e href (9),b343055e (9),b343055e href (10),b343055e (10),b343055e href (11),b343055e (11),b343055e href (12),b343055e (12),b343055e href (13),b343055e (13),b343055e href (14),b343055e (14),b343055e href (15),b343055e (15),b343055e href (16),b343055e (16),b343055e href (17),b343055e (17),b343055e href (18),b343055e (18),b343055e href (19),b343055e (19),b343055e href (20),b343055e (20),b343055e href (21),b343055e (21),b343055e href (22),b343055e (22),b343055e href (23),b343055e (23),b343055e href (24),b343055e (24),b343055e href (25)", ",")), Combined = Table.Combine(Table.AddColumn(Source, "T", each Table.SelectColumns(Table.AddColumn(Table.PromoteHeaders(Csv.Document([Content],[Delimiter=",", Columns=52, Encoding=1250, QuoteStyle=QuoteStyle.None])), "Name", (x)=> [Name]), {"Name"} & ColNames, MissingField.UseNull))[T]) in CombinedCSV files and their missing columns:
finbox-2025-08-14 (43).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (44).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (45).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (46).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (47).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (48).csv b343055e (4), b343055e (16), b343055e (17) finbox-2025-08-14 (49).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (7), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (50).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (51).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (52).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (53).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (7), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (54).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (55).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (56).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (57).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (58).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (7), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (59).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (7), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (60).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (6), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (61).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (7), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (62).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (5), b343055e (6), b343055e (7), b343055e (8), b343055e (9), b343055e (10), b343055e (12), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (20), b343055e (21), b343055e (24) finbox-2025-08-14 (63).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (64).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (65).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (66).csv b343055e, b343055e (2), b343055e (3), b343055e (4), b343055e (8), b343055e (9), b343055e (13), b343055e (14), b343055e (15), b343055e (16), b343055e (17), b343055e (18), b343055e (19), b343055e (21), b343055e (24) finbox-2025-08-14 (84).csv b343055e (16)
Start as usual to import all files, e.g. from a folder, click Combine and combine and transform the data. This way PQ creates the necessary functions and queries for you.
There is one query to transform the example file, in there we have to perform some steps. Select the first 3 columns, right-click and unpivot other columns, you'll get an Attribute and Value column. In the Attribute column you'll see all the different headers with the index numbers in brackets, e.g. "b343055e href (2)" so the first step is to remove these.
In the Transform tab choose Extract, Text before Delimiter and use the "(" as delimter. We know there's a blank after the content so Trim the column also.
Basically the next step would be to pivot the Attribute column, but that doesn't work directly, if you try you'll get an error "Expression.Error: There were too many elements in the enumeration to complete the operation."
The solution is to group and add a local index.
Select the first 4 columns, group and use "All Rows" and column and choose "All Rows" as aggregation and you get this code:
= Table.Group(#"Trimmed Text", {"a263ff10 href", "d6d4b13f", "f397a0c8", "Attribute"}, {{"All Rows", each _, type table [a263ff10 href=nullable text, d6d4b13f=nullable text, f397a0c8=nullable text, Attribute=text, Value=text]}})
Modify the code to this code:
= Table.Group(#"Trimmed Text", {"a263ff10 href", "d6d4b13f", "f397a0c8", "Attribute"}, {{"All Rows", each Table.AddIndexColumn(_, "Index")}})
Remove all columns, except the "All Rows" column, expand the column, the data is the same as before, just an Index column is added ith an index number of each group.
Now you can Pivot the Attribute column and use the Value column as value and choose "Dont' aggregate" as function.
The final step is to remove the Index column, done. Below is the code how to transform the example file.
Andreas.
let
Source = Csv.Document(Parameter1,[Delimiter=",", Columns=51, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"a263ff10 href", "d6d4b13f", "f397a0c8"}, "Attribute", "Value"),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Attribute", each Text.BeforeDelimiter(_, "("), type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Extracted Text Before Delimiter",{{"Attribute", Text.Trim, type text}}),
#"Grouped Rows" = Table.Group(#"Trimmed Text", {"a263ff10 href", "d6d4b13f", "f397a0c8", "Attribute"}, {{"All Rows", each Table.AddIndexColumn(_, "Index")}}),
#"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"All Rows"}),
#"Expanded {0}" = Table.ExpandTableColumn(#"Removed Other Columns", "All Rows", {"a263ff10 href", "d6d4b13f", "f397a0c8", "Attribute", "Value", "Index"}, {"a263ff10 href", "d6d4b13f", "f397a0c8", "Attribute", "Value", "Index"}),
#"Pivoted Column" = Table.Pivot(#"Expanded {0}", List.Distinct(#"Expanded {0}"[Attribute]), "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
#"Removed Columns"
- omrangassan1 year agoNew Member
thanks, let me try and see !
- Anonymous1 year agoNot applicable
Maybe it is a good idea to show a working example, also for all fellow readers, here is an Excel file:
In that file there is just one row with data in the table, I deleted all other rows to keep the file size small.
I downloaded all the CSV files from your link into Z:\finbox and used that folder as input folder.On your machine you propably have to change the source folder: Data\Get Data\Data Source Settings...\ and choose your local folder. Or edit the Source step of the "finbox" query.
The query that transforms each CSV file is named "Transform Sample File".
If you refresh the finbox query you'll end up with a bit more then 240.000 rows.
Andreas.