Forum Discussion
Mcode: Custom Column- Evaluate IF Text Column has Number Value
- 5 years ago
Hello Anonymous
you can add a custom column with this formula (you have to replace "Column1" with your real column name)
if List.AllTrue(List.Transform(Text.ToList([Column1]), each try Value.Is(Number.From(_), type number) otherwise false)) then "numeric" else "alpha numeric"Here the complete code example
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjRUitWJVjI2BlNRUWDKACIIlXN2hshBeJaWECUGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if List.AllTrue(List.Transform(Text.ToList([Column1]), each try Value.Is(Number.From(_), type number) otherwise false)) then "numeric" else "alpha numeric") in #"Added Custom"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
Anonymousyou can do it in this way
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjBQitWJVnJ0BFPGxmCqqgoiaKgUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each try Number.From([Column1]) otherwise -0.1),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom]<>-0.1 then "number" else "alphanumeric")
in
#"Added Custom1"Hello smpa01 ,
I tried your approach and I am getting Token EoF expected error on below formula.
I highlighted the closing parentheses in red with the error. Did I missed something in my syntax?
let
Source = Table.FromRows(#"Table Master Valid"), let_t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Data Type" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{#"Data Type" , type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each try Number.From([#"Data Type" ]) otherwise -0.1),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom]<>-0.1 then "number" else "alphanumeric")
in
#"Added Custom1"