Search notes:

Power Query M formula language standard library: Expression.Evaluate

The following nested Expression.Evaluate calls evaluate to 42 (= (9-3) * (5+2)):
Expression.Evaluate (
   Number.ToText(Expression.Evaluate(" 9 - 3 ")) & " * " &
   Number.ToText(Expression.Evaluate(" 5 + 2 "))
)
Github repository about-Power-Query-Formula-M, path: /standard-library/Expression/Evaluate/nested.M

Environment

The optional second parameter of Expression.Evaluate can be passed a record that contains values for names that are bound to the corresponding symbols in the expression that is evaluated.
The following example evaluates to 42:
Expression.Evaluate (
   " x + y ",
   [
     x = 17,
     y = 25
   ]
)
Github repository about-Power-Query-Formula-M, path: /standard-library/Expression/Evaluate/environment.M

Accessing global symbols such as the standard library

The #shared record comes in handy to pass the names of global symbols such as those of function names of the standard library to the expression to be evaluated.
Without passing #shared, the following document would produce an The name 'Number.Sin' doesn't exist in the current context error.
Expression.Evaluate (
  "Number.Sin(Number.PI / 6)",
  #shared
)
Github repository about-Power-Query-Formula-M, path: /standard-library/Expression/Evaluate/shared.M

Reusing functions stored in files

Expression.Evaluate can be used when PowerQuery functions are stored in files so that they can be re used in different projects.

One function

Imagine a file (here named add_nums.M) with the following (simple because of demonstration purposes) function:
(a, b) => a + b
This function can then be executed with the following expression:
let
   add_nums = Expression.Evaluate(Text.FromBinary(File.Contents("C:\Users\rene\PowerQuery\GlobalFunctions\add_nums.M")))
in
   add_nums(9, 33)

Multiple function

Multiple functions can be stored in a file using a record structure:
[
   mult = (a,b) => a * b,
   add  = (a,b) => a + b,
   sub  = (a,b) => a - b,
   div  = (a,b) => a / b
]
A function can then be selected from the record like so:
let
   funcs = Expression.Evaluate(Text.FromBinary(File.Contents("C:\Users\rene\PowerQuery\GlobalFunctions\funcs.M")))
in
   funcs[sub](61,19

See also

The Power Query standard library

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1759398274, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/Power-Query/M-formula/standard-library/Expression/Evaluate/index(106): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78