Search notes:

Power Query M formula language: let expression

The most simple let expression

Arguably, the most simple let expression is
let in 42
Github repository about-Power-Query-Formula-M, path: /language/expressions/let/simple.M
When evaluated in Excel, for example with this VBA code, this formula evaluates to:

Using a variable list

It's possible to define variables in between the let and the in keywords. The value of these variables can then be used in the part after the in.
let
   variable_1      = 18,
   unused_variable = 99,
   variable_2      = 24
in
   variable_1 + variable_2
Github repository about-Power-Query-Formula-M, path: /language/expressions/let/variable-list.M
This code produces the same result as the «most simple let expression» above.

Automatic resolving of dependencies in the variable list

When a let expression is evaluated, the dependencies between variables in the variable list are resolved automatically. In the following example, bar is assigned first, then baz followed by foo calculated, resulting in 42.
let
   foo = 2 * baz,
   bar = 7,
   baz = 3 * bar
in
   foo
Github repository about-Power-Query-Formula-M, path: /language/expressions/let/variable-dependencies.M
Compare with automatic resolving of dependencies in records.

Nested let expressions

let expressions can be nested. The following expression results in 6:
let
   one   = let
              a = -2,
              b =  3
           in
              a + b,

   two   = let
              c =  5,
              d =  3
           in
              c - d,

   three = let
              e = 1
           in
              e + two

in
   one + two + three
Github repository about-Power-Query-Formula-M, path: /language/expressions/let/nested.M

Index