PowerQuery - Text.BetweenDelimiters with StartDelimiter from list (ie. multiple)

%3CLINGO-SUB%20id%3D%22lingo-sub-1941554%22%20slang%3D%22en-US%22%3EPowerQuery%20-%20Text.BetweenDelimiters%20with%20StartDelimiter%20from%20list%20(ie.%20multiple)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1941554%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20split%20Text%20from%20a%20Column%20in%20to%20a%20seperate%20column%2C%20buth%20where%20they%20StartDelimiter%20can%20consist%20of%203-4%20different%20options%20due%20to%20differences%20in%20language.%20Is%20it%20possible%20to%20use%20a%20List%20for%20this%20purpose%3F%20Have%20tried%20to%20replicate%20a%20code%20used%20for%20List.AnyTrue%20but%20no%20luck.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20list%20is%20called%20Currency_Conversion_Delimiter%20and%20current%20code%20is%20as%20follows.%20No%20syntax%20error%20but%20doesnt%20work%20as%20expected%20(ie.%20if%20I%20change%20out%20the%20StartDelimiter%20with%20the%20only%20value%20in%20the%20list%20it%20works%2C%20but%20not%20via%20the%20below).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%22Inserted%20Text%20Between%20Delimiters%22%20%3D%20Table.AddColumn(%23%22Added%20Conditional%20Column%22%2C%20%22Text%20Between%20Delimiters%22%2C%20each%20List.Transform%20(Currency_Conversion_Delimiter%2C%20(StartDelimiter)%20%3D%26gt%3B%20Text.BetweenDelimiters(%5BDescription%5D%2C%20StartDelimiter%2C%20%22%20%22)%2C%20type%20text))%2C%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1941554%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1941913%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20-%20Text.BetweenDelimiters%20with%20StartDelimiter%20from%20list%20(ie.%20multiple)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1941913%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F883239%22%20target%3D%22_blank%22%3E%40JoakimHGjerde%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENot%20sure%20I%20understood%20you%20correctly%2C%20but%20if%20split%20column%20on%20few%20ones%20using%20list%20of%20delimiters%2C%20when%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3E%3DTable.SplitColumn(%0A%20%20%20prevStep%2C%0A%20%20%20%22Column%22%2C%0A%20%20%20Splitter.SplitTextByAnyDelimiter(%7B%22del1%22%2C%22del2%22%2C%22del3%22%7D%2C%20QuoteStyle.Csv)%2C%0A%20%20%20null%0A)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi all, 

I need to extract Text from a Column in to a seperate column, but where they StartDelimiter can consist of 3-4 different options due to differences in language. Is it possible to use a List for this purpose? Have tried to replicate a code used for List.AnyTrue but no luck. 

 

The list is called Currency_Conversion_Delimiter and current code is as follows. No syntax error but doesnt work as expected (ie. if I change out the StartDelimiter with the only value in the list it works, but not via the below).

 

    #"Inserted Text Between Delimiters" = Table.AddColumn(#"Added Conditional Column", "Text Between Delimiters", each List.Transform (Currency_Conversion_Delimiter, (StartDelimiter) => Text.BetweenDelimiters([Description], StartDelimiter, " "), type text)),

3 Replies

@JoakimHGjerde 

Not sure I understood you correctly, but if split column on few ones using list of delimiters, when

=Table.SplitColumn(
   prevStep,
   "Column",
   Splitter.SplitTextByAnyDelimiter({"del1","del2","del3"}, QuoteStyle.Csv),
   null
)

@Sergei Baklan 

 

Hi Sergei, 
Apologize, it is actually an extract I am trying to do (ie. extract the content between the delimiters). 

 

The StopDelimiter will always be the same, but the StartDelimiter may as an example be "belop" or "belob" due to the language differences. As such, I would like to create a list with these required delimiters, and use them as StartDelimiter in Text.BetweenDelimiters if possible, as the Delimiter will only ever occour once.

@JoakimHGjerde 

That's better with small sample, but let me try as I understood you. As a basis I'd take slightly modified function suggested by Ingeborg here Improved Text.SplitAny - function for Power BI and Power Query – The BIccountant

(string as text, separator as any) =>

let

    ListFunction = List.Accumulate(
        separator, 
        {string}, 
        (state, current) => 
        [   DoForEveryItemInTheList = List.Transform(state, each Text.Split(_, current)),
            FlattenNestedList = List.Combine(DoForEveryItemInTheList),
            RemoveEmpties = List.Select(FlattenNestedList, each _<>"" and _<>" ")
        ][RemoveEmpties]
    )
in
    ListFunction

Assuming any of "first" delimiters is always paired with "second" delimiter and end of the text we consider as "second" delimiter, transition from left to right here

image.png

could be done as

let
    firstDelims = {"ABC", "XYZ"},
    secondDelim = "/>",
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Custom" = Table.AddColumn(
        Source,
        "Custom",
        each
            List.Transform(
                List.FindText(
                    fnSplitText([A] & secondDelim,firstDelims),
                    secondDelim
                ),
                each
                    Text.Start(_, Text.PositionOf(_,secondDelim) )
            )
    ),
    NumberOfDelims = Table.AddColumn(
        #"Added Custom",
        "CountDelims",
        each List.Count([Custom])
    ),
    maxNumberOfDelims = List.Max(NumberOfDelims[CountDelims]),
    ColNames = List.Transform({1..maxNumberOfDelims}, each "Col " & Text.From(_) ),
    #"Extracted Values" = Table.TransformColumns(
        #"Added Custom",
        {"Custom",
         each
            Text.Combine(
                List.Transform(_, Text.From),
                ";"
            ),
         type text
        }
    ),
    #"Split Column by Delimiter" = Table.SplitColumn(
        #"Extracted Values",
        "Custom",
        Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv),
        ColNames),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"A"})
in
    #"Removed Columns"

This module also could be converted to function.