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
campelliann
Post Patron
Post Patron

Extracting text IF these conditions are met (Power Query)

Hi,

 

I need my coleagues to fill a Project ID field, the problem is that there are many digits, and some people forget to put some zeros. E.g. P2019000000555 some people put P20190555, for example. (I need to do a kind of "sumif", of the work done in each ID.

 

To work around this, I am thinking on a simplified ID field, witch extracts the 5 first Characters, and last 4. This should solve the majority of my problems (although i am open to other ideas). Although I want to do this extraction (first 5, last 4) only when the ID starts with P2018, P2019, P2020 etc... More examples.

 

ID: P2019000000723 - transform to P20190723

ID: P20200000005233- transform to P20205233

ID: PAC1241 - stays the same

ID: MP2323 - stays the same.

 

Many thanks!

 

 

1 ACCEPTED SOLUTION

Hi, @campelliann 

 

You may try to add a custom column with following codes. The pbix file is attached in the end.

 

 

let 
len = Text.Length([ID]),id = [ID]
in
if Text.Contains("ID","P20") and len>=9 then
Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1)
else id)

 

 

Result:

a1.png

 

Best Regards

Allan

 

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

View solution in original post

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @campelliann 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

 

Table:

g1.png

 

You may add a custom column with following codes in 'Query Editor'.

let 
len = Text.Length([ID]),id = [ID]
in
if len>=9 then
Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1)
else id

 

Result:

g2.png

 

Best Regards

Allan

 

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

 

Hi Allan,

 

This almost works, the problem is that I have some text fields which lenght is >= 9, and I want them to stay the same.

 

Ist it possible, instead of using the condition len>=9,  if text contains "P20??".

 

Many thanks

Hi, @campelliann 

 

You may try to add a custom column with following codes. The pbix file is attached in the end.

 

 

let 
len = Text.Length([ID]),id = [ID]
in
if Text.Contains("ID","P20") and len>=9 then
Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1)
else id)

 

 

Result:

a1.png

 

Best Regards

Allan

 

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

lbendlin
Super User
Super User

Like this?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjAyMLQ0AANzI2OlWB2wkBFExMDUyBgq5uhsaGRiCGb7BhgZg5TGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.Length([Column1])>9 then Text.Start([Column1],5) & Text.End([Column1],4) else [Column1])
in
    #"Added Custom"
mahoneypat
Microsoft Employee
Microsoft Employee

In the query editor, just highlight that column and click on Replace Values in the ribbon.  Enter "000000" and "null" in the two fields in the popup (w/o the quotes), and hit ok.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.