Skip to content

Custom functions

This page shows how to turn a query into a custom function, so that the same steps can be run on any number of inputs.

A query often takes real effort to build, with many steps tuned to the quirks of one file. When other files share the same layout, they need the same steps.

Turning the query into a function means the steps are written once and then called for each file, which applies them identically every time. There is nothing to copy, and nothing to keep in step by hand when the logic changes.

Building a function takes three stages:

  1. Build a query that works on one file.
  2. Turn that query into a function.
  3. Call the function from another query.

The example here starts from a query that imports a text file called Varying Header Rows.txt, using M to keep only the rows that matter even when the number of header rows changes. The aim now is to apply the same logic to other files.

The single-file query is built on another page:

Filtering rows with M

The same folder now holds further files with the same layout. Each needs the same transformations, and the results should end up in one table.

Turning a query into a function involves three edits:

  1. Choose a name for a variable that will hold the value that changes from one run to the next.
  2. Add the following text at the very start of the query:
(variable_name) =>
  1. Find the value in the query that should change, and replace it with the variable name.

Give the variable a name that says what it holds, because that makes the code easier to read later. The name should not contain spaces or special characters.

Here the value that changes is the file path, so the variable is called filepath:

  • Open the Varying Header Rows query, and on the Home tab choose Advanced Editor.
  • Put the cursor immediately before the word let.
  • Type the following:
(filepath)=>
  • Press Enter.

The code now begins like this:

The Advanced Editor for the Varying Header Rows query, with (filepath) => on the first line above let, and the Source step still holding a fixed path starting C:\Users

Next, replace the fixed file path with the variable:

  • Find the full file path in the line that starts with Source.
  • Select the path, but not the quotation marks on either side of it.
  • Press Ctrl+C to copy the path, because it is needed again for the test.
  • Press Delete, and then delete the two quotation marks as well.
  • Type filepath in their place.

The code now reads like this:

The Advanced Editor with File.Contents(filepath) in the Source step in place of the fixed path

Click Done. The query becomes a function, and the editor shows a box for entering its parameter instead of a table.

The Power Query Editor showing an Enter Parameter box for filepath (optional) with Invoke and Clear buttons, and the signature function (filepath as any) as any

Check that the function still produces the same result:

  • Click Invoke.
  • Paste the full file path from your own machine, for example C:\Users\…\Varying Header Files\Varying Header Rows.txt.

When you click OK, Power Query reads the file, runs it through every step, and puts the result in a new query called Invoked Function.

Once the function works:

  • Rename the Varying Header Rows query to fnGetVaryingRows.
  • On the Home tab, choose Close & Load.

A function cannot be loaded to a worksheet, so the query is saved as a connection only, and nothing appears on the sheet.

The Queries & Connections pane listing fnGetVaryingRows as Connection only

Now use the function to combine all the files in the folder:

  • Create a new query from From File and then From Folder.
  • Browse to the folder that holds the Varying Header Rows files, and click OK.
  • In the window that lists the files, click Transform Data.

The folder preview listing Varying Header Rows.txt, Varying Header Rows 2.txt and Varying Header Rows 3.txt, with Combine & Transform Data, Transform Data and Cancel buttons

  • Select the Folder Path and Name columns, right-click, and choose Remove Other Columns.

Then add a column that calls the function for each file:

  • On the Add Column tab, click Custom Column.
  • Enter this formula:
=fnGetVaryingRows([Folder Path]&[Name])
  • Click OK.

Each row now holds a Table value, which is the cleaned contents of that file. Clicking beside any Table shows a preview of it.

The Varying Header Files query with Name, Folder Path and a Custom column of Table values, and below it a preview of one table with the columns ID Number, Amount, OrderDate, PreviousOrder and DaysSinceLastOrder

Next, remove the file details and combine the tables:

  • Remove the Folder Path and Name columns.
  • Click the expand button on the Custom column, clear the option to use the original column name as a prefix, and click OK.

Set the data type of each column:

  • ID Number: Whole Number
  • Amount: Whole Number
  • OrderDate: Date
  • PreviousOrder: Date
  • DaysSinceLastOrder: Whole Number

Finally, load the result:

  • On the Home tab, choose Close & Load.

A custom function lets a transformation be perfected on one file and then applied to as many others as needed, with the results appended into one table. When the logic needs to change, it changes in one place, and every file picks it up on the next refresh.

This example follows one in Ken Puls and Miguel Escobar’s M is for (Data) Monkey (Holy Macro! Books).