Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
I made the custom function below as I need to run the same function for 15+ columns, basically to replace colA with colB if it isn't null, otherwise to retain the value of colA:
let updateField = (dt as table, colA as text, colB as text) =>
let Source =
Table.ReplaceValue(dt, each colA, each if colB <> null then colB else colA, Replacer.ReplaceValue, {colA})
in Source
in updateField
Unfortunately, when I invoked the custom function in my query, it didn't work. However, if I use the actual Table.ReplaceValue in a step, it works just fine. Is there some way I can make this function work? I figure what's causing it is because colA and colB are text parameters, but I don't think I can pass a column as a parameter.
You would need to wrap that in an Evaluate.Expression to allow running text as M-Code. See this example
Also, Table.ReplaceValue is really just passing (in_value, look_for, replace_with) parameters to any function. You could write yours as:
Table.ReplaceValue(
table
, each _ColA
, each _ColB
, (value, old, new) => old ?? new
, {ColA}
)
The `??` operator is M-Code's null-coalesce
It needs an Evaluate.Expression wrap to allow running text as M-Code. See this example
Also, Table.ReplaceValue is simply passing three arguments - (in_value, look_for, replace_with) - to any function. Replacer.ReplaceValue is just a function that is used in the documentation, without really explaining how that works.
So, try this:
Evaluate.Expression(
"Table.ReplaceValue(
table
, each _" & ColA & "
, each _" & ColB & "
, (value, value_in_ColA, value_in_ColB) => value_in_ColA ?? value_in_ColB
, {" & ColA & "}
)"
,[Table.ReplaceValue = Table.ReplaceValue]
)
* - `??` operator is M-Code's null-coalesce
Hi @olimilo ,
I think you need to reference the Table.ReplaceValue() function once for each column you want to replace.
Best regards,
Lionel Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
84 | |
75 | |
68 | |
41 | |
35 |
User | Count |
---|---|
102 | |
56 | |
52 | |
46 | |
40 |