Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
hiral
Frequent Visitor

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

1 ACCEPTED SOLUTION
v-luwang-msft
Community Support
Community Support

Hi @hiral ,

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... 

 

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"

 

 

vluwangmsft_0-1658394921763.png

vluwangmsft_1-1658395006359.png

 

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


Best Regards

Lucien

View solution in original post

12 REPLIES 12
VinceMaku
New Member

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

v-luwang-msft
Community Support
Community Support

Hi @hiral ,

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... 

 

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"

 

 

vluwangmsft_0-1658394921763.png

vluwangmsft_1-1658395006359.png

 

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


Best Regards

Lucien

@v-luwang-msft 

Using the "Contains" option in the conditional column interface seems to work for me:
contains digit.png

 

Attached is the file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






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

PaulDBrown
Community Champion
Community Champion

See if this helps:
https://eriksvensen.wordpress.com/2018/03/06/extraction-of-number-or-text-from-a-column-with-both-te... 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






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
Community Champion
Community Champion

Well... that link got me to this:

PQ.png

 

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?





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






I'm sorry but the code is just too complex for me to understand. I find it easier to add a condition with true conditions being every number from 1-9, see below:

hiral_0-1657467913157.png

However a single line code would be easier to manage. Somtehing like this: 

text.Contains([Product], type number)
PaulDBrown
Community Champion
Community Champion

Your conditional column approach does deliver a single line of code (and is way simpler than my suggestion! )

 

 

 

= Table.AddColumn(#"Renamed Columns", "Number?", each if Text.Contains([Product], "1") then true else if Text.Contains([Product], "2") then true else if Text.Contains([Product], "3") then true else if Text.Contains([Product], "4") then true else if Text.Contains([Product], "5") then true else if Text.Contains([Product], "6") then true else if Text.Contains([Product], "7") then true else if Text.Contains([Product], "8") then true else if Text.Contains([Product], "9") then true else if Text.Contains([Product], "0") then true else false)

 

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






However, I can't get the code to work properly, I keep getting an error

PaulDBrown
Community Champion
Community Champion

It shouldn't return an error. Is the Product column set as type Text?

number.png

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Yes it's set as Text. I get an Expression. Error. Not possible to convert the type Type to type Text.

Information:
Value=[Type]
Type=[Type]

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.