Expression.Constant

Expression

Returns the M source code representation of a constant value.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Expression.Constant(value as any) as text

Parameters

NameTypeRequiredDescription
valueanyYesThe constant value to convert to its M source code representation.

Return Value

textA text string containing the M source code representation of the constant value.

Remarks

Expression.Constant returns the M source code text representation of a constant value. This is useful for building M expressions dynamically as text strings that can later be passed to Expression.Evaluate.

The function handles all M literal types including numbers, text (with proper quote escaping), dates, times, datetimes, datetimezones, durations, logicals, and null. For text values, double quotes are escaped by doubling them in the output.

This function is the counterpart to Expression.Identifier — while Expression.Identifier serializes identifiers (variable names), Expression.Constant serializes literal values.

### Common use cases

  • Building dynamic M expressions from runtime values for use with Expression.Evaluate.
  • Code generation or meta-programming scenarios in custom connectors.
  • Serializing M values to their source code form for logging or display.

Examples

Example 1: Get the M representation of different value types

let
    NumRepr = Expression.Constant(123),
    DateRepr = Expression.Constant(#date(2035, 1, 2)),
    TextRepr = Expression.Constant("abc")
in
    #table(
        {"Value Type", "M Representation"},
        {
            {"Number (123)", NumRepr},
            {"Date (2035-01-02)", DateRepr},
            {"Text (abc)", TextRepr}
        }
    )
Output
Value Type
M Representation
1Number (123)123
2Date (2035-01-02)#date(2035, 1, 2)
3Text (abc)"abc"

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks