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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Trebor84
Helper II
Helper II

Extract first occurring number set from text string

Hi, I'm trying to extract the first group of numbers from a text string in Power Query editor . Have seen a few tips on the this but problem is that the strings don't have any set pattern.

 

Please see examples below.

 

hello1234.  1234

hello 123    123

12 hello 21  12

 

Anu suggestions please? I have a way to do this in VBA but have been asked to recreate in Power Query now. 

Thanks 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

You can use below in a custom column

 

 

try Number.From(Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c))(Text.RemoveRange([Text],0,Text.PositionOfAny([Text],{"0".."9"}))){0}) otherwise null

 

Edit - another construct can be

Number.From(Text.Split(Text.Trim(Text.Combine(List.Transform(Text.ToList([Text]), (x)=> if not List.Contains({"0".."9"},x) then " " else x)))," "){0})

 

View solution in original post

3 REPLIES 3
Syndicate_Admin
Administrator
Administrator

Hi,

 

Please try the below...

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Table", type text}}),
NumbersOnly = Table.TransformColumns(#"Changed Type", {{"Table", each Text.Select( _ , {"0".."9", " "})}}),
#"Trimmed Text" = Table.TransformColumns(NumbersOnly,{{"Table", Text.Trim, type text}}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Trimmed Text", {{"Table", each Text.BeforeDelimiter(_, " "), type text}})
in
#"Extracted Text Before Delimiter"

 

Basically extracting all numbers and spaces from your column (called mine Table), then only keeping numbers before the first space which I think will give you what you need.

 

Thanks,

Vijay_A_Verma
Super User
Super User

You can use below in a custom column

 

 

try Number.From(Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c))(Text.RemoveRange([Text],0,Text.PositionOfAny([Text],{"0".."9"}))){0}) otherwise null

 

Edit - another construct can be

Number.From(Text.Split(Text.Trim(Text.Combine(List.Transform(Text.ToList([Text]), (x)=> if not List.Contains({"0".."9"},x) then " " else x)))," "){0})

 

Thanks, all of these work great. I went with the top solution.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors