Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

insert character in string after last number

Hello,

 

I have a field with several job codes formated like this.

 

A123ABC

333BGT

5555TMGT

B4454JKRR

 

How can I create a new column in my query so that it creates a new value with the insertion of a "/" right after the last number so that the list above is

 

A123/ABC

333/BGT

5555/TMGT

B4454/JKRR

 

Thank you

J

  • Hi Anonymous 

    Download PBIX file with code

    This works

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQ0MnZ0claK1YlWMjY2dnIPATNNgSDEF8pxMjExNfHyDgpSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Split Column by Character Transition" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Column1.1", "Column1.2"}),
        #"Merged Columns" = Table.CombineColumns(#"Split Column by Character Transition",{"Column1.1", "Column1.2"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"Merged")
    in
        #"Merged Columns"

     

    regards

    Phil

3 Replies

  • Hi Anonymous 

    Download PBIX file with code

    This works

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQ0MnZ0claK1YlWMjY2dnIPATNNgSDEF8pxMjExNfHyDgpSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Split Column by Character Transition" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Column1.1", "Column1.2"}),
        #"Merged Columns" = Table.CombineColumns(#"Split Column by Character Transition",{"Column1.1", "Column1.2"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"Merged")
    in
        #"Merged Columns"

     

    regards

    Phil

    • Anonymous's avatar
      Anonymous
      Not applicable

      wow! 

      thank you very much

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    Place the following M code in a blank query to see the steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQ0MnZ0claK1YlWMjY2dnIPATNNgSDEF8pxMjExNfHyDgoC8xyNjUF6TEycTE2B+mIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Insert([Column1],1+List.Max(Text.PositionOfAny([Column1],{"0".."9"},Occurrence.All)),"/"), type text)
    in
        #"Added Custom"

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.