<!-- https://learnsql.cpetoday.com/functions/m/let -->
# let

Language: Power Query M
Category: Language
Syntax: let name = expression, name2 = expression2 in result
Summary: Names a series of steps so that each one can be read, and then gives the result in the in clause.
Returns: The value of the expression after in, which is the result of the query.
Level: Beginner
Related: each, if, try, type, Table.SelectRows

## What it does
Every query in Power Query is built with `let` and `in`. Between them you write steps: each step has a name, an equals sign and an expression. A later step can use any earlier one by name. The `in` clause says which step is the answer. The Applied Steps pane in the editor is a picture of these steps, in the order you wrote them.

## Syntax and arguments
- `name = expression`: a step. Separate steps with commas. The last step before `in` has no comma.
- `in result`: the name of the step to return.
- A step name with spaces is written with quotes and a hash, such as `#"Changed Type"`.

## Example
A query for the March bank file: read the file, drop the header lines and keep only the debits, with a named step for each action.

```powerquery
let
    Source = Csv.Document(File.Contents("C:\Banking\statement-2026-03.csv"), [Delimiter = ","]),
    Headers = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
    Debits = Table.SelectRows(Headers, each [Type] = "DEBIT"),
    #"Debits typed" = Table.TransformColumnTypes(Debits, {{"Amount", type number}})
in
    #"Debits typed"
```

Each name describes what that step does, so a reviewer can read the query from top to bottom. Changing the final `in` line changes which step is returned, which is a quick way to test an earlier step.

## Example: a second, different use
Using a step name that refers to an earlier step inside an `each` function is the same idea: names let you reuse a result without writing it twice.

## Things to watch
- A missing comma or an extra one is the most common error. The message usually points to the line above the one highlighted.
- Names are case sensitive. `Debits` and `debits` are different steps.
- The order of the steps does not change the result, because M works out which step depends on which. Still, write them in the order they run, so a reader can follow them.
- A step that nothing uses is never run, because M is lazy.

## In business terms
Named steps make a query something you can explain to an auditor: each step is a documented transformation. Keep the names short and meaningful, such as "Removed test rows" or "Converted to USD".
