Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
tigerp77
New Member

How to normalize irregular CSV files...

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...)
----------------

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

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"

ronrsnfld_0-1676512875242.png

 

 

 

View solution in original post

4 REPLIES 4
ronrsnfld
Super User
Super User

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"

ronrsnfld_0-1676512875242.png

 

 

 

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 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors