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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

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 @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"

 

 

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 @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"

 

 

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.






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

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.






Anonymous
Not applicable

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)

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.






Anonymous
Not applicable

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

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.






Anonymous
Not applicable

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.