Forum Discussion
ovetteabejuela
9 years agoImpactful Individual
PowerQuery | Extract Numbers from A string (eg ABCD1234)
Hi Community, In this example, FirstName12345. How can I extract 12345 from it using M? Assume that the number length varies (eg abc123, abc1234567, abc1234, etc...) [Edit] Spelling ...
- Anonymous9 years ago
HI ovetteabejuela,
You can refer to below formula to if it suitable for your requirement.
Logic: split text to character list, select the number part and merge them to text.
Functions comment.
Text.ToList: split text to character list.
Values.Is: check value type.
List.Transform: transform list from original list.
List.RemoveNulls: remove replaced null value.
Text.Combine: merge character list to text.Formula:
#"Added Custom" = Table.AddColumn(#"Promoted Headers", "Custom", each Text.Combine(List.RemoveNulls(List.Transform(Text.ToList([Name]),each if Value.Is(Value.FromText(_), type number) then _ else null))))
Full query:
let Source = Excel.Workbook(File.Contents("C:\Users\xxxxxx\Desktop\test.xlsx"), null, true), Sheet3_Sheet = Source{[Item="Sheet3",Kind="Sheet"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Sheet3_Sheet,{{"Column1", type text}}), #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]), #"Added Custom" = Table.AddColumn(#"Promoted Headers", "Custom", each Text.Combine(List.RemoveNulls(List.Transform(Text.ToList([Name]),each if Value.Is(Value.FromText(_), type number) then _ else null)))) in #"Added Custom"Regards,
Xiaoxin Sheng
jazz
7 years agoAdvocate I
Hello,
a simple one step answer to this question:
OneStep = Table.AddColumn(Source, "MyNumberColumn", each Text.Select([MyStringColumn], {"0".."9"}))
alice2k
3 years agoNew Member
Hi, jazz; I was wondering how this might work with decimals! I tried your code, and it works, but it strips out the decimal (as a string).