Forum Discussion
M formula to check if text is a number and avoid errors
- 2 years ago
Hi mlsx4 ,
Thank you for your suggestion that could be a solution. My problem is that is not just a single if but multiple if inside if.
Meanwhile it seems that I found the solution that initially I've missed out (forgot): TRY... OTHERWISE...
So the following formula it seems it worked:
= Table.AddColumn(#"Added Custom", "Custom2", each if (try Value.Is(Number.FromText(Text.Start([TestColumn],4)), Int64.Type) otherwise false) then "Is number" else "Not number", type text)And now it seems I've got the expected result:
Kind Regads,
Lucian
Hi Lucian
It may not be the optimum solution but you can add intermediate steps:
1. Add a try to Number.FromText and write "Not number" if error
2. Then, create a conditional formula (if column = "Not number" then "Not number" else "Is number")
3. Remove extra columns not needed
let
Origen = Excel.Workbook(File.Contents("C:\ex.xlsx"), null, true),
Hoja1_Sheet = Origen{[Item="Sheet",Kind="Sheet"]}[Data],
#"Encabezados promovidos" = Table.PromoteHeaders(Hoja1_Sheet, [PromoteAllScalars=true]),
#"Tipo cambiado" = Table.TransformColumnTypes(#"Encabezados promovidos",{{"TestColumn", type text}}),
#"Primeros caracteres insertados" = Table.AddColumn(#"Tipo cambiado", "Primeros caracteres", each Text.Start([TestColumn], 4), type text),
#"Columnas con nombre cambiado" = Table.RenameColumns(#"Primeros caracteres insertados",{{"Primeros caracteres", "FirstChar"}}),
#"Personalizada agregada" = Table.AddColumn(#"Columnas con nombre cambiado", "IsNumber", each try Number.FromText([FirstChar]) otherwise "Not Number"),
#"Columna condicional agregada" = Table.AddColumn(#"Personalizada agregada", "isNumb", each if [IsNumber] <> "Not Number" then "Is Number" else "Not Number"),
#"Columnas quitadas" = Table.RemoveColumns(#"Columna condicional agregada",{"FirstChar", "IsNumber"})
in
#"Columnas quitadas"
- Lucian2 years ago
Responsive Resident
Hi mlsx4 ,
Thank you for your suggestion that could be a solution. My problem is that is not just a single if but multiple if inside if.
Meanwhile it seems that I found the solution that initially I've missed out (forgot): TRY... OTHERWISE...
So the following formula it seems it worked:
= Table.AddColumn(#"Added Custom", "Custom2", each if (try Value.Is(Number.FromText(Text.Start([TestColumn],4)), Int64.Type) otherwise false) then "Is number" else "Not number", type text)And now it seems I've got the expected result:
Kind Regads,
Lucian