Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreWe've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now
Hi, everybody!
I usually do simple aggregation with Power Query, but I was asked to aggregate an irregular CSV file and I was in trouble.
In Excel's Power Query, as described later, I want to convert a CSV file in which all rows are not one row of data to a CSV file of one row and one data, but I can't get the method. Can someone please help me?
Source Table
----------------
1
apple, 2, 5
orange, 1, 10
grape, 2, 4
3
orange, 1.2, 20
4
apple, 2, 8
grape, 2.5, 6
(next...)
----------------
↓
----------------
1, apple, 2, 5
1, orange, 1, 10
1, grape, 2, 4
3, orange, 1,2, 20
4, apple, 2, 8
4, grape, 2.5, 6
(next...)
----------------
Solved! Go to Solution.
Here's one way to do this in Power Query:
let
//change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table28"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//remove blank rows
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Column1] <> null and [Column1] <> " "),
//create a "grouper" column
#"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1, Int64.Type),
//check if cell contents are all digits
// if so then copy over the Index value else write null
// Then fill down
#"Added Custom" = Table.AddColumn(#"Added Index", "grouper", each if List.ContainsAll({"0".."9"},Text.ToList([Column1])) then [Index] else null, Int64.Type),
#"Filled Down" = Table.FillDown(#"Added Custom",{"grouper"}),
#"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Index"}),
//Group by the "grouper" column
#"Grouped Rows" = Table.Group(#"Removed Columns", {"grouper"}, {
//create a list of the first row in the subtable concatenated to each of the other lines
{"new csv", (t)=>List.Transform(List.RemoveFirstN(t[Column1],1), each t[Column1]{0} & ", " & _), type list}
}),
//Remove the "Grouper" column and expand the List column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
#"Expanded new csv" = Table.ExpandListColumn(#"Removed Columns1", "new csv")
in
#"Expanded new csv"
Here's one way to do this in Power Query:
let
//change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table28"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//remove blank rows
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Column1] <> null and [Column1] <> " "),
//create a "grouper" column
#"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1, Int64.Type),
//check if cell contents are all digits
// if so then copy over the Index value else write null
// Then fill down
#"Added Custom" = Table.AddColumn(#"Added Index", "grouper", each if List.ContainsAll({"0".."9"},Text.ToList([Column1])) then [Index] else null, Int64.Type),
#"Filled Down" = Table.FillDown(#"Added Custom",{"grouper"}),
#"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Index"}),
//Group by the "grouper" column
#"Grouped Rows" = Table.Group(#"Removed Columns", {"grouper"}, {
//create a list of the first row in the subtable concatenated to each of the other lines
{"new csv", (t)=>List.Transform(List.RemoveFirstN(t[Column1],1), each t[Column1]{0} & ", " & _), type list}
}),
//Remove the "Grouper" column and expand the List column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
#"Expanded new csv" = Table.ExpandListColumn(#"Removed Columns1", "new csv")
in
#"Expanded new csv"
Wow, thanks ronrsnfld. I'm happy to get a reply so quickly.
I'm sorry I can't understand it quickly, but I'll read the reply I received.
I guess I was not familiar with Table.FillDown.
Thanks ronrsnfld, I got some very great tips. Appreciate it.
Happy to help
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 2 |
| User | Count |
|---|---|
| 8 | |
| 8 | |
| 6 | |
| 6 | |
| 5 |