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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
BenScargill
Regular Visitor

How do I extract a specific text with a specific text length?

On Power BI, I am trying to extract a 5 digit number from a column with text and numbers, image below. I can extract the numbers from the column using 'Text.Select([Column Name], {"0".."9"})' but I'm not sure how to say it has to be 5 digits in length. I have seen the code, 'each (Text.Length(_) = 5', but this shows an Error in the new column.

 

I want to be able to use the below formula but it shows as an error. How can I make this work?

 

Text.Select([Column Name], {"0".."9"}, each (Text.Length(_) = 5)

 

Any suggestions?

 

$RXK4H4I.PNG

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I am right that you are a beginner with MCode!?

 

Don't thouch the "x" in this code:

= let 
x = Text.Select([Cross hire for Contract], {"0".."9"})
in
if Text.Length(x)=5 then x else null

 

Andreas.

View solution in original post

16 REPLIES 16
AlienSx
Super User
Super User

let
    Source = your_table,
    nums = "0123456789",
    func = (txt) => 
        [cln = List.Transform(Text.ToList(txt), (x) => if Text.Contains(nums, x) then x else " "),
        t = Text.Combine(cln),
        split = Splitter.SplitTextByWhitespace()(t), 
        combine = Text.Combine(List.Select(split, (x) => Text.Length(x) = 5), ", ")][combine],
    add_col = Table.AddColumn(Source, "5 digits", each func([Cross hire for Contract]))
in
    add_col
Anonymous
Not applicable

Hi all,

 

The insights of the super users are very good. Here I would like to share a blog that is about executing regular expressions with JavaScript in Power Query Editor.

In this blog, you will be shown how to use regular expressions to get strings in a specific format from a column and I think for this post is a solution.

Blog: Explore Web.Page in power query: Execute regular e... - Microsoft Fabric Community

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Anonymous
Not applicable

Regular expressions with Java Script are min. 100x but mostly 1000x times slower as an complex MCode!

 

Andreas.

Anonymous
Not applicable

Even easier, just append a space to your text column, And then no matter where you're five digits are, you can use Text.BetweenDelimiters with "Cross Hire for Contract" and " " as your delimiters.

 

 

--Nate

Of course he can do it this way, but it will not extract numbers in case there is a missing space in text like "Cross hire for contract25698"


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Anonymous
Not applicable

True, but the could first use the replace values function from the GUI to replace "contract" with "contract ", and since that might cause double spaces, use the replace values function to replace "  " with " ".  Now you can use Text.BetweenDelimiters with "Cross Hire for Contract " and " " as your delimiters.

 

--Nate

Anonymous
Not applicable

Add a custom column:

Andreas_Killer_0-1723731111607.png

= let 
numbers = Text.Select([Test], {"0".."9"})
in
if Text.Length(numbers)=5 then numbers else null

 

 

BenScargill_0-1723731832936.png

 

Thank you for your suggestion. However, this does not work as the new column doesn't exist yet.

 

Can I add a new blank column before and name it?

Anonymous
Not applicable

The issue is up to you, why do you change "numbers" to "Contract Numbers"?

 

Andreas.

Hi Andreas,

 

I tried this but there was an error, see below image...

 

BenScargill_0-1723732967727.png

 

Anonymous
Not applicable

I am right that you are a beginner with MCode!?

 

Don't thouch the "x" in this code:

= let 
x = Text.Select([Cross hire for Contract], {"0".."9"})
in
if Text.Length(x)=5 then x else null

 

Andreas.

Anonymous
Not applicable

I can't tell from that sample size, but if all of your values are at the end of the string, you can just use Text.End([ColumnName], 5).

 

--Nate

Hi watkinnc,

 

Unfortunately, the digits are not always at the end of the string. The numbers are always after 'Cross Hire for Contract text'. But the other values are random.

 

Hope this helps.

dufoq3
Super User
Super User

Hi @BenScargill, check this:

 

Output

dufoq3_0-1723730799885.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcxBC4JAFATgvzLsOYQ219RjBnUM6iYeFnvqwurGe1vgv2+xa7dhmPnaVjUcRHB1TBgCowlLZNtH6Kowpep2rbq9uZ+skKDGmbz7EK9IDY8k2+BHTH+IXFfbQsNFmqXGKXhPeDkfIu52oLjiEsbRJ7zxZDnLsu3wIInY60NukMTC5McSdnmmUBjVdV8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cross hire for Contract" = _t]),
    Ad_Extract = Table.AddColumn(Source, "Extract", each 
        [ a = {"0".."9"},
          b = Splitter.SplitTextByCharacterTransition((x)=> not List.Contains(a, x), a)([Cross hire for Contract]),
          c = List.Transform(b, (y)=> Splitter.SplitTextByCharacterTransition(a, (x)=> not List.Contains(a, x))(y)),
          d = List.Select(List.Combine(c), (x)=> (try Number.From(x) otherwise false) is number and Text.Length(Text.Trim(x)) = 5),
          e = Text.Combine(d, ", ")
        ][e], type text)
in
    Ad_Extract

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi dufoq3,

 

Thank you for your message.

 

Is there not a simpler way of doing this code? I'm very new to Power Query and Power BI so I do not understand what this means.

 

How can I correct the below code when adding a Custom Column...

 

Text.Select([Column Name], {"0".."9"}, each (Text.Length(_) = 5)

If you use Text.Select it will extract each number - for example if you have text: "ABC 12 CD 34 EFG 5" - it will extract "12345" and match your condition which is not correct. Just use my query and it will work as you wish. If you don't know ho to use my query - read note below my posts.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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