Forum Discussion

campelliann's avatar
campelliann
Post Patron
6 years ago
Solved

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!

 

 

  • v-alq-msft's avatar
    v-alq-msft
    6 years ago

    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:

     

    Best Regards

    Allan

     

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

5 Replies

  • mahoneypat's avatar
    mahoneypat
    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

  • 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"
  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, campelliann 

     

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

     

    Table:

     

    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:

     

    Best Regards

    Allan

     

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

     

    • campelliann's avatar
      campelliann
      Post Patron

      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

      • v-alq-msft's avatar
        v-alq-msft
        Community Support

        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:

         

        Best Regards

        Allan

         

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