Skip to content

Instantly share code, notes, and snippets.

@halbuki
Last active March 10, 2026 20:49
Show Gist options
  • Select an option

  • Save halbuki/c676f0fbf482aac6a1fb845fdddfef88 to your computer and use it in GitHub Desktop.

Select an option

Save halbuki/c676f0fbf482aac6a1fb845fdddfef88 to your computer and use it in GitHub Desktop.
Excel Lambda functions for Power Query Replacer functions
/* USE NAMESPACE "Replacer" */
ReplaceText = LAMBDA(_text, _old, _new,
LET(
Help, TRIM(TEXTSPLIT(
"DESCRIPTION:->Replaces all occurrences of _old with _new in _text\nVERSION:->1.0\nPARAMETERS:->_text as text, _old as text, _new as text",
"->",
"\n"
)),
ErrorMessages, {"_text is not text."; "_old is not text."; "_new is not text."},
_err1, IF(NOT(ISTEXT(_text)), #VALUE!, FALSE()),
_err2, IF(NOT(ISTEXT(_old)), #VALUE!, FALSE()),
_err3, IF(NOT(ISTEXT(_new)), #VALUE!, FALSE()),
ErrorArray, VSTACK(ISERROR(_err1), ISERROR(_err2), ISERROR(_err3)),
Messages, FILTER(ErrorMessages, ErrorArray, ""),
Result, SUBSTITUTE(_text, _old, _new),
Error, IF(OR(ErrorArray), 3, IF(OR(ISERROR(Result)), 2, 1)),
CHOOSE(Error, Result, Help, Messages)
)
);
ReplaceValue = LAMBDA(_value, _old, _new,
LET(
Help, TRIM(TEXTSPLIT(
"DESCRIPTION:->Replaces _value if it equals _old with _new, otherwise returns _value\nVERSION:->1.0\nPARAMETERS:->_value as any, _old as any, _new as any",
"->",
"\n"
)),
ErrorMessages, {""},
ErrorArray, {},
Messages, {},
Result, IF(_value = _old, _new, _value),
Error, IF(OR(ErrorArray), 3, IF(OR(ISERROR(Result)), 2, 1)),
CHOOSE(Error, Result, Help, Messages)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment