Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Detect number in a text string

I want a true/false statement in Power Query to detect numbers in a text string.

 

45a- TRUE

ab- FALSE

34- TRUE

aa46- TRUE

12 Replies

  • Actually there is another solution if you want to make it dynamic you can use this kind of code
    Let's say the name of this field is [Sample1]

    45a- TRUE

    ab- FALSE

    34- TRUE

    aa46- TRUE

    Power Query code:
    if List.AnyTrue(List.Transform({0..9},(NumberColumn) => Text.Contains([Sample1],Number.ToText(NumberColumn)))) = true then "TRUE" else "FALSE"

    Please put thumbs up if I help you better

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for the reply but I don't want to extract a number from the string. I want a function to only detects IF there is a number in the text string and give me a true/false value

      • PaulDBrown's avatar
        PaulDBrown
        Community Champion

        Well... that link got me to this:

         

        Using

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkw0NlGK1QEykszy8sCs3FwIlZdvCGYkJRlZghmWFklJSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [List = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"List", type text}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Select([List], {"0".."9"})),
            #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}}),
            #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Custom.1", each if [Custom] = null then false else true),
            #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Custom"})
        in
            #"Removed Columns"

        Does that help?

  • v-luwang-msft's avatar
    v-luwang-msft
    Community Support

    Hi Anonymous ,

    Text.contails could not work on more then one values, similar error refer:

    https://community.powerbi.com/t5/Power-Query/using-if-text-Contains-for-multiple-conditions-in-Power-Query-M/td-p/2196090 

     

    And change yours to the below:

     

     

    if Text.Contains([Product], "1")  or Text.Contains([Product], "2")                   or Text.Contains([Product], "3")                   or Text.Contains([Product], "4")                   or Text.Contains([Product], "5")                   or Text.Contains([Product], "6")                   or Text.Contains([Product], "7")                   or Text.Contains([Product], "8")                   or Text.Contains([Product], "9")                   or Text.Contains([Product], "0")    then "True" else "False"

     

     

     

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


    Best Regards

    Lucien

      • v-luwang-msft's avatar
        v-luwang-msft
        Community Support

        Hi  PaulDBrown ,

        Thank you for your reply, based on what you have provided, each time you use Contains, you also judge only one element and then use a combination of multiple judgments to get the final result.

         

        Best Regards

        Lucien