Forum Discussion

MrLidums's avatar
MrLidums
Frequent Visitor
4 years ago
Solved

Extract id from the string

Hi.

 

Problem

I need to split the String into separate parts. Usually, I would use the built-in Split Column function.

However, in this case, I have data (String) with this pattern: [text1]_[text 2]_..._[text n]_[id]

The problem is, that the [id] part, which I need to extract, may contain the underscore symbol "_". This makes using Split Column useless. 

 

I have a list of these ids (from a different source), that I could probably use in a function like Text.Contains to at least identify which exactly id is in the particular String. But I'm assuming this would involve some sort of iteration (looping) through all the ids in the list. Maybe I overthink myself and there is a more simple solution 🙂 (I did google and search in the form, with no luck)

 

What's the goal

I need each String ([text1]_[text 2]_..._[text n]_[id]) to be divided into 2 columns:

  • [text1]_[text 2]_..._[text n]
  • [id]

Any ideas or suggestions are welcome. A solution would amazing.

Thank you!

  • If the [id] can context letters and underscores, then it could theoretically contain "_all_", so splitting on that might not be the best approach, especially if not all of the names end with that.

     

    It looks like your [id] always has 8 characters, so why not split on that?

    Table.SplitColumn(Source, "Name with id", Splitter.SplitTextByPositions({0, 8}, true), {"Name", "Id"})

     

5 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    I need to see some sample data to work on this. If you could post some sample, that would be great....

    • MrLidums's avatar
      MrLidums
      Frequent Visitor

      Hi Vijay_A_Verma ! 

      thank you for the quick response. 

      Sure, I can post some samples. Here you go. Let me know if you prefer them in any other form 🙂 

      I made in bold ids that are messing up the auto spliter function.

       

      Original dataResult data 
      Name with idNameId
      30days-b2b-visitors_dynamic_all_4msRKgXG30days-b2b-visitors_dynamic_all4msRKgXG
      30days-b2b-visitors_dynamic_all_CUSz2R9Z30days-b2b-visitors_dynamic_allCUSz2R9Z
      30days-b2b-visitors_dynamic_all_DG2dv5KS30days-b2b-visitors_dynamic_allDG2dv5KS
      30days-b2b-visitors_dynamic_all_di_a87Xu30days-b2b-visitors_dynamic_alldi_a87Xu
      30days-b2b-visitors_dynamic_all_iHNKaES430days-b2b-visitors_dynamic_alliHNKaES4
      30days-b2b-visitors_dynamic_all_iw4w0y9m30days-b2b-visitors_dynamic_alliw4w0y9m
      30days-b2b-visitors_dynamic_all_jlOadkrD30days-b2b-visitors_dynamic_alljlOadkrD
      30days-b2b-visitors_dynamic_all_mI7sbsrB30days-b2b-visitors_dynamic_allmI7sbsrB
      30days-b2b-visitors_dynamic_all_MiaI2oPB30days-b2b-visitors_dynamic_allMiaI2oPB
      30days-b2b-visitors_dynamic_all_oaxdVgA-30days-b2b-visitors_dynamic_alloaxdVgA-
      30days-b2b-visitors_dynamic_all_pYxvtQil30days-b2b-visitors_dynamic_allpYxvtQil
      30days-b2b-visitors_dynamic_all_qBY3_LcL30days-b2b-visitors_dynamic_allqBY3_LcL
      30days-b2b-visitors_dynamic_all_tYT6m4Av30days-b2b-visitors_dynamic_alltYT6m4Av
      30days-b2b-visitors_dynamic_all_YXEQ30lJ30days-b2b-visitors_dynamic_allYXEQ30lJ
      30days-other-visitors_dynamic_all__yul4_AK30days-other-visitors_dynamic_all_yul4_AK
      30days-other-visitors_dynamic_all_2IAdRAYx30days-other-visitors_dynamic_all2IAdRAYx
      30days-other-visitors_dynamic_all_3scLSEik30days-other-visitors_dynamic_all3scLSEik
      30days-other-visitors_dynamic_all_AfRikImf30days-other-visitors_dynamic_allAfRikImf
      30days-other-visitors_dynamic_all_avE_2pSx30days-other-visitors_dynamic_allavE_2pSx
      30days-other-visitors_dynamic_all_-cZjFwRO30days-other-visitors_dynamic_all-cZjFwRO
      30days-other-visitors_dynamic_all_D5EHFa1g30days-other-visitors_dynamic_allD5EHFa1g
      30days-other-visitors_dynamic_all_fYoPYPcB30days-other-visitors_dynamic_allfYoPYPcB
      30days-other-visitors_dynamic_all_O7uWwGlO30days-other-visitors_dynamic_allO7uWwGlO
      30days-other-visitors_dynamic_all_oZUyuovU30days-other-visitors_dynamic_alloZUyuovU
      30days-registered_dynamic_all_ACcSriEU30days-registered_dynamic_allACcSriEU
      30days-registered_dynamic_all_h2fFvAJA30days-registered_dynamic_allh2fFvAJA
      30days-registered_dynamic_all_mQnRQMsM30days-registered_dynamic_allmQnRQMsM
      30days-registered_dynamic_all_MsZwitSX30days-registered_dynamic_allMsZwitSX
      30days-registered_dynamic_all_OKN0OpCZ30days-registered_dynamic_allOKN0OpCZ
      30days-registered_dynamic_all_SfV92llr30days-registered_dynamic_allSfV92llr
      30days-registered_dynamic_all_u2nFal9430days-registered_dynamic_allu2nFal94
      30days-registered_dynamic_all_UJCjwOMQ30days-registered_dynamic_allUJCjwOMQ
      30days-registered_dynamic_all_yTj51eb530days-registered_dynamic_allyTj51eb5
      • AlexisOlson's avatar
        AlexisOlson
        Super User

        If the [id] can context letters and underscores, then it could theoretically contain "_all_", so splitting on that might not be the best approach, especially if not all of the names end with that.

         

        It looks like your [id] always has 8 characters, so why not split on that?

        Table.SplitColumn(Source, "Name with id", Splitter.SplitTextByPositions({0, 8}, true), {"Name", "Id"})

         

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    If this is the pattern, then custom split with delimiter "_all_" (without quotes).

    Now, you will get ID in second result column.

    In first result column, you can concatenate "_all" again.

    To see the working - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZISaws1k0yStItyyzOLMkvKo5PqcxLzM1Mjk/MyYk3yS0O8k6PcFeK1SGsuNAp0jjeJ9lHKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Name with ID" = _t]),
        #"Split Column by Delimiter" = Table.SplitColumn(Source, "Name with ID", Splitter.SplitTextByDelimiter("_all_", QuoteStyle.Csv), {"Name with ID.1", "Name with ID.2"}),
        #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Name", each [Name with ID.1]&"_all"),
        #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Name with ID.2", "ID"}}),
        #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Name with ID.1"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Name", "ID"})
    in
        #"Reordered Columns"