Forum Discussion
andris_
8 years agoResolver I
Power Query: remove all text between delimiters
Hey all, I'm creating a report, which has a SharePoint list datasource. It has like ~5 columns, and it contains memorandums about meetings. There is a description column, which contains the memos...
- 8 years ago
you can use recursion in PowerQuery
crate a blank query and paste this code into editor, then call this function on your HTML column(txt as text) => [ fnRemoveFirstTag = (HTML as text)=> let OpeningTag = Text.PositionOf(HTML,"<"), ClosingTag = Text.PositionOf(HTML,">"), Output = if OpeningTag = -1 then HTML else Text.RemoveRange(HTML,OpeningTag,ClosingTag-OpeningTag+1) in Output, fnRemoveHTMLTags = (y as text)=> if fnRemoveFirstTag(y) = y then y else @fnRemoveHTMLTags(fnRemoveFirstTag(y)), Output = @fnRemoveHTMLTags(txt) ][Output]EDIT - typo in syntax
Anonymous
4 years agoNot applicable
There is an even simpler solution.
You can create a new function called fun_ReplaceTextBetweenDelimiters, and in it add this code 👇
let
fun_ReplaceTextBetweenDelimiters = (Text as text, StartDelimiter as text, EndDelimiter as text, optional ReplaceDelimiters as nullable logical, optional NewText as nullable text, optional TrimResult as nullable logical, optional FixDoubleSpaces as nullable logical) as text =>
let
// Add Default Parameters
Default_ReplaceDelimiters = if ReplaceDelimiters is null then true else ReplaceDelimiters,
Default_NewText = if NewText is null then "" else NewText,
Default_TrimResult = if TrimResult is null then true else TrimResult,
Default_FixDoubleSpaces = if FixDoubleSpaces is null then true else FixDoubleSpaces,
//Do work
TextBetweenDelimiters = Text.BetweenDelimiters(Text, StartDelimiter, EndDelimiter),
TextToReplace = if Default_ReplaceDelimiters then Text.Combine({StartDelimiter,TextBetweenDelimiters,EndDelimiter}) else TextBetweenDelimiters,
ReplacedText = Text.Replace(Text, TextToReplace, Default_NewText),
//Clean Result
TrimmedText = if Default_TrimResult then Text.Trim(ReplacedText) else ReplacedText,
FixedSpaces = if Default_FixDoubleSpaces then Text.Replace(TrimmedText, " ", " ") else TrimmedText
in
FixedSpaces
in
fun_ReplaceTextBetweenDelimitersThen, we can test it like this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtAw1FTSAbGUYnWilVzd3BU0jEAiQBZYJCIyCqhGRwEsCOQoxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TestData = _t, TargetData = _t]),
ChangeType = Table.TransformColumnTypes(Source,{{"TestData", type text}, {"TargetData", type text}}),
RunFunction = Table.AddColumn(ChangeType, "NewText", each fun_ReplaceTextBetweenDelimiters([TestData], "(", ")", true), type text),
TestResult = Table.AddColumn(RunFunction, "Test", each [TargetData]=[NewText], type logical)
in
TestResultInput:
| TestData | TargetData |
| ABC (1) | ABC |
| EFG (2) | EFG |
| XYZ (1, 2) | XYZ |
Output:
| TestData | TargetData | NewText | Test |
| ABC (1) | ABC | ABC | TRUE |
| EFG (2) | EFG | EFG | TRUE |
| XYZ (1, 2) | XYZ | XYZ | TRUE |
lawrence_sproul
1 year agoNew Member
This is great for flexibility and expands o nthe request in valueable way, but it only gets the first occurrence. Could you add recursion like in the accepted answer?