Forum Discussion

igonzalezb's avatar
igonzalezb
Icon for Helper I rankHelper I
5 years ago
Solved

Split from digit to non digit only once

I would like to split a text column by character transition (digit to non digit), but only once from left to right.  SplitTextByCharacterTransition doesn't seem to have that option. Also, I don't k...
  • v-kelly-msft's avatar
    v-kelly-msft
    5 years ago

    Hi  igonzalezb ,

     

    Use below M codes:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrAwNNI1STQ0Mk40NExUitWJVkpMijc2yTM1g3KSzcxMSoxNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let 
    l = Text.ToList([Text]),
    p = List.PositionOfAny(l,{"0".."9"}),
    txt = 
    if Value.Is(l{0},type text)
    then List.RemoveFirstN(l,p)
    else l,
    p1 = List.PositionOfAny(txt,{"0".."9"}),
    p2 = List.PositionOfAny(txt,{"A".."z"}&{"-"}),
    res = Text.Combine( List.Range(txt,p1,p2-p1),"")
    in res),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type text}}),
        #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each let 
    l = Text.Split([Text],[Custom]),
    res = l{0}&[Custom]
    in 
    res
    ),
        Custom1 = Table.AddColumn(#"Added Custom1", "Custom.2", each let 
    l = Text.Split([Text],[Custom]),
    res = l{1}
    in
    res
    )
    in
        Custom1

    And you will see:

    For the related .pbxi file,pls see attached.

     

    Best Regards,
    Kelly

    Did I answer your question? Mark my post as a solution!