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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
DanFromMontreal
Helper IV
Helper IV

Test cell value for string

Good afternoon all,

I want to create a new column with information based on another column.

This column (NEQ) is formatted as TEXT and contain some string and some numbers (formatted as TEXT).

The new column should evaluate the column NEQ and return in the new column its status based on these criteria.

Spent more than 60 minutes to make it work but could not figure out what is wrong.

 

Any idea would be appreciated. 

 

#"Add column Info String" = Table.AddColumn(#"PreviousStep", "Info String", each
if Number.IsNaN(Number.FromText([NEQ])) and [NEQ]="NAS" then "Some String" else
if Number.IsNaN(Number.FromText([NEQ])) and [NEQ] = "" then "Empty String" else
if Number.IsNaN(Number.FromText([NEQ])) then "Other String" else
if Number.FromText([NEQ])<=10 then "<=10" else
if Number.FromText([NEQ])<=100 then "<=100" else ">100"),

 

Many thanks

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

Number.IsNaN is used to test the presence of 0/0. I believe this is not needed. Use below in a custom column

= try (if Number.FromText([NEQ])<=10 then "<=10" else
if Number.FromText([NEQ])<=100 then "<=100" else
if Value.Is(Number.FromText([NEQ]), type number) then ">100" else "") otherwise
if [NEQ]="NAS" then "Some String" else
if [NEQ] = "" then "Empty String" else
"Other String"

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlWK1YlWMjSCUAaGYNrPMRhMg4nElLQUpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NEQ = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each try (if Number.FromText([NEQ])<=10 then "<=10" else
if Number.FromText([NEQ])<=100 then "<=100" else
if Value.Is(Number.FromText([NEQ]), type number) then ">100" else "") otherwise
if [NEQ]="NAS" then "Some String" else
if [NEQ] = "" then "Empty String" else
"Other String")
in
    #"Added Custom"

View solution in original post

@Vijay_A_Verma , thank you, it does the trick.

First time I see a "otherwise" in an "if" statement.  Researched it but could not find anything.

Always thought that the structure is pretty linear, meaning that if it meets the FIRST criteria, it exit with the "then".

If not, it looks at the SECOND (else if). criteria and so fourth...

If none of the X previous criteria was meet then it returns the final else "value by default".

Why do we need the "otherwise"??? 

View solution in original post

3 REPLIES 3
Vijay_A_Verma
Super User
Super User

Number.IsNaN is used to test the presence of 0/0. I believe this is not needed. Use below in a custom column

= try (if Number.FromText([NEQ])<=10 then "<=10" else
if Number.FromText([NEQ])<=100 then "<=100" else
if Value.Is(Number.FromText([NEQ]), type number) then ">100" else "") otherwise
if [NEQ]="NAS" then "Some String" else
if [NEQ] = "" then "Empty String" else
"Other String"

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlWK1YlWMjSCUAaGYNrPMRhMg4nElLQUpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NEQ = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each try (if Number.FromText([NEQ])<=10 then "<=10" else
if Number.FromText([NEQ])<=100 then "<=100" else
if Value.Is(Number.FromText([NEQ]), type number) then ">100" else "") otherwise
if [NEQ]="NAS" then "Some String" else
if [NEQ] = "" then "Empty String" else
"Other String")
in
    #"Added Custom"

@Vijay_A_Verma , thank you, it does the trick.

First time I see a "otherwise" in an "if" statement.  Researched it but could not find anything.

Always thought that the structure is pretty linear, meaning that if it meets the FIRST criteria, it exit with the "then".

If not, it looks at the SECOND (else if). criteria and so fourth...

If none of the X previous criteria was meet then it returns the final else "value by default".

Why do we need the "otherwise"??? 

This is try otherwise statement - This is to handle error. (Error handling is described here - https://docs.microsoft.com/en-us/powerquery-m/m-spec-error-handling  )

Since I am using Number.FromText and if you field is not number, it will generate error. It will mean that this field is text. Hence due to otherwise, it will reach to following statement

if [NEQ]="NAS" then "Some String" else

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.

Top Solution Authors