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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Convert Rows to columns

Hi,

 

I have a csv file that look like this:

 

iditem_1value_1item_2value_2item_3value_3item_4value_4item_5value_5
1a1b2c3d4e5
2e3        
3c3        

 

 

And I want to look like this:

 

iditemvalue
1a1
1b2
1c3
1d4
1e5
2e3
3c3

 

With python or vba it will be easy, but I wan't to do it with power query. Is that posible?

 

Thank you,

 

 

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

@Anonymous 

 

This works with your sample data

Please see attached file's query editor for steps

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUoEYhCdBMRGQJwMxMZAnALEJkCcCsSmSrE60WDZVKisAl4MUm2MZBZ2FBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [id = _t, item_1 = _t, value_1 = _t, item_2 = _t, value_2 = _t, item_3 = _t, value_3 = _t, item_4 = _t, value_4 = _t, item_5 = _t, value_5 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"item_1", type text}, {"value_1", Int64.Type}, {"item_2", type text}, {"value_2", Int64.Type}, {"item_3", type text}, {"value_3", Int64.Type}, {"item_4", type text}, {"value_4", Int64.Type}, {"item_5", type text}, {"value_5", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"id"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute.1]), "Attribute.1", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([value] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.2"})
in
    #"Removed Columns"

Regards
Zubair

Please try my custom visuals

View solution in original post

2 REPLIES 2
Zubair_Muhammad
Community Champion
Community Champion

@Anonymous 

 

This works with your sample data

Please see attached file's query editor for steps

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUoEYhCdBMRGQJwMxMZAnALEJkCcCsSmSrE60WDZVKisAl4MUm2MZBZ2FBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [id = _t, item_1 = _t, value_1 = _t, item_2 = _t, value_2 = _t, item_3 = _t, value_3 = _t, item_4 = _t, value_4 = _t, item_5 = _t, value_5 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"item_1", type text}, {"value_1", Int64.Type}, {"item_2", type text}, {"value_2", Int64.Type}, {"item_3", type text}, {"value_3", Int64.Type}, {"item_4", type text}, {"value_4", Int64.Type}, {"item_5", type text}, {"value_5", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"id"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute.1]), "Attribute.1", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([value] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.2"})
in
    #"Removed Columns"

Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Thank you!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.