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.
Why use custom functions
Section titled “Why use custom functions”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 custom function
Section titled “Building a custom function”Building a function takes three stages:
- Build a query that works on one file.
- Turn that query into a function.
- 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.
1. Build the query for one file
Section titled “1. Build the query for one file”The single-file query is built on another page:
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.
2. Turn the query into a function
Section titled “2. Turn the query into a function”Turning a query into a function involves three edits:
- Choose a name for a variable that will hold the value that changes from one run to the next.
- Add the following text at the very start of the query:
(variable_name) =>- 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:

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
filepathin their place.
The code now reads like this:

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

3. Test the function
Section titled “3. Test the function”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.

4. Call the function on every file
Section titled “4. Call the function on every file”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.

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

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.
5. Summary
Section titled “5. Summary”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).