Expression.Constant
ExpressionReturns the M source code representation of a constant value.
Syntax
Expression.Constant(value as any) as textParameters
| Name | Type | Required | Description |
|---|---|---|---|
value | any | Yes | The constant value to convert to its M source code representation. |
Return Value
text — A 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}
}
)Value Type | M Representation | |
|---|---|---|
| 1 | Number (123) | 123 |
| 2 | Date (2035-01-02) | #date(2035, 1, 2) |
| 3 | Text (abc) | "abc" |