Forum Discussion

PhilC's avatar
PhilC
Resolver I
6 years ago
Solved

Split text based on every second delimiter

Hi, looking for assistance with manipulating columns in Power Query.   I need to split a column based on a Comma delimiter, however I need to only split every second one.  The data is formatted Sur...
  • az38's avatar
    6 years ago

    Hi PhilC 

    try split by ".,"

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs5ILMhNzNNRcNFz1NNRcErMTi3SUQgCMkMy8nMTi4FCej56SrE60UouiWWZQH4gUM69KDGvREfBVy8IKBULAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Person = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Person", Splitter.SplitTextByDelimiter(".,", QuoteStyle.Csv), {"Person.1", "Person.2", "Person.3"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Person.1", type text}, {"Person.2", type text}, {"Person.3", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
        #"Trimmed Text" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Value", Text.Trim, type text}}),
        #"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each [Value]&"."),
        #"Replaced Value" = Table.ReplaceValue(#"Added Custom","..",".",Replacer.ReplaceText,{"Custom"}),
        #"Removed Other Columns" = Table.SelectColumns(#"Replaced Value",{"Custom"})
    in
        #"Removed Other Columns"

    do not hesitate to give a kudo to useful posts and mark solutions as solution
    Linkedin

  • Jimmy801's avatar
    6 years ago

    Hello PhilC 

     

    had created once a nice function that can be used here. Soltuion by az38  is nice, but would work only in for this 3 cells. This function can be applied dynamically

    (tSplitText as text, 
    tDelimiter as text, 
    nOccurance as nullable number) as list =>
    
    let
        nOccuranceIntern = if nOccurance = null then 2 else nOccurance,
        Source = "Chapman, D.A., Baker, R., Thomas, B.L.",
        ListFromText = Text.ToList(tSplitText),
        CreateListOfDelimiter = List.Accumulate
            (
                ListFromText,
                [CurrentRow = -1, DelimiterFound={}],
                (state, current)=>
                    if current = tDelimiter then
                            Record.TransformFields(state, {{"DelimiterFound", each _ & {state[CurrentRow]}},{"CurrentRow", each _ +1 }})
                            else
                            Record.TransformFields(state, {{"CurrentRow", each _ +1 }})
            )[DelimiterFound],
        ListAlternate = List.Alternate(CreateListOfDelimiter,nOccuranceIntern-1,nOccuranceIntern-1),
        ChangeTextList = List.Accumulate
            (
                ListAlternate,
                ListFromText,
                (state, current)=>
                    List.ReplaceRange(state,current+1,1,{"%%%"})
            ),
        SplitTextByNewDelimiter = Text.Split
            (
                List.Accumulate
                    (
                        ChangeTextList,
                        "",
                        (state, current)=> 
                            state & current
                    ),
                "%%%"
             )
    
    
    in
        SplitTextByNewDelimiter

     

    If this post helps or solves your problem, please mark it as solution.
    Kudos are nice to - thanks
    Have fun

    Jimmy

  • Mariusz's avatar
    6 years ago

    Hi PhilC 

     

    a third option for you to choose.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs5ILMhNzNNRcNFz1NNRcErMTi3SUQgCMkMy8nMTi4FCej56SrE60UouiWWZQH4gUM69KDGvREfBVy8IKBULAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Person = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,".,",".#",Replacer.ReplaceText,{"Person"}),
        #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value", {{"Person", Splitter.SplitTextByDelimiter("#", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Person"),
        #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter",{{"Person", Text.Trim, type text}})
    in
        #"Trimmed Text"

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.