Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Extract Using Any Non-Number As Delimiter

Hi. I am trying to extract numbers that only exist before any characters occur in the string. Once ANY characters start, that will be the end of the number. The trick here is that somtimes there are also numbers after the characters I want to use as a delimiter and should be ignored, so I simply can't use the function to extract all numbers. If there are no numbers before characters start, it would default to 1. Below are some examples:

 

SourceExtracted
30 30
40A 40
12,5CLR 12
ABC 1
ABC123

 1

 

I have attempted to use the Add Column --> Column From Examples function but it seems to revert to a simple "IF this value, THEN that value". Thanks!

  • Anonymous's avatar
    Anonymous
    5 years ago
    = Table.AddColumn(#"Modificato tipo", "Personalizzato", each try Number.From(Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => c <"0" or c>"9")([Source]){0}) otherwise 1)
  • CNENFRNL's avatar
    CNENFRNL
    5 years ago

    Anonymous , you need to add a new column this way in your senario,

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

     

    = Table.AddColumn(#"previou step", "extract", each try Number.From(Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c))([Source]){0}) otherwise 1)

     

    • Anonymous's avatar
      Anonymous
      Not applicable
      = Table.AddColumn(#"Modificato tipo", "Personalizzato", each try Number.From(Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => c <"0" or c>"9")([Source]){0}) otherwise 1)
      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous. All values in this column are showing up as 1 with this paritcular code.

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello Anonymous 

     

    you can use this formula in a new column

    let GetNumber=  List.FirstN(Text.ToList(_[Source]), (itemlist)=> Value.Is(try Number.From(itemlist) otherwise "text",type number) ) in if List.IsEmpty(GetNumber) then 1 else Number.From(Text.Combine(GetNumber))

     

    here a complete example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZQitWJVjIxcATThkY6ps4+QWC2o5MzjDY0MlaKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Source = _t]),
        AddColumn = Table.AddColumn
        (
            Source,
            "Extract",
            each let GetNumber=  List.FirstN(Text.ToList(_[Source]), (itemlist)=> Value.Is(try Number.From(itemlist) otherwise "text",type number) ) in if List.IsEmpty(GetNumber) then 1 else Number.From(Text.Combine(GetNumber))
        )
    in
        AddColumn

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • CNENFRNL's avatar
    CNENFRNL
    Community Champion

    Hi, Anonymous , you might want try a simple solution

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZQitWJVjIxcATThkY6ps4+QWC2o5MzjDY0MlaKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Source = _t]),
        
        #"Added Custom" = Table.AddColumn(Source, "Extracted", each let str = List.First(Text.SplitAny([Source],Text.Remove([Source],{"0".."9"}))) in if str="" then 1 else Number.From(str))
    in
        #"Added Custom"

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi CNENFRNL . Thanks for replying. When I create a new column with this code, it looks like my table turns into the exact image you provided. I do not need those exact values as they are examples to show the different scenarios of values I would need to convernt.

      • CNENFRNL's avatar
        CNENFRNL
        Community Champion

        Anonymous , you need to add a new column this way in your senario,