Forum Discussion
Split a .csv with multiple tables into multiple queries
- 4 years ago
1. Import your csv and have following code for primary file (C:\test.csv should be replaced with your csv file path)
let Source = Csv.Document(File.Contents("C:\test.csv"),[Delimiter=",", Columns=4, Encoding=65001, QuoteStyle=QuoteStyle.None]), #"Added Custom" = Table.AddColumn(Source, "IsBlank", each List.IsEmpty(List.RemoveItems(Record.ToList(_),{""}))), #"List with Index" = List.Generate(()=>[x=0,i=0], each [i]<Table.RowCount(#"Added Custom"), each [i=[i]+1, x=(if #"Added Custom"[IsBlank]{i}=true then [x]+1 else [x])], each [x]), #"Create Table with Index" = Table.FromColumns(Table.ToColumns(#"Added Custom") & {#"List with Index"},Table.ColumnNames(#"Added Custom")&{"Index"}), #"Filtered Rows" = Table.SelectRows(#"Create Table with Index", each ([IsBlank] = false)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"IsBlank"}) in #"Removed Columns"2. Right click on this query in left pane and uncheck Enable load as this file need not be loaded into your report.
3. Right click on this query and Reference. Create overall 4 references. Rename them as csv1, csv2, csv3 and csv4 or whatever name you like.
4. In csv1, filter it on Index 0, in csv2, filter on 1, in csv3, filter on 2 and in csv4, filter on 3. Delete Index columns from there.
5. Promote headers in all these 4 queries and do a Detect data type for entire table.
The solution file as well as test.csv used in this can be downloaded from - https://1drv.ms/u/s!Akd5y6ruJhvhuWwDHySKxd-kYc0-?e=scwGxK
1. Import your csv and have following code for primary file (C:\test.csv should be replaced with your csv file path)
let
Source = Csv.Document(File.Contents("C:\test.csv"),[Delimiter=",", Columns=4, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Added Custom" = Table.AddColumn(Source, "IsBlank", each List.IsEmpty(List.RemoveItems(Record.ToList(_),{""}))),
#"List with Index" = List.Generate(()=>[x=0,i=0], each [i]<Table.RowCount(#"Added Custom"), each [i=[i]+1, x=(if #"Added Custom"[IsBlank]{i}=true then [x]+1 else [x])], each [x]),
#"Create Table with Index" = Table.FromColumns(Table.ToColumns(#"Added Custom") & {#"List with Index"},Table.ColumnNames(#"Added Custom")&{"Index"}),
#"Filtered Rows" = Table.SelectRows(#"Create Table with Index", each ([IsBlank] = false)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"IsBlank"})
in
#"Removed Columns"2. Right click on this query in left pane and uncheck Enable load as this file need not be loaded into your report.
3. Right click on this query and Reference. Create overall 4 references. Rename them as csv1, csv2, csv3 and csv4 or whatever name you like.
4. In csv1, filter it on Index 0, in csv2, filter on 1, in csv3, filter on 2 and in csv4, filter on 3. Delete Index columns from there.
5. Promote headers in all these 4 queries and do a Detect data type for entire table.
The solution file as well as test.csv used in this can be downloaded from - https://1drv.ms/u/s!Akd5y6ruJhvhuWwDHySKxd-kYc0-?e=scwGxK