cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
madrid
Frequent Visitor

Transform blocks of rows into table

Dear all,

 

I have some data in format of block of rows. Is there any idea on how to transform this kind of data into table ?

See attached picture for demonstration.

 

Thank you,

madrid

 

madrid_0-1653128151479.png

 

1 ACCEPTED SOLUTION
ronrsnfld
Solution Sage
Solution Sage

If the rows will always be in groups of four, you can

  • Add an index column where each group of rows has an assigned number
    • Without this unique identifier for each block, the Pivot with no aggregation will error
  • Pivot on Column 1 with Column2 values and no aggregation

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUXJ0dFSK1YlWcs1NzMwB8lNBtKFDcn5uQWJepR6QBks7poMUGxuAOSGZJTkgbnAikAIJQM1ycnLCNMsIh1lGpihmeYYgm+Ts7IxpkjEuVxmimOSbmJeYnlqkFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),

    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), Int64.Type}}),

    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

 

The above code assumes each block has four rows.  If the numbers of rows might vary, but, for example, the first row is always "Name", then you can still assign a unique number to each block with the code below, and then do your pivot as before.

 

    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Column1] = "Name" then [Index] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),

 

 

View solution in original post

4 REPLIES 4
ronrsnfld
Solution Sage
Solution Sage

If the rows will always be in groups of four, you can

  • Add an index column where each group of rows has an assigned number
    • Without this unique identifier for each block, the Pivot with no aggregation will error
  • Pivot on Column 1 with Column2 values and no aggregation

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUXJ0dFSK1YlWcs1NzMwB8lNBtKFDcn5uQWJepR6QBks7poMUGxuAOSGZJTkgbnAikAIJQM1ycnLCNMsIh1lGpihmeYYgm+Ts7IxpkjEuVxmimOSbmJeYnlqkFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),

    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), Int64.Type}}),

    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

 

The above code assumes each block has four rows.  If the numbers of rows might vary, but, for example, the first row is always "Name", then you can still assign a unique number to each block with the code below, and then do your pivot as before.

 

    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Column1] = "Name" then [Index] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),

 

 

madrid
Frequent Visitor

Fantastic! I was able to replicate these steps and achieve the result.

Huge thanks!

ronrsnfld
Solution Sage
Solution Sage

Are there always going to be four rows in a block? 

madrid
Frequent Visitor

yes, it is.

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors