Forum Discussion

jerryr125's avatar
jerryr125
Helper IV
1 year ago
Solved

Power Query - create multiple rows

Hi - I would like to do the following - create a row for each value in a field.

 

Example:

 

IDFood Type
1Ice Cream; Cake; Cookies
2Apples; Bananas; Pears; Peaches
3Pizza


Output:

 

IDFood Type
1Ice Cream
1Cake
1Cookies
2Apples
2Bananas
2Peaches
2Pears
3Pizza


Therefore, the ";" indicates a new record (so to speak) - any thoughts ? Jerry

  • Source

     

     

    You turn the string into a List, and then expand it:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJMTlVwLkpNzLVWcE7MTgWS+fnZmanFSrE60UpGQAWOBQU5qcXWCk6JeUAIZASkJhZBqOQMqDpjoLqAzKqqRKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Food Type" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Food Type", type text}}),
        #"Create List" = Table.TransformColumns(#"Changed Type",{"Food Type", each Text.Split(_,";"),type {text}}),
        #"Expanded Food Type" = Table.ExpandListColumn(#"Create List", "Food Type")
    in
        #"Expanded Food Type"

     

    Result

     

     

     

  • jerryr125 Many ways to solve this, here is one using List.TransformMany

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJMTlVwLkpNzLVWcE7MTgWS+fnZmanFSrE60UpGQAWOBQU5qcXWCk6JeUAIZASkJhZBqOQMqDpjoLqAzKqqRKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Food Type" = _t]),
        ChangedType =
            Table.TransformColumnTypes ( Source, { { "ID", Int64.Type } } ), 
        Transform = 
            List.TransformMany (
                Table.ToRows ( ChangedType ), 
                ( z ) => Text.SplitAny ( z{1}, ";" ), 
                ( x, y ) => { x{0}, y }
            ), 
        ToTable = 
            Table.FromRows ( 
                Transform, 
                type table [ ID = Int64.Type, Food Type = text ] 
            )
    in
        ToTable

     

  • Hi jerryr125 , you can solve this problem by UI, just select the column Food Type, go to the homw tab, select Split Column by delimiter and make the setting presented in the below picture (it is important to select by row)

     

     

4 Replies

  • Source

     

     

    You turn the string into a List, and then expand it:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJMTlVwLkpNzLVWcE7MTgWS+fnZmanFSrE60UpGQAWOBQU5qcXWCk6JeUAIZASkJhZBqOQMqDpjoLqAzKqqRKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Food Type" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Food Type", type text}}),
        #"Create List" = Table.TransformColumns(#"Changed Type",{"Food Type", each Text.Split(_,";"),type {text}}),
        #"Expanded Food Type" = Table.ExpandListColumn(#"Create List", "Food Type")
    in
        #"Expanded Food Type"

     

    Result

     

     

     

  • AntrikshSharma's avatar
    AntrikshSharma
    Community Champion

    jerryr125 Many ways to solve this, here is one using List.TransformMany

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJMTlVwLkpNzLVWcE7MTgWS+fnZmanFSrE60UpGQAWOBQU5qcXWCk6JeUAIZASkJhZBqOQMqDpjoLqAzKqqRKXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Food Type" = _t]),
        ChangedType =
            Table.TransformColumnTypes ( Source, { { "ID", Int64.Type } } ), 
        Transform = 
            List.TransformMany (
                Table.ToRows ( ChangedType ), 
                ( z ) => Text.SplitAny ( z{1}, ";" ), 
                ( x, y ) => { x{0}, y }
            ), 
        ToTable = 
            Table.FromRows ( 
                Transform, 
                type table [ ID = Int64.Type, Food Type = text ] 
            )
    in
        ToTable

     

  • Hi jerryr125 , you can solve this problem by UI, just select the column Food Type, go to the homw tab, select Split Column by delimiter and make the setting presented in the below picture (it is important to select by row)

     

     

  • Hi - I did a split then a unpivot and solved it - thanks so much - Jerry