Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Transform Delimited Data

Hi,

 

I hae a set of data that looks like below;

FirstNameSurnameEmployeeIDResult
Ryan|Alan|RyanArrowsmith|Delaney|Delaney123456|456789|456133Positive|Positive|Negative

 

I would like to convert it to this;

FirstNameSurnameEmployeeIDResult
RyanArrowsmith123456Positive
AlanDelaney456789Positive
RyanDelaney456133Negative

 

Thankyou

  • Hi Anonymous, try this code:

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCqpMzKtxzAESYJZXalqako6SY1FRfnlxbmZJRo1LKlAytRJOB4NEgUoMjYxNTM1qgNjcwhJEGRob15iCAVA2IL84sySzLLUGzvBLTU+EMPJLFIJLi1KVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [FirstName = _t, Surname = _t, EmployeeID = _t, Result = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", type text}, {"Result", type text}}),
        FirstName = List.Transform(Table.Column(#"Changed Type","FirstName"), Splitter.SplitTextByDelimiter("|")){0},
        Surname = List.Transform(Table.Column(#"Changed Type","Surname"), Splitter.SplitTextByDelimiter("|")){0},
        EmployeeID = List.Transform(Table.Column(#"Changed Type","EmployeeID"), Splitter.SplitTextByDelimiter("|")){0},
        Result = List.Transform(Table.Column(#"Changed Type","Result"), Splitter.SplitTextByDelimiter("|")){0},
        #"Combined Lists" = List.Zip({FirstName,Surname,EmployeeID,Result}),
        #"Converted to Table" = Table.FromList(#"Combined Lists", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Extracted Values" = Table.TransformColumns(#"Converted to Table", {"Column1", each Text.Combine(List.Transform(_, Text.From), "|"), type text}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Column1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"FirstName", "Surname", "EmployeeID", "Result"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", Int64.Type}, {"Result", type text}})
    in
        #"Changed Type1"

     

    As you can see, this works 4 items, and should work with an indefinite number.

    Becomes...

     

    You just have to rename your columns to whatever you want them to be. (EDIT: I changed to the code to rename them back to the original names in the split step. Didn't bother uploading a revised image though)

     

16 Replies

  • Vvelarde's avatar
    Vvelarde
    Community Champion

    Anonymous 

     

    Hi, try this:

     

    1. Unpivot columns

    2.Split by delimiter

    3.Transpose

    4. Promote headers

     

    Regards 

     

    Victor

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for this.

       

      Do you know how to allow for variable numbers of delimited components, ie. more fields separated by the the pipe symbol, without having to edit the query?

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

        Taking a hint from Vvelarde solution, try this M code

        let
            Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", type text}, {"Result", type text}}),
            #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
            #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
            #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Value.1", "Value.2", "Value.3"}),
            #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value.1", type text}, {"Value.2", type text}, {"Value.3", type text}}),
            #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Index"}),
            #"Transposed Table" = Table.Transpose(#"Removed Columns"),
            #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
            #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", Int64.Type}, {"Result", type text}})
        in
            #"Changed Type2"

        This will work for as many columns as you keep adding.

        Hope this helps.

  • edhans's avatar
    edhans
    Community Champion

    Hi Anonymous, try this code:

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCqpMzKtxzAESYJZXalqako6SY1FRfnlxbmZJRo1LKlAytRJOB4NEgUoMjYxNTM1qgNjcwhJEGRob15iCAVA2IL84sySzLLUGzvBLTU+EMPJLFIJLi1KVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [FirstName = _t, Surname = _t, EmployeeID = _t, Result = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", type text}, {"Result", type text}}),
        FirstName = List.Transform(Table.Column(#"Changed Type","FirstName"), Splitter.SplitTextByDelimiter("|")){0},
        Surname = List.Transform(Table.Column(#"Changed Type","Surname"), Splitter.SplitTextByDelimiter("|")){0},
        EmployeeID = List.Transform(Table.Column(#"Changed Type","EmployeeID"), Splitter.SplitTextByDelimiter("|")){0},
        Result = List.Transform(Table.Column(#"Changed Type","Result"), Splitter.SplitTextByDelimiter("|")){0},
        #"Combined Lists" = List.Zip({FirstName,Surname,EmployeeID,Result}),
        #"Converted to Table" = Table.FromList(#"Combined Lists", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Extracted Values" = Table.TransformColumns(#"Converted to Table", {"Column1", each Text.Combine(List.Transform(_, Text.From), "|"), type text}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Column1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"FirstName", "Surname", "EmployeeID", "Result"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"FirstName", type text}, {"Surname", type text}, {"EmployeeID", Int64.Type}, {"Result", type text}})
    in
        #"Changed Type1"

     

    As you can see, this works 4 items, and should work with an indefinite number.

    Becomes...

     

    You just have to rename your columns to whatever you want them to be. (EDIT: I changed to the code to rename them back to the original names in the split step. Didn't bother uploading a revised image though)

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      edhans Perfect!  It does what it says on the tin!

       

      Thankyou all.

      • edhans's avatar
        edhans
        Community Champion

        Great Anonymous . Glad your project can move forward.