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.

Advanced 20 min read

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

  1. In the editor choose Home › Manage Parameters › New Parameter.
  2. Name: ReportFolder. Description: optional, shown as a tooltip.
  3. Tick Required. Type: Text. Suggested Values: Any value (or a list of allowed values).
  4. Current Value: C:\Reports\Sales\Monthly. Click OK.
  5. 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.
Power Query M
// 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)
A parameter is just a query returning a value, marked with meta information so the UI treats it as a parameter.

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.

Note. Parameters made with Manage Parameters live inside the editor, so users must open Power Query to change them. For values that business users should control, read them from a worksheet cell instead, as shown next.

Parameters from a worksheet cell

Create a small settings area on a sheet called Settings:

AB
1SettingValue
2Report folderC:\Reports\Sales\Monthly
3Year2026
4VAT rate0.24

A settings table on a worksheet; each value cell gets its own name.

  1. Click cell B2 (the folder path) and type pFolder in the Name Box left of the formula bar, then press Enter. Name B3 as pYear and B4 as pVat.
  2. Click B2 and 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.)
  3. 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.
  4. Rename the query FolderPath, choose Close & Load To… › Only Create Connection.
  5. Repeat for the year and VAT rate, or write one query by hand as below.
Power Query M
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]
One query called Settings returns a record. Other queries use Settings[Folder], Settings[Year] and Settings[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.

Watch out. Combining a workbook cell with an external source in the same query can trigger the Formula.Firewall error described in Getting data: files, folders, tables, web pages and databases. Keep the setting in its own query, as above, and give the workbook and the folder compatible privacy levels in Data Source Settings (for example both Organizational). If the error remains on a file that only you use, the per-workbook option to ignore privacy levels is the last resort.

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.

  1. Create a parameter pFile (Text) with the path of one sample file.
  2. Build a query CleanStatement from that file: Data › Get Data › From File › From Text/CSV, then change the Source step to use pFile. Add your cleaning steps.
  3. Right-click CleanStatement in the Queries pane and choose Create Function…. Name it fnCleanStatement.
  4. Power Query creates a function that takes pFile as 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:

Power Query M
(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
    NoZero
fnCleanStatement: a function with a typed parameter and a typed result. Its body is an ordinary let expression.

Invoking 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:

AB
1BranchFile
2AthensC:\Bank\athens_2026-05.csv
3PortoC:\Bank\porto_2026-05.csv
4Riga\\fileserver\finance\riga_2026-05.csv

Before: a table listing the statement file of each branch.

  1. Load the list with Data › From Table/Range.
  2. Choose Add Column › Invoke Custom Function. New column name Data, Function query fnCleanStatement, filePath set to the column File.
  3. Click OK. Each row gets a table. Click the expand icon on Data to stack them, keeping Branch as a label.
ABCD
1BranchDateDescriptionAmount
2Athens2026-05-02CARD SUPPLIES-120.4
3Athens2026-05-04CUSTOMER TRANSFER3200
4Porto2026-05-03RENT-1450
5Riga2026-05-06CUSTOMER TRANSFER870

After: all branch statements cleaned by the same function and stacked, each row tagged with its branch.

Power Query M
= Table.AddColumn(#"Changed Type", "Data", each fnCleanStatement([File]))
Invoke Custom Function writes a normal custom column that calls the function with the row's File value.

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:

Power Query M
// 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
    Result
Returns the category of the first keyword found in the description, or Other. Call it with each fnCategory([Description]).
Tip. A function that reads a table, like Keywords 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.

Note. Parameters, Create Function and Invoke Custom Function are available in Power Query for Windows and in current Microsoft 365 builds on the Mac. A function whose source is a local file path still depends on that path existing on the machine that refreshes.

Check yourself

1. What is the main advantage of reading a parameter from a worksheet cell?

2. After right-click › Drill Down on the only value of a one-cell query, what does the query return?

3. What does Create Function… need in the source query?

4. Which command calls a function for each row of a table?

5. What is the function header for a function with a required text input that returns a table?