Power Query functions let you turn a one-off cleanup into a reusable step you can apply to every file, table, or refresh in a workbook or report. A function in Power Query is just a saved expression that takes one or more inputs, does something with them, and returns a result — for example, a custom function that takes a raw date column and returns the fiscal quarter, or one that takes a text field and returns a cleaned phone number. You can write functions by hand in the formula bar or the Advanced Editor, or let Power Query generate one for you by converting an existing query into a function. Before you start, you need a workbook with Power Query available (Excel 2016 or later on Windows, Excel for Microsoft 365, or Power BI Desktop) and at least one query that already produces the transformation you want to reuse. Power Query is not available in Excel for the web, so do this work in the desktop app.
What You'll Need
- Excel for Microsoft 365 or Excel 2016+ on Windows, or Power BI Desktop. On Mac, Power Query is limited and some function-authoring features behave differently, so these steps assume desktop Windows or Power BI Desktop.
- At least one working query in the Power Query Editor (Data tab > Get Data, or a table loaded via Data > From Table/Range).
- A clear idea of the input your function will accept and the output you want, such as "one text column in, one cleaned text value out."
- Familiarity with the formula bar and the Advanced Editor (Home tab > Advanced Editor).
Menu labels can differ slightly by version, device, and region — if a label you see does not match exactly, look for the closest equivalent on the same ribbon tab.
Step-by-Step
- Open the Power Query Editor. Select your data, then choose Data > From Table/Range (or use Get Data for an external source). The Power Query Editor opens in its own window.
- Build the transformation you want to reuse as a normal query first. For example, load a sample table and add a custom column that trims spaces and changes case: Home tab > Add Column > Custom Column, then enter an expression such as . Confirm the result looks right on the sample data before you turn it into a function.
- Decide the input parameter. Your function needs one or more named inputs. In the query above, the input is the column value being cleaned. Note what type it should be: text, number, date, and so on. Getting the type right up front prevents most later errors.
- Create the function from the working query. Right-click the query in the Queries pane and choose Create Function (in some versions this appears under a context menu or the Home/Transform ribbon). Power Query prompts you for a function name and a parameter name. Use a descriptive name such as
CleanName, and a clear parameter likeinputText. When you accept, the query becomes a function query, shown in the Queries pane with a function icon. - Check the generated code. Open the Advanced Editor and look for a line starting with
letand ending in a=>arrow, followed by the result expression. The part before the arrow lists your parameters; the part after is the body. For a single parameter it looks roughly like(inputText as text) => let ... in .... Theas textis a type hint — keep it if you want Power Query to reject wrong input types. - Test the function. In the Queries pane, right-click the function query and choose Invoke. Enter a sample value such as a name with extra spaces and mixed case, and check the preview returns the cleaned result. Repeat with an empty string and a null to see how it behaves.
- Use the function in a real query. Go back to your data query, add a custom column, and call the function by name: . Alternatively, in the Add Column tab use Invoke Custom Function to pick the function and map a column to its parameter. Each row now passes through the same logic.
- Apply it to every file in a folder (optional). If you are combining files, apply the function transformation inside the sample-file query used by the folder combine, so the transformation is applied to every file rather than just one.
- Save and refresh. Close and Load your queries, then refresh. Confirm the results match your manual checks, and adjust the function code in the Advanced Editor if not.
Common Mistakes
The most frequent problem is forgetting the parameter in the function body — if the code references a column name like instead of the parameter name, the function will fail or return the wrong value when applied to other columns. Another common error is mixing types: passing a number into a parameter declared as text throws an error, so either change the type hint or convert the value first. People also create functions from queries that still contain source-specific steps (a hard-coded file path or sheet name), which makes the function unusable elsewhere. Strip or parameterize those steps. Finally, naming a function the same as a built-in function causes confusing references; use a distinctive name such as fnCleanName .
Tips
Start with a plain query and only convert to a function once the logic works — debugging inside a function is harder. Give parameters meaningful names, because they appear in the code and in error messages. Use type hints in the parameter list to catch bad inputs early. Keep each function small and single-purpose, then chain them: one function trims text, another validates a date, a third formats output. Document threshold values and assumptions in a comment using // inside the Advanced Editor. If you will reuse a function across many workbooks, keep it in a Power Query template and import that template rather than rebuilding it each time.
Troubleshooting
If that did not work: first check the Advanced Editor for syntax errors and unbalanced parentheses or quotes. Next, verify the parameter names in the body match exactly, including case. If you see a type mismatch, confirm the input column's type matches the parameter's type hint. If the function preview works but the applied column fails, look for null or empty values the body does not handle — wrap the logic in a conditional that checks for null. For refresh errors, use the error message in the query's applied steps pane; it usually names the step and the value that failed. If the function query no longer refreshes after an update, reopen the Advanced Editor and recheck the code rather than deleting the function.
Frequently Asked Questions
Can I write a Power Query function without the Advanced Editor?
Yes, for simple cases. Create Function generates the syntax for you from a working query. Hand-writing or editing the code still happens in the Advanced Editor, where you can see parameters, types, and the body.
Can a Power Query function take more than one input?
Yes. Add extra parameter names in the function declaration separated by commas, and reference each one in the body. Each parameter can have its own type hint.
Why does my function fail only after I apply it to another table?
Usually a type or null problem: the new column may contain values the function body was not written to handle. Add null checks and confirm column types match the parameter types.
Does a function refresh automatically when data changes?
Yes. A function is logic, not data, so once it is used in a query it re-runs on refresh against whatever the source contains. No manual step is needed beyond refreshing the workbook.
Can I share a function with someone else?
Yes, by sharing the workbook or by placing the function in a Power Query template and importing it. Functions are stored in the workbook's queries, so recipients need a compatible version of Excel or Power BI Desktop.
Are custom functions the same as the built-in ones?
They are used the same way once defined, but custom functions live in your workbook, not in Power Query itself, and are only available where the workbook or template containing them is open.