March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
I have text in one column, which I want to replace in another column. I need to do this replacement for multiple columns so I created a custom function fxReplaceValue. Because I want to repeat this for various columns with different key words, I created a parameter for the column, e.g. [Summer]
let
Source = (qrySource) => let
Source = qrySource,
replaceSummer = Table.ReplaceValue(Source, each [Summer], each if Text.Contains([Value], "Summer", Comparer.OrdinalIgnoreCase) then [Value] else [Summer], Replacer.ReplaceValue,{"Summer"}),
replaceValSummer = Table.ReplaceValue(replaceSummer, each [Value], each if Text.Contains([Summer], "Summer", Comparer.OrdinalIgnoreCase) then "" else [Value], Replacer.ReplaceValue,{"Value"})
in
replaceValSummer
in
Source
What is the correct syntax to reference the desired column as a parameter?
let
Source = (qrySource, colReplacement) => let
Source = qrySource,
replaceSummer = Table.ReplaceValue(Source, each colReplacement, each if Text.Contains([Value], "Summer", Comparer.OrdinalIgnoreCase) then [Value] else colReplacement, Replacer.ReplaceValue,{colReplacement}),
replaceValSummer = Table.ReplaceValue(replaceSummer, each [Value], each if Text.Contains(colReplacement, "Summer", Comparer.OrdinalIgnoreCase) then "" else [Value], Replacer.ReplaceValue,{"Value"})
in
replaceValSummer
in
Source
Thank you for any advice!
Solved! Go to Solution.
let
Source = (qrySource,ColumnToCheck as text,Condition as text,ColumnToBeReplaced as text) => let
Source = qrySource,
replaceSummer = Table.ReplaceValue(Source, each Record.Field(_,ColumnToBeReplaced), each if Text.Contains(Record.Field(_,ColumnToCheck), Condition, Comparer.OrdinalIgnoreCase) then Record.Field(_,ColumnToCheck) else Record.Field(_,ColumnToBeReplaced), Replacer.ReplaceValue,{ColumnToBeRelaced}),
replaceValSummer = Table.ReplaceValue(replaceSummer, each Record.Field(_,ColumnToCheck), each if Text.Contains(Record.Field(_,ColumnToBeReplaced), Condition, Comparer.OrdinalIgnoreCase) then "" else Record.Field(_,ColumnToCheck), Replacer.ReplaceValue,{ColumnToCheck})
in
replaceValSummer
in
Source
regardless of efficiency, just amend your code.
I changed the function to append the [ColumnToBeReplaced] with the contents from [ColumnToCheck] if the [ColumnToBeReplaced] already has text.
let
Source = (qrySource, ColumnToCheck as text, Condition as text, ColumnToBeReplaced as text) => let
Source = qrySource,
replaceColValue = Table.ReplaceValue(Source, each Record.Field(_,ColumnToBeReplaced),
each if Text.Contains(Record.Field(_,ColumnToCheck), Condition, Comparer.OrdinalIgnoreCase) then
(if Record.Field(_,ColumnToBeReplaced)="" then Record.Field(_,ColumnToCheck)
else
Record.Field(_,ColumnToBeReplaced) & " " & Record.Field(_,ColumnToCheck))
else
Record.Field(_,ColumnToBeReplaced), Replacer.ReplaceValue,{ColumnToBeReplaced}),
eraseValue = Table.ReplaceValue(replaceColValue, each Record.Field(_,ColumnToCheck),
each if Text.Contains(Record.Field(_,ColumnToBeReplaced), Condition, Comparer.OrdinalIgnoreCase) then ""
else
Record.Field(_,ColumnToCheck), Replacer.ReplaceValue,{ColumnToCheck})
in
eraseValue
in
Source
let
Source = (qrySource,ColumnToCheck as text,Condition as text,ColumnToBeReplaced as text) => let
Source = qrySource,
replaceSummer = Table.ReplaceValue(Source, each Record.Field(_,ColumnToBeReplaced), each if Text.Contains(Record.Field(_,ColumnToCheck), Condition, Comparer.OrdinalIgnoreCase) then Record.Field(_,ColumnToCheck) else Record.Field(_,ColumnToBeReplaced), Replacer.ReplaceValue,{ColumnToBeRelaced}),
replaceValSummer = Table.ReplaceValue(replaceSummer, each Record.Field(_,ColumnToCheck), each if Text.Contains(Record.Field(_,ColumnToBeReplaced), Condition, Comparer.OrdinalIgnoreCase) then "" else Record.Field(_,ColumnToCheck), Replacer.ReplaceValue,{ColumnToCheck})
in
replaceValSummer
in
Source
regardless of efficiency, just amend your code.
I think I solved my own question by just changing the entries in the formula.
= fxReplaceValue(_NotesMech, "Summer")
Hi @MarkusEng1998,
Intersting, it should not really as you pass the parameter as text and intend to use it as column, unless relevant rows in the [Summer] column = "Summer". A correct way to address (if necessary) a column from text would be something like Record.FieldOrDefault(_, colReplacement), where you refer colReplacement as a column.
Cheers,
John
Hi @jbwtp , yes this formula worked, but did not return the desired results. I will try Daniels solution instead.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
34 | |
30 | |
20 | |
19 | |
12 |