Skip to content

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.

The Replace Values dialog, with one Value To Find box, one Replace With box and Advanced options

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 uses two tables.

The first, called Text_Table, holds sentences written in American spelling, in a column named Text Content.

The Text_Table in Excel, with an ID column and a Text Content column of sentences in American spelling, such as “She recognized that her favorite color was blue”

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.

The Translation_Table in Excel, with Old Text and New Text columns pairing American and British spellings, such as armor and armour, and color and colour

  • 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.

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
New

The 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.

  • 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
Checking

The function takes three parameters:

  • x is the table to work on.
  • n is a counter, which is the position of the current pair in the lists.
  • ColName is the name of the column in x whose text should be replaced.

The function has two steps:

  • Replace calls Table.ReplaceValue on the column named by ColName, replacing the item at position n in 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 n has 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
Checking
in
fxTranslate

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
Translation

When 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.

The loaded table, with the same IDs and the sentences now in British spelling, such as “She recognised that her favourite colour was blue”