cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

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

Accepted Solutions
Highlighted
Community Support
Community Support

Re: Extracting text IF these conditions are met (Power Query)

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
Highlighted
Super User VI
Super User VI

Re: Extracting text IF these conditions are met (Power Query)

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? Please mark my post as a solution! Kudos are also appreciated!

Proud to be a Super User!




Highlighted
Super User V
Super User V

Re: Extracting text IF these conditions are met (Power Query)

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"
Highlighted
Community Support
Community Support

Re: Extracting text IF these conditions are met (Power Query)

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.

 

Highlighted
Frequent Visitor

Re: Extracting text IF these conditions are met (Power Query)

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

Highlighted
Community Support
Community Support

Re: Extracting text IF these conditions are met (Power Query)

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

Helpful resources

Announcements
Upcoming Events

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Upcoming Events

Community Summit North America – Join Online!

Join this community-driven Power Platform digital event for unbiased support and problem-solving.

Experience what’s next for Power BI

Join us for an in-depth look at the new Power BI features and capabilities at the free Microsoft Business Applications Launch Event.

Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Top Solution Authors
Top Kudoed Authors