Forum Discussion
How to split a CSV file into tables based on blank rows
- Anonymous5 years ago
Hi JollyRoger01
I missed that multiple tables...you need to group the rows into different tables, another way of increasing index, you can take from here
let Source = Csv.Document(File.Contents("C:\Users\----\Book1.csv"),[Delimiter=",", Encoding=1252, QuoteStyle=QuoteStyle.None]), checkBlank = Table.CombineColumns(Source, Table.ColumnNames(Source), (x)=> if List.MatchesAll(x,each _ = "") then 1 else 0,"check"), #"Added Index" = Table.AddIndexColumn(checkBlank, "Index", 0, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Added Index", "Group", each if [Index]=0 then 0 else if [check] = 0 then List.Sum(List.FirstN( checkBlank[check], [Index])) else -1), SouceIndex = Table.AddIndexColumn(Source, "Index", 0,1), #"Merged Queries" = Table.NestedJoin(SouceIndex, {"Index"}, #"Added Custom", {"Index"}, "Group", JoinKind.LeftOuter), #"Expanded Group" = Table.ExpandTableColumn(#"Merged Queries", "Group", {"check", "Group"}, {"check", "Group"}), #"Filtered Rows" = Table.SelectRows(#"Expanded Group", each ([check] = 0)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index", "check"}), #"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"allrows", each _, type table }}) in #"Grouped Rows"
Yes that looks like it would work, however I don't know how many blank rows and hence how many tables there will be. I have tried to index each none blank section and increment them here: How-to-increment-a-number-variable-if-column-value-equals thinking that I could somehow use this info to create indexed tables, but so far no luck. This is a common csv output format from many of the tools we use at work so I thought there would be some sort of standardised way to do this.
Hi JollyRoger01
I missed that multiple tables...you need to group the rows into different tables, another way of increasing index, you can take from here
let
Source = Csv.Document(File.Contents("C:\Users\----\Book1.csv"),[Delimiter=",", Encoding=1252, QuoteStyle=QuoteStyle.None]),
checkBlank = Table.CombineColumns(Source, Table.ColumnNames(Source), (x)=> if List.MatchesAll(x,each _ = "") then 1 else 0,"check"),
#"Added Index" = Table.AddIndexColumn(checkBlank, "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Group", each if [Index]=0 then 0
else if [check] = 0 then List.Sum(List.FirstN( checkBlank[check], [Index]))
else -1),
SouceIndex = Table.AddIndexColumn(Source, "Index", 0,1),
#"Merged Queries" = Table.NestedJoin(SouceIndex, {"Index"}, #"Added Custom", {"Index"}, "Group", JoinKind.LeftOuter),
#"Expanded Group" = Table.ExpandTableColumn(#"Merged Queries", "Group", {"check", "Group"}, {"check", "Group"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Group", each ([check] = 0)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index", "check"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"allrows", each _, type table }})
in
#"Grouped Rows"
- JollyRoger015 years agoHelper III
Perfect, thank you so much!
I just had one last question on this, do you know how I promote the headers of all the tables when they are grouped (from the very last step) without having to open each?
EDIT: All good, got this one worked out:
#"Promote Headers" = Table.TransformColumns(#"Grouped Rows", {{"allrows", each Table.PromoteHeaders(_, [PromoteAllScalars = true])}}) in