Power Query Value.Expression Function Guide
In this article
The Value.Expression function in Power Query (M language) is used to create a function from a text string. Essentially, it allows you to take a piece of text that looks like a formula and tell Power Query to treat that text as an executable expression. This is primarily used when you need to generate dynamic logic that cannot be hard-coded into a standard step, such as creating a custom calculation based on a value stored in a table cell or building a flexible API request parameter.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- Access to the Power Query Editor (Get Data > Launch Power Query Editor).
- A basic understanding of M language syntax (case sensitivity and comma usage).
- A data source (table or list) containing the text strings you intend to convert into expressions.
Step-by-Step
- Prepare Your Expression Source: Create a table in your data source that contains the logic you want to execute. For example, create a column named "FormulaText" where a cell contains a string like "([Price] * [Quantity]) * 1.1". Ensure the text is written exactly as it would appear in an M formula, including necessary brackets for column references.
- Load Data into Power Query: Open Power BI or Excel, go to the Data tab, and import this table into the Power Query Editor.
- Create a Custom Column: Navigate to the Add Column tab on the ribbon and select Custom Column. This will open the Custom Column dialog box.
- Enter the Value.Expression Syntax: In the Custom Column formula box, use the following structure:
Value.Expression([FormulaText]). Replace[FormulaText]with the actual name of the column containing your text strings. Note thatValue.Expressiondoes not execute the code immediately; it returns a function object. - Invoke the Generated Function: Because
Value.Expressionreturns a function, you cannot see the result yet. To get the actual value, you must invoke the function. In the same Custom Column dialog (or a subsequent one), wrap the expression to pass the current row as an argument:Record.Field(Value.Expression([FormulaText])(_, [Price=10, Quantity=2]), "Result"). However, the most common professional method is to useValue.Expressioninside a custom function definition. - Using Value.Expression in a Custom Function: For more complex scenarios, go to Home > New Source > Blank Query. Open the Advanced Editor and define a function like this:
(row) => Value.Expression([FormulaText])(row)
This tells Power Query to take the text from theFormulaTextcolumn and execute it using the current row's data as the context. - Apply the Function to the Table: Go back to your main table, select Add Column > Invoke Custom Function, and select the function you just created. Map the input parameter to the current row.
- Set the Data Type: Once the expression is evaluated, the resulting column will likely be of "Any" type. Right-click the column header, select Change Type, and choose Decimal Number, Whole Number, or Text depending on your formula's output.
Common Mistakes
One of the most frequent errors is forgetting that Value.Expression returns a function, not a value. Users often enter Value.Expression([Column]) and are confused when the result in the table is simply the word "Function" in every cell. To get the result, you must follow the function with a set of parentheses () to invoke it, passing the required arguments inside those parentheses.
Another common pitfall is case sensitivity. M is a strictly case-sensitive language. If your text string refers to a column named [Price] but your table column is actually named [price], the Value.Expression will trigger an error during execution. The text within the string must match the schema of the record being passed to the function exactly.
Users also frequently struggle with quoting. If your expression string needs to contain text, you must use double-double quotes. For example, if the expression is supposed to be if [Status] = "Complete" then 1 else 0, the text stored in your source cell must be "if [Status] = ""Complete"" then 1 else 0". Failing to escape these quotes will result in a syntax error when the expression is parsed.
Tips
To make your Value.Expression workflows more maintainable, store your formulas in a separate "Configuration Table" in your database or Excel workbook. This allows non-technical users to update the business logic (e.g., changing a tax rate or a bonus threshold) without needing to open the Power Query Advanced Editor or modify the M code directly.
When dealing with complex logic, test your expressions in a simple Blank Query first. Write the formula as a standard M expression to ensure the logic is sound and the column names are correct. Once it works, convert that formula into a string and place it in your source table for Value.Expression to handle.
Combine Value.Expression with Record.Combine if you are pulling formula components from multiple different columns. This allows you to build a dynamic record that the expression can reference, making your data transformations highly modular.
Troubleshooting
If you encounter a "Token Literal expected" or "Expression.Error" message, the first step is to check the syntax of the text string in your source column. Copy the text from the cell and paste it into the formula bar of a Blank Query. If the formula bar shows a red underline, the error is in the text string, not in the Value.Expression function itself. Correct the syntax in the source data and refresh the preview.
If the result is consistently "null," verify that the arguments being passed to the invoked function match the requirements of the expression. If your expression references [Quantity] but you are passing a record that only contains [Price], the function may fail silently or return null depending on how the error handling is wrapped.
For performance issues, be aware that Value.Expression prevents Power Query from performing "Query Folding." This means the operation cannot be pushed back to the SQL server and must be processed locally on your machine. If you are working with millions of rows, try to move as much filtering and aggregation as possible to steps before the Value.Expression is invoked to reduce the dataset size.
Frequently Asked Questions
Is Value.Expression the same as Expression.Evaluate?
No. Expression.Evaluate takes a string and evaluates it immediately within a provided environment. Value.Expression creates a reusable function object from a string, which can then be called multiple times with different arguments, making it more efficient for row-by-row calculations in a table.
Can I use Value.Expression for text manipulation?
Yes. As long as the M language supports the operation (such as Text.Upper or Text.Combine), you can write those functions into your text string and Value.Expression will execute them normally.
Does this function work in Power BI Service?
Yes, Value.Expression is a standard part of the M engine and works in both Power BI Desktop and the Power BI Service (cloud) once the dataset is published.
Can I pass multiple arguments to a Value.Expression?
Yes. The function created by Value.Expression can accept any number of arguments. You simply need to ensure that the text string is written to handle those arguments and that you pass the corresponding values when invoking the function.
Is there a security risk using Value.Expression?
Since Value.Expression executes code based on text input, it can be risky if the input comes from an untrusted external source. Always ensure that the table containing your formulas is protected and only editable by authorized administrators.