Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Power Query let Keyword: How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The let keyword in Excel Power Query is how you build a self-contained block of steps that produces a single value or table. You use it to define intermediate variables, keep a query readable, and control exactly what gets returned with an in clause. If you have ever opened the Advanced Editor and seen let ... in ..., that is the whole idea: define names, then say which name is the result. Nothing needs to be installed beyond Excel with Power Query (available in Excel 2016 and later, and in Microsoft 365). The steps below use the Advanced Editor, which is the same on Windows and Mac, though menu labels can differ slightly by version and region.

What You'll Need

  • Excel with Power Query (Excel 2016 or later, or Microsoft 365).
  • A query to edit, or a blank query you can paste a formula into.
  • Basic familiarity with the Power Query Editor and the Advanced Editor (Home > Advanced Editor).

Step-by-Step

  1. Open the Power Query Editor. Select any cell in your data and go to Data > From Table/Range, or use Data > Get Data to connect to a source. The Power Query Editor opens in a separate window.
  2. Open the Advanced Editor. On the Home tab, click Advanced Editor. You will see the full M code for the query, typically starting with let and ending with in.
  3. Understand the skeleton. A let expression lists one or more assignments separated by commas, each in the form StepName = expression . After the last comma, the in keyword names the step whose value the query returns. For example: let Filtered = Table.SelectRows(Source, each [Amount] > 100) in Filtered
  4. Add your own variable. Place the cursor after the last step's comma and type a new name, an equals sign, and an expression. Names are case-sensitive and cannot contain spaces unless you wrap them in #"...". For example, Total = List.Sum(Filtered[Amount]).
  5. Reference earlier steps. Inside the let block, any name defined above can be used by later steps. Order matters: a step can only refer to names already defined before it.
  6. Set the output with in. After the final comma, write in on its own line and then the name of the step you want to return. If you want the filtered table, put Filtered; if you want the total, put Total. Only one name can follow in.
  7. Click Done, then check the result. The editor validates the syntax. If the preview shows the expected table or value, close and load with Home > Close & Load. If you see an error, read the message and check commas, brackets, and the name after in.
  8. Use let inside a custom column or function. You can also write a let ... in ... expression inside a custom column formula or a custom function, which is useful for multi-step calculations that would otherwise be one long nested expression.

Common Mistakes

  • Missing or extra commas. Every assignment except the last must end with a comma. The step named after in must not have a trailing comma.
  • Referring to a step defined later. M is not a forward reference; a name must appear before it is used.
  • Case mismatches. Filtered and filtered are different names.
  • Spaces in names. Use #"My Step" if you must include spaces or special characters.
  • Returning the wrong name after in. It is easy to leave the default last step in place after adding a new final step.
  • Forgetting that let is an expression, not a statement. It must produce a value; you cannot put arbitrary statements inside it.

Tips

  • Keep step names descriptive and consistent, such as Source, ChangedType, FilteredRows.
  • Use the Advanced Editor sparingly for small edits; the ribbon steps still generate valid let code you can inspect.
  • To debug, temporarily set in to an earlier step to see its output.
  • Comment with // for a single line or /* ... */ for a block to explain non-obvious logic.
  • If a calculation is reused, define it once as a variable rather than repeating the expression.

Troubleshooting

If the Advanced Editor refuses to close, the error message usually points to the line with the problem. Check the token before the highlighted position: a missing comma, an unclosed bracket, or a name that does not exist. If the query loads but returns the wrong shape, confirm the name after in. If a step works alone but fails when combined, test it by moving in upward one step at a time. If the preview is slow, reduce the number of rows early with a filter or Table.FirstN while you debug. When an error mentions a field that cannot be found, verify the column name in the step before it, since column names are case-sensitive in M.

Frequently Asked Questions

What does let actually do in Power Query?

It creates a block of named expressions evaluated in order and returns the one named after in. It is the standard structure of every query.

Can I have more than one in?

No. A single let expression has exactly one in clause naming one result.

Do I have to use let?

No. Simple queries can be written as a single expression, but the editor generates let blocks by default because they are easier to read and edit.

Why does my step name need #"..."?

Names with spaces or special characters must be quoted that way. Plain names without spaces do not need it.

Can I use let inside a custom function?

Yes. A function body can be a let ... in ... expression, which is useful for multi-step logic.

Does let change my data source?

No. It only structures the query logic; the source connection and credentials are unchanged.