Error.Record

Expression

Creates an error record from the provided reason, message, detail, parameters, and error code values.

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

Syntax

Error.Record(reason as text, optional message as nullable text, optional detail as any, optional parameters as nullable list, optional errorCode as nullable text) as record

Parameters

NameTypeRequiredDescription
reasontextYesThe high-level cause of the error (e.g., "DataFormat.Error", "Expression.Error").
messagetextNoA human-readable description of the error.
detailanyNoAdditional detailed information about the error.
parameterslistNoA list of values that provide additional context for the error, typically used for diagnostics or programmatic handling.
errorCodetextNoAn identifier for the error.

Return Value

recordAn error record with Reason, Message, Detail, Message.Parameters, and ErrorCode fields.

Remarks

Error.Record creates a structured error record that can be used with the error keyword to raise custom errors in M. The resulting record contains the fields Reason, Message, Detail, Message.Format, Message.Parameters, and ErrorCode.

This is the recommended way to construct error values for use with error expressions and try ... catch patterns:

``powerquery error Error.Record("MyCustomError", "Something went wrong", [Step = "Validation"]) ``

### Error record fields

The returned record contains these fields:

  • Reason — the high-level error category (from the reason parameter).
  • Message — the human-readable error description (from the message parameter).
  • Detail — additional structured detail (from the detail parameter).
  • Message.Format — the original message format string.
  • Message.Parameters — the parameters list for message formatting.
  • ErrorCode — the error code identifier.

While you can also raise errors with a plain text string (error "something"), Error.Record gives you full control over all fields of the error record, making it easier to catch and handle errors programmatically.

Examples

Example 1: Raise and catch a custom error

let
    Result = try error Error.Record(
        "DivideByZero",
        "You attempted to divide by zero."
    )
in
    #table(
        {"HasError", "Reason", "Message"},
        {{Result[HasError], Result[Error][Reason], Result[Error][Message]}}
    )
Output
HasError
Reason
Message
1TRUEDivideByZeroYou attempted to divide by zero.

Example 2: Error record with all fields

let
    Result = try error Error.Record(
        "CustomerNotFound",
        "Customer ID 12345 was not found.",
        "Customer does not exist.",
        {"Invalid ID = 12345", "Valid IDs: https://api.contoso.com/customers"},
        "ERR404"
    )
in
    #table(
        {"Reason", "Message", "ErrorCode"},
        {{Result[Error][Reason], Result[Error][Message], Result[Error][ErrorCode]}}
    )
Output
Reason
Message
ErrorCode
1CustomerNotFoundCustomer ID 12345 was not found.ERR404

Compatibility

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