Power Query course · Lesson 14 of 17
Parameters and custom functions
Make queries flexible with parameters from Manage Parameters or worksheet cells, turn a query into a function, and call it on every row or file.
A query that hard-codes C:\Users\maria\Reports\Sales works on Maria's laptop and nowhere else. A query that filters Year = 2026 must be edited every January. Parameters move such values out of the steps into one visible place, ideally a cell the user can change without opening the editor. Custom functions go one step further: they package a whole recipe so it can be applied to many inputs, such as every file in a folder or every row of a table. You have already used one without knowing it: the Transform File helper from Combine files from a folder is a custom function.
Power Query parameters
- In the editor choose Home › Manage Parameters › New Parameter.
- Name:
ReportFolder. Description: optional, shown as a tooltip. - Tick Required. Type: Text. Suggested Values: Any value (or a list of allowed values).
- Current Value:
C:\Reports\Sales\Monthly. Click OK. - The parameter appears in the Queries pane with its own icon. Now open the folder query, click the gear next to Source, and in the path box switch the small ABC button to Parameter and choose
ReportFolder.
// The parameter query (shown in the Advanced Editor)
"C:\Reports\Sales\Monthly" meta [IsParameterQuery = true, Type = "Text", IsParameterQueryRequired = true]
// The Source step of the folder query now reads
Source = Folder.Files(ReportFolder)Many dialogs accept a parameter wherever you see the ABC 123 button: filter values, Replace Values, Conditional Column values, the file path of a source and the server name of a database. To change the value, click the parameter in the Queries pane and edit Current Value, or choose Home › Manage Parameters.
Parameters from a worksheet cell
Create a small settings area on a sheet called Settings:
| A | B | |
|---|---|---|
| 1 | Setting | Value |
| 2 | Report folder | C:\Reports\Sales\Monthly |
| 3 | Year | 2026 |
| 4 | VAT rate | 0.24 |
A settings table on a worksheet; each value cell gets its own name.
- Click cell
B2(the folder path) and typepFolderin the Name Box left of the formula bar, then press Enter. NameB3aspYearandB4aspVat. - Click
B2and choose Data › From Table/Range. Because the cell is a named range, Power Query does not ask to create a table; it imports the named range. (If it insists on a table, untick My table has headers and accept, then rename the table.) - In the editor the query shows one column,
Column1, with the path. Right-click the value cell and choose Drill Down. The result is a single text value. - Rename the query
FolderPath, choose Close & Load To… › Only Create Connection. - Repeat for the year and VAT rate, or write one query by hand as below.
let
GetSetting = (name as text) => Excel.CurrentWorkbook(){[Name = name]}[Content]{0}[Column1],
Folder = GetSetting("pFolder"),
Year = GetSetting("pYear"),
Vat = GetSetting("pVat")
in
[Folder = Folder, Year = Year, Vat = Vat]In the sales query, replace the hard-coded path with Settings[Folder] and the hard-coded year filter with each Date.Year([Date]) = Settings[Year]. A user now changes the year in B3 and clicks Data › Refresh All.
Turning a query into a function
Suppose each branch sends a bank-style CSV that needs the same five cleaning steps, and you want to run them on several files that are not in one folder. Build the steps once for one file, with the file path as a parameter, then convert the query into a function.
- Create a parameter
pFile(Text) with the path of one sample file. - Build a query
CleanStatementfrom that file: Data › Get Data › From File › From Text/CSV, then change the Source step to usepFile. Add your cleaning steps. - Right-click
CleanStatementin the Queries pane and choose Create Function…. Name itfnCleanStatement. - Power Query creates a function that takes
pFileas input and runs all the steps. The original query stays as a template: edit it, and the function follows.
You can also write a function directly. Open a blank query's Advanced Editor and wrap the steps in a function header:
(filePath as text) as table =>
let
Source = Csv.Document(File.Contents(filePath), [Delimiter = ";", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(Promoted, {{"Date", type date}, {"Amount", type number}}, "de-DE"),
NoZero = Table.SelectRows(Typed, each [Amount] <> 0)
in
NoZeroInvoking a function
Click the function in the Queries pane to see its parameter form. Type a value and click Invoke to test it: Power Query creates a new query with the result. To call it for every row of a table, use Add Column › Invoke Custom Function:
| A | B | |
|---|---|---|
| 1 | Branch | File |
| 2 | Athens | C:\Bank\athens_2026-05.csv |
| 3 | Porto | C:\Bank\porto_2026-05.csv |
| 4 | Riga | \\fileserver\finance\riga_2026-05.csv |
Before: a table listing the statement file of each branch.
- Load the list with Data › From Table/Range.
- Choose Add Column › Invoke Custom Function. New column name
Data, Function queryfnCleanStatement, filePath set to the columnFile. - Click OK. Each row gets a table. Click the expand icon on
Datato stack them, keepingBranchas a label.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Branch | Date | Description | Amount |
| 2 | Athens | 2026-05-02 | CARD SUPPLIES | -120.4 |
| 3 | Athens | 2026-05-04 | CUSTOMER TRANSFER | 3200 |
| 4 | Porto | 2026-05-03 | RENT | -1450 |
| 5 | Riga | 2026-05-06 | CUSTOMER TRANSFER | 870 |
After: all branch statements cleaned by the same function and stacked, each row tagged with its branch.
= Table.AddColumn(#"Changed Type", "Data", each fnCleanStatement([File]))Small helper functions
Functions do not have to return tables. Short helpers keep custom columns readable and make rules reusable across queries. For example, the keyword categoriser promised in Custom and conditional columns, driven by a user-maintained table Keywords with columns Keyword and Category:
// Query fnCategory
(description as nullable text) as text =>
let
Rules = Excel.CurrentWorkbook(){[Name = "Keywords"]}[Content],
Upper = Text.Upper(description ?? ""),
Hits = Table.SelectRows(Rules, each Text.Contains(Upper, Text.Upper([Keyword]))),
Result = if Table.IsEmpty(Hits) then "Other" else Hits{0}[Category]
in
ResultKeywords above, reads it again for every row it is called on. For a few thousand rows that is fine. For large tables, load the rules once in the calling query (wrapped in Table.Buffer) and pass them to the function as a second parameter. See Performance, query folding and refresh options.Optional parameters and documentation
Parameters can be optional: (amount as number, optional rate as nullable number) => amount * (1 + (rate ?? 0.24)). Callers may omit rate, which then defaults to 24%. Keep function names consistent (a prefix like fn helps), and use the query's Properties (right-click › Properties) to add a description that colleagues will see when hovering over it.
Check yourself
1. What is the main advantage of reading a parameter from a worksheet cell?
A named cell is visible and editable in the workbook.
2. After right-click › Drill Down on the only value of a one-cell query, what does the query return?
Drill Down navigates from the table to the cell's value.
3. What does Create Function… need in the source query?
The parameter becomes the function's input.
4. Which command calls a function for each row of a table?
Invoke Custom Function adds a column with the function's result per row.
5. What is the function header for a function with a required text input that returns a table?
M functions are written (parameters) => body, with optional type annotations.