0
votes

I want to remove any text between "( )" including the "( )". There are many difference instances where I can't simply find and replace. Small example:
ABC (1)
EFG (2)
XYZ (1, 2)
I wish to display
ABC
EFG
XYZ

Found this post, but the code for the function is no longer visible(at least on all the browsers I've tried). https://www.thebiccountant.com/2019/07/15/text-removebetweendelimiters-function-for-power-bi-and-power-query/

I copied the code from one of the comments and it seems to work fine, however when I invoke the function on the column I get all errors with the following: "Expression.Error: The specified index parameter is invalid. Details: List"

Does anyone have the code from the author? Or know what I'm doing wrong?

Here is the code from the new custom column after I run the function:

 Table.AddColumn(#"Changed Type1", "N", each Query1([#"NEC(s)"], "(", ")", 1, null))

Thanks

2
A different solution all together would be just fine too - Connor

2 Answers

1
votes

Here's a different solution that uses recursion.

(txt as text) =>
[ 
fnRemoveFirstTag = (DELIM as text)=>
    let
        OpeningTag = Text.PositionOf(DELIM,"("),
        ClosingTag = Text.PositionOf(DELIM,")"),
        Output = 
            if OpeningTag = -1 
            then DELIM 
            else Text.RemoveRange(DELIM,OpeningTag,ClosingTag-OpeningTag+1)
    in
        Output,
fnRemoveDELIM = (y as text)=>
    if fnRemoveFirstTag(y) = y
    then y 
    else @fnRemoveDELIM(fnRemoveFirstTag(y)),
Output = @fnRemoveDELIM(txt) 
][Output]

It works on your sample data, and should also work if there is more than one set of parentheses delimited substrings in your string.

enter image description here

Copied shamelessly and modified minimally from Power Query: remove all text between delimiters

1
votes

Is there text to the right of the )?

If not, just split column on custom delimiter ( leftmost, then remove the 2nd column

= Table.SplitColumn(Source, "Column1", Splitter.SplitTextByEachDelimiter({"("}, QuoteStyle.Csv, false), {"Column1.1", "Column1.2"})

OR transform the column to remove anything after the initial (

= Table.TransformColumns(Source,{{"Column1", each Text.Start(_,Text.PositionOf(_,"(")), type text}})

If text to the right of the ), try

= Table.TransformColumns(Source,{{"Column1", each Text.Start(,Text.PositionOf(,"("))&Text.End(,Text.Length()-Text.PositionOf(_,")")-1), type text}})