Recursive functions
A recursive function is one that calls itself. In M, a function defined as a step inside a query refers to itself by putting the @ sign in front of its own name.
One practical use is making many text replacements at once. Power Query’s Replace Values command finds and replaces one value at a time.

Fifteen replacements would therefore mean opening the dialog fifteen times and filling it in each time, which also leaves fifteen steps in Applied Steps. A recursive function can work through a whole list of replacements in one step, and the list can be kept in a worksheet where it is easy to extend.
The example
Section titled “The example”The example uses two tables.
The first, called Text_Table, holds sentences written in American spelling, in a column named Text Content.

The second, called Translation_Table, holds pairs of words: the text to find in an Old Text column, and the text to replace it with in a New Text column. Here each pair maps an American spelling to the British one.

1. Load both tables into Power Query
Section titled “1. Load both tables into Power Query”- Click any cell in the first table, and then click From Table/Range on the Data tab. Repeat for the second table.
- Give each query the same name as its table in Excel.
2. Start a new query
Section titled “2. Start a new query”Next, create a third query.
- In Excel, click Get Data, then From Other Sources, then Blank Query.
Start the query with this code:
let Source = Text_Table, Old = List.Buffer(Translation_Table[Old Text]), New = List.Buffer(Translation_Table[New Text]) in NewThe Source step loads the Text_Table query. The other two steps read the two columns of Translation_Table into lists and buffer them with List.Buffer, which holds each list in memory. The function reads these lists many times, so buffering avoids evaluating Translation_Table again on every pass.
3. Write the recursive function
Section titled “3. Write the recursive function”- Add a step to the query called fxTranslate, with this code:
fxTranslate = (x as table, n as number, ColName as text ) as table =>let Replace = Table.ReplaceValue(x, Old{n}, New{n}, Replacer.ReplaceText,{ColName}), Checking = if n = List.Count(Old)-1 then Replace else @fxTranslate(Replace, n+1, ColName )in CheckingThe function takes three parameters:
xis the table to work on.nis a counter, which is the position of the current pair in the lists.ColNameis the name of the column inxwhose text should be replaced.
The function has two steps:
- Replace calls Table.ReplaceValue on the column named by
ColName, replacing the item at positionnin the Old list with the item at the same position in the New list. - Checking decides whether to call the function again. The counter starts at 0, which is the first pair. If
nhas reached the last position in the Old list, which is one less than its count, the function returns the table as it stands. Otherwise it calls itself, through@fxTranslate(Replace, n+1, ColName), passing on the table with this replacement made and the counter moved on by one.
The query now reads as follows:
let Source = Text_Table, Old = List.Buffer(Translation_Table[Old Text]), New = List.Buffer(Translation_Table[New Text]), fxTranslate = (x as table, n as number, ColName as text ) as table => let Replace = Table.ReplaceValue(x, Old{n}, New{n}, Replacer.ReplaceText,{ColName}), Checking = if n = List.Count(Old)-1 then Replace else @fxTranslate(Replace, n+1, ColName ) in Checkingin fxTranslate4. Call the function
Section titled “4. Call the function”Add one more step that calls the function on the source table, starting at position 0 and working on the Text Content column:
let Source = Text_Table, Old = List.Buffer(Translation_Table[Old Text]), New = List.Buffer(Translation_Table[New Text]), fxTranslate = (x as table, n as number, ColName as text ) as table => let Replace = Table.ReplaceValue(x, Old{n}, New{n}, Replacer.ReplaceText,{ColName}), Checking = if n = List.Count(Old)-1 then Replace else @fxTranslate(Replace, n+1, ColName ) in Checking, Translation = fxTranslate (Source, 0, "Text Content")in TranslationWhen the query loads, every replacement in the translation table has been made. Adding a pair to Translation_Table and refreshing applies the new replacement too.
