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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MichaelJSchulz
Frequent Visitor

Extract last sequence of numerical value from string

I have a process that works but it is rather inelegant. I'm sure there must be a better way to do this.

 

In a certain column, the last several characters might be numbers. If the length of a number sequence is four, five, or six characters then I need to extract that sequence into a new column. If the length of final sequence numbers is less than four characters, then nothing should be extracted. There may be numbers at the beginning of the string and/or in the middle but only the numerical characters at the end of the string are of interest.

 

There is not a constant delimiter. There may or may not be a space between the last alpha character and the first numerical character. There might or might not be a symbol chacacter, such as a colon or a dash.

 

ColumnA                                     NewColumn

TEXT TEXT TEXT 123456             123456

TEXT TEXT TEXT TEXT 1234        1234

TEXT TEXT TEXT12345                 12345

TEXT 123 TEXT TEXT-123456       123456

1234 TEXT TEXT TEXT:12345        12345

TEXT TEXT TEXT TEXT 123            null

1234 TEXT TEXT TEXT 1                null

TEXT TEXT 123456 TEXT               null

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

You may use below in a custom column

 

= if Text.Select(Text.End([ColumnA],6),{"0".."9"})=Text.End([ColumnA],6) then Text.End([ColumnA],6) 
else if Text.Select(Text.End([ColumnA],5),{"0".."9"})=Text.End([ColumnA],5) then Text.End([ColumnA],5)
else if Text.Select(Text.End([ColumnA],4),{"0".."9"})=Text.End([ColumnA],4) then Text.End([ColumnA],4)
else null

 

EDIT - Another alternative construct is this which is more flexible

 

=[a=List.LastN(List.Transform(Text.ToList([ColumnA]),(x)=>if Text.Contains(Text.Combine({"0".."9"}),x) then x else null), each _ <> null),
b=if List.Count(a)<4 then null else Text.Combine(a)][b]

 

 

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Super User
Super User

You may use below in a custom column

 

= if Text.Select(Text.End([ColumnA],6),{"0".."9"})=Text.End([ColumnA],6) then Text.End([ColumnA],6) 
else if Text.Select(Text.End([ColumnA],5),{"0".."9"})=Text.End([ColumnA],5) then Text.End([ColumnA],5)
else if Text.Select(Text.End([ColumnA],4),{"0".."9"})=Text.End([ColumnA],4) then Text.End([ColumnA],4)
else null

 

EDIT - Another alternative construct is this which is more flexible

 

=[a=List.LastN(List.Transform(Text.ToList([ColumnA]),(x)=>if Text.Contains(Text.Combine({"0".."9"}),x) then x else null), each _ <> null),
b=if List.Count(a)<4 then null else Text.Combine(a)][b]

 

 

That's fantastic. Thank you!

I understand how the first approach works but I'm having trouble wrapping my mind around the second approach.

Would you explain how it works?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors