Downloads
Power Query MLanguageIntermediate

error

Raises an error on purpose, using an error record, so that the query stops with a clear message.

Syntax
error errorRecord

Returns: Nothing. It stops the expression it is in and reports the error.

What it does

The error keyword raises an error. You use it inside an if or another expression when something must not continue, such as a control total that does not agree or a file that should not be empty. The error carries a reason and a message, which you build with Error.Record. Without error, a bad input can pass silently.

Syntax and arguments

Example

A month-end file must balance: the total of the debit lines must equal the total of the credit lines. If it does not, the refresh stops and the reviewer is told by how much.

let
    Debits = {1250, 480, 75},
    Credits = {1805, 0},
    DebitTotal = List.Sum(Debits),
    CreditTotal = List.Sum(Credits),
    Balanced = if DebitTotal = CreditTotal
        then "Balanced"
        else error Error.Record("Unbalanced file",
            "Debits and credits do not agree.",
            DebitTotal - CreditTotal)
in
    Balanced

Here the totals agree, so Balanced is returned. If a credit line were missing, the query would stop with the reason "Unbalanced file" and the difference in the detail, which the reviewer can read at once.

Example: a second, different use

Stopping a load when a required rate is missing from the rate table, so that no report is produced with a silent gap.

Things to watch

In business terms

Control checks that must pass before a report is used, such as a balanced file, a complete period or a matching control total, are written with error. A failed check that stops the refresh is stronger evidence than a note in a spreadsheet.

Related functions

All Power Query M functions · View as Markdown · M basics