Excel Power Query Formulas: A How-To Guide
In this article
Power Query formulas (technically called the M language) let you clean, transform and combine data in Excel in a repeatable way. You do not need to write them from scratch: the Power Query Editor records your clicks as formula steps, and you can edit or add custom formulas when the built-in buttons are not enough. To follow this guide you need a desktop version of Excel that includes Power Query (Excel for Microsoft 365, Excel 2021, Excel 2019, or Excel 2016 with the Power Query add-in enabled). The steps below are for the Windows desktop app; the Mac version has Power Query but a slightly different ribbon layout, and some connectors are not available there.
What You'll Need
- A desktop version of Excel with Power Query available (check the Data tab for the Get & Transform Data group).
- A data source such as a table in the same workbook, a CSV file, or a folder of files.
- Basic familiarity with Excel formulas helps, but Power Query uses its own syntax.
Step-by-Step
- Load your data into Power Query. Select a cell in your table, then go to Data > From Table/Range. For a file, use Data > Get Data > From File > From Text/CSV (or the folder option for multiple files). The Power Query Editor opens in a separate window. Menu labels can differ slightly by Excel version, so look for the closest match.
- Watch the Applied Steps pane. Every action you take (removing a column, filtering rows, changing a type) is recorded as a step on the right. Click any step to preview the data at that point. This is how Power Query formulas are built without typing.
- Open the formula bar. If it is hidden, go to View > Formula Bar in the Power Query Editor. Each step shows its underlying M formula, for example
= Table.SelectRows(Source, each [Sales] > 100). You can edit this text directly. - Add a custom column with a formula. Go to Add Column > Custom Column. In the dialog, give the column a name and type an M expression. For example, to combine two text columns use
[FirstName] & " " & [LastName]. To do arithmetic use[Price] * [Quantity]. Click OK. The new column appears and a new step is added. - Use the Advanced Editor for larger changes. Go to Home > Advanced Editor. This shows the full M script: a
letblock with named steps and aninstatement returning the final table. You can rename steps, reorder them, or insert new ones. Keep step names unique and reference earlier steps by name. - Change a column's data type with a formula. Select the column header, then use Transform > Data Type and pick the type. In the formula bar this appears as
Table.TransformColumnTypes. Correct types matter: text that looks like a number will not sum until it is typed as a number. - Handle errors and nulls. Use
try ... otherwise ...in a custom column to replace errors, for exampletry Number.From([Amount]) otherwise null. Useif [Value] = null then 0 else [Value]to substitute blanks. - Apply and load. When the preview looks right, go to Home > Close & Load > Close & Load To and choose a table or the Data Model. The query runs and the results appear in a worksheet table or in the Power Pivot data model.
- Refresh later. Right-click the loaded table and choose Refresh, or use Data > Refresh All. Power Query re-runs every step, so your formulas apply to new data automatically.
Common Mistakes
- Typing formulas in the wrong case for function names. M is case-sensitive:
Text.Upperworks,text.upperdoes not. - Referencing a step that comes later. Steps can only refer to earlier steps in the
letblock. - Forgetting the
eachkeyword. Row-level expressions in functions likeTable.SelectRowsandTable.AddColumnneedeachbefore the formula. - Leaving columns as Any type. This causes errors in later steps; set explicit types early.
- Editing the loaded table directly. Changes made to the output table are overwritten on refresh; make all changes in the query.
Tips
- Rename steps in the Applied Steps pane to something meaningful; this makes the Advanced Editor readable.
- Use the formula bar's autocomplete to discover function names instead of guessing.
- Test a formula on a small sample query before applying it to a large dataset.
- Keep a copy of the original source file; Power Query reads from it on every refresh.
- If a step breaks, click the step before it to confirm the data shape, then fix the formula.
Troubleshooting
If a custom column shows Error, click the error cell to see the message, then wrap the expression in try ... otherwise ... or correct the data type. If the query fails to refresh, check that the source file path or credentials are still valid under Data > Get Data > Data Source Settings. If the Advanced Editor shows a syntax error, check for missing commas, unmatched brackets, or a step name that does not exist. If results look wrong after a refresh, review the Applied Steps one at a time to find where the data diverges. For connection problems, sign out and back in to your Microsoft account, then retry.
Frequently Asked Questions
What language does Power Query use?
It uses the M formula language, which is different from standard Excel worksheet formulas.
Can I use regular Excel formulas in Power Query?
No. Power Query has its own function library; worksheet functions like VLOOKUP or SUMIF are not available inside the editor.
Do Power Query formulas update automatically?
They re-run when you refresh the query, either manually or on a schedule set in the workbook's connection properties.
Is Power Query available on Mac?
Yes, in current Excel for Mac, but some data connectors and features differ from Windows. Check the Data tab for the Get & Transform Data group.
Can I edit a query after loading it?
Yes. Select the loaded table, then go to Data > Queries & Connections, right-click the query and choose Edit to reopen the Power Query Editor.