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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
adamcoffey15
New Member

Extract Multiple Occurrences of Text Between Delimiters

I have one column of text that has various keywords or phrases that are separated by "~~" before and after each word/phrase. I need to exact the text that is contained between this custom delimiter every time that it occurs (Note: Text.BetweenDelimiters only extracts the text from the 1st occurrence instead of extracting it from each occurrence). 

 

I need to concatenate each keyword/phrase that was extracted and separate them with a ";" delimiter. 

 

The below image shows an example of the text input and the expected output that I need.

 

How can I accomplish this in Power Query?

 

Screen Shot 2022-12-30 at 11.22.02 AM.png

1 ACCEPTED SOLUTION

  • List.Accumulate to create each delimited substring
    • Count the number of delimiters
    • Create a list and extract every other number (the start index of each delimiter pair
  • Note that my data comes from an Excel table. Change the Source= line to reflect your actual data source.
let
    Source = Excel.CurrentWorkbook(){[Name="Table26"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

Delim = "~~",

    Extract = Table.AddColumn(#"Changed Type", "Text Between ~~", each 
        Text.Combine(
            List.Accumulate(
                List.Alternate(List.Numbers(0, List.Count(Text.Split([Column1], "~~"))-1),1,1,1),
                {},
                (state, current)=>
                    state & {Text.BetweenDelimiters([Column1],Delim,Delim,current)}),"; "), type text)
in
    Extract

 

ronrsnfld_0-1672454311873.png

 

 

View solution in original post

11 REPLIES 11
AntrikshSharma
Community Champion
Community Champion

@adamcoffey15 Basesd on the sample shared you can use this:

let
    Source = Table.FromRows (
        Json.Document (
            Binary.Decompress (
                Binary.FromText (
                    "i45WCsnILFaoqytOzStJzUtOratTAPKzSotLFBLzgOKpFYm5BTkg4fw0hUSFktSKEoXMvILSEj2l2FgA",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
        in
            type table [ Text = _t ]
    ),
    AddedCustom = 
        Table.AddColumn (
            Source,
            "Output",
            each Text.Combine (
                List.RemoveItems (
                    List.Transform (
                        Text.Split ( [Text], " " ),
                        each Text.BetweenDelimiters ( _, "~~", "~~" )
                    ),
                    { "" }
                ),
                "; "
            ),
            type text
        )
in
    AddedCustom

 

@AntrikshSharma 

This is very close but I am not only extracting single words every time. I am also extracting whole phrases so I think splitting the text at each space is causing errors for me. 

 

Here is a new example that shows how both words and phrases need to be extracted. How can we change your code to fix this?

 

Screen Shot 2022-12-30 at 4.00.50 PM.png

  • List.Accumulate to create each delimited substring
    • Count the number of delimiters
    • Create a list and extract every other number (the start index of each delimiter pair
  • Note that my data comes from an Excel table. Change the Source= line to reflect your actual data source.
let
    Source = Excel.CurrentWorkbook(){[Name="Table26"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

Delim = "~~",

    Extract = Table.AddColumn(#"Changed Type", "Text Between ~~", each 
        Text.Combine(
            List.Accumulate(
                List.Alternate(List.Numbers(0, List.Count(Text.Split([Column1], "~~"))-1),1,1,1),
                {},
                (state, current)=>
                    state & {Text.BetweenDelimiters([Column1],Delim,Delim,current)}),"; "), type text)
in
    Extract

 

ronrsnfld_0-1672454311873.png

 

 

Hi All,

 

This is much easier:

List.RemoveFirstN(List.Transform(Text.Split(YOUR_TEXT_VARIABLE, "START_DELIM"), each Text.BeforeDelimiter(_, "END_DELIM")), 1)

 

 

This does seem much simplier. One issue is it captures the first value infront of the start delimiter. Any suggestions? 

 

Goal: Capture all numeric values, sum and convert to decimal (not present in code yet)

 

Code: 

Table.AddColumn(#"Removed Other Columns1", "Custom", each List.Transform(Text.Split([#"FTE`s required"], "["), each Text.BeforeDelimiter(_, "%")))


Field Value: 

AUTO1[100%],MOT1[100%],MOT2[25%],OPS1[25%]

 

Desired Result:

{100,100,25,25}

 

Current Result

{Auto1,100,100,25,25}

 

End state desired result - Attempting to use List.Sum with additional LEN logic to insert decimal but can't get there until text value is removed.

2.50

Hey, nearly there! Don't forget the List.RemoveFirstN at the start. That will get rid of the first element.

How do we accomplish this from a calculated table column?

That's a different question. Suggest you post it as such so as to attract some DAX experts.

@ronrsnfld Thank you so much! This worked perfectly!

adamcoffey15
New Member

Hi @ImkeF , thanks for your help. I did actually see this post previously but I wasn't able to figure out where or how to convert your text explanations into something that works using my own values. I saw that you have a .pbix file linked but I am only running power query in excel on a Mac so I am not able to open the file. 

 

Do you think you'd be able to copy and paste your code from your file here so I can see how this works from a functioning example?

ImkeF
Super User
Super User

Hi @adamcoffey15 ,
I've created a function for that a while ago: Return all occurrences of text between delimiters in Power BI and Power Query – The BIccountant
Please let me know if that works for you.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors