Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Introduction

Feedback

In this article

Overview

Microsoft Power Query provides a powerful "get data" experience that encompasses many features. A core capability of Power Query is to filter and combine, that is, to "mash-up" data from one or more of a rich collection of supported data sources. Any such data mashup is expressed using the Power Query formula language (informally known as "M"). Power Query embeds M documents in a wide range of Microsoft products, including Excel, Power BI, Analysis Services, and Dataverse, to enable repeatable mashup of data.

This document provides the specification for M. After a brief introduction that aims at building some first intuition and familiarity with the language, the document covers the language precisely in several progressive steps:

  1. Thelexical structure defines the set of texts that are lexically valid.

  2. Values, expressions, environments and variables, identifiers, and the evaluation model form the language'sbasic concepts.

  3. The detailed specification ofvalues, both primitive and structured, defines the target domain of the language.

  4. Values havetypes, themselves a special kind of value, that both characterize the fundamental kinds of values and carry additional metadata that is specific to the shapes of structured values.

  5. The set ofoperators in M defines what kinds of expressions can be formed.

  6. Functions, another kind of special values, provide the foundation for a rich standard library for M and allow for the addition of new abstractions.

  7. Errors can occur when applying operators or functions during expression evaluation. While errors aren't values, there are ways tohandle errors that map errors back to values.

  8. Let expressions allow for the introduction of auxiliary definitions used to build up complex expressions in smaller steps.

  9. If expressions support conditional evaluation.

  10. Sections provide a simple modularity mechanism. (Sections aren't yet leveraged by Power Query.)

  11. Finally, aconsolidated grammar collects the grammar fragments from all other sections of this document into a single complete definition.

For computer language theorists: the formula language specified in this document is a mostly pure, higher-order, dynamically typed, partially lazy functional language.

Expressions and values

The central construct in M is theexpression. An expression can be evaluated (computed), yielding a singlevalue.

Although many values can be written literally as an expression, a value isn't an expression. For example, the expression1 evaluates to the value1; the expressions1+1 evaluates to the value2. This distinction is subtle, but important. Expressions are recipes for evaluation; values are the results of evaluation.

The following examples illustrate the different kinds of values available in M. As a convention, a value is written using the literal form in which they would appear in an expression that evaluates to just that value. (Note that the// indicates the start of a comment that continues to the end of the line.)

  • Aprimitive value is single-part value, such as a number, logical, text, or null. A null value can be used to indicate the absence of any data.

    123                  // A numbertrue                 // A logical"abc"                // A textnull                 // null value
  • Alist value is an ordered sequence of values. M supports infinite lists, but if written as a literal, lists have a fixed length. The curly brace characters{ and} denote the beginning and end of a list.

    {123, true, "A"}     // list containing a number, a logical, and                       //     a text {1, 2, 3}            // list of three numbers
  • Arecord is a set offields. A field is a name/value pair where the name is a text value that's unique within the field's record. The literal syntax for record values allows the names to be written without quotes, a form also referred to asidentifiers. The following shows a record containing three fields named "A", "B", and "C", which have values1,2, and3.

    [       A = 1,        B = 2,        C = 3 ]
  • Atable is a set of values organized into columns (which are identified by name), and rows. There's no literal syntax for creating a table, but there are several standard functions that can be used to create tables from lists or records.

    For example:

    #table( {"A", "B"}, { {1, 2}, {3, 4} } )

    This creates a table of the following shape:

    Image of an example table in the M formula language.

  • Afunction is a value that, when invoked with arguments, produces a new value. A function is written by listing the function'sparameters in parentheses, followed by the goes-to symbol=>, followed by the expression defining the function. That expression typically refers to the parameters (by name).

    (x, y) => (x + y) / 2`

Evaluation

The evaluation model of the M language is modeled after the evaluation model commonly found in spreadsheets, where the order of calculation can be determined based on dependencies between the formulas in the cells.

If you've written formulas in a spreadsheet such as Excel, you might recognize the formulas on the left result in the values on the right when calculated:

Screenshots of of the formulas on the right resulting in the values on the left.

In M, parts of an expression can reference other parts of the expression by name, and the evaluation process automatically determines the order in which referenced expressions are calculated.

You can use a record to produce an expression that's equivalent to the previous spreadsheet example. When initializing the value of a field, you can refer to other fields within the record by using the name of the field, as shown in the following example:

[      A1 = A2 * 2,      A2 = A3 + 1,      A3 = 1  ]

The previous expression is equivalent to the following example (in that both evaluate to equal values):

[      A1 = 4,      A2 = 2,      A3 = 1  ]

Records can be contained within, ornest, within other records. You can use thelookup operator ([]) to access the fields of a record by name. For example, the following record has a field namedSales containing a record, and a field namedTotal that accesses theFirstHalf andSecondHalf fields of theSales record:

[      Sales = [ FirstHalf = 1000, SecondHalf = 1100 ],     Total = Sales[FirstHalf] + Sales[SecondHalf] ]

The previous expression is equivalent to the following example when it's evaluated:

[      Sales = [ FirstHalf = 1000, SecondHalf = 1100 ],     Total = 2100 ]

Records can also be contained within lists. You can use thepositional index operator ({}) to access an item in a list by its numeric index. The values within a list are referred to using a zero-based index from the beginning of the list. For example, the indexes0 and1 are used to reference the first and second items in the following list:

[     Sales =          {              [                  Year = 2007,                  FirstHalf = 1000,                  SecondHalf = 1100,                 Total = FirstHalf + SecondHalf // 2100             ],             [                  Year = 2008,                  FirstHalf = 1200,                  SecondHalf = 1300,                 Total = FirstHalf + SecondHalf // 2500             ]          },     TotalSales = Sales{0}[Total] + Sales{1}[Total] // 4600 ]

List and record member expressions (as well aslet expressions) are evaluated usinglazy evaluation, which means that they are evaluated only as needed. All other expressions are evaluated usingeager evaluation, which means that they are evaluated immediately when encountered during the evaluation process. A good way to think about this is to remember that evaluating a list or record expression returns a list or record value that itself remembers how its list items or record fields need to be computed, when requested (by lookup or index operators).

Functions

In M, afunction is a mapping from a set of input values to a single output value. A function is written by first naming the required set of input values (the parameters to the function) and then providing an expression that computes the result of the function using those input values (the body of the function) following the goes-to (=>) symbol. For example:

(x) => x + 1                    // function that adds one to a value (x, y) =>  x + y                // function that adds two values

A function is a value just like a number or a text value. The following example shows a function that's the value of an Add field, which is theninvoked, or executed, from several other fields. When a function is invoked, a set of values are specified that are logically substituted for the required set of input values within the function body expression.

[     Add = (x, y) => x + y,    OnePlusOne = Add(1, 1),     // 2     OnePlusTwo = Add(1, 2)      // 3]

Library

M includes a common set of definitions available for use from an expression called thestandard library, or justlibrary for short. These definitions consist of a set of named values. The names of values provided by a library are available for use within an expression without having been defined explicitly by the expression. For example:

Number.E                        // Euler's number e (2.7182...) Text.PositionOf("Hello", "ll")  // 2

Operators

M includes a set of operators that can be used in expressions.Operators are applied tooperands to form symbolic expressions. For example, in the expression1 + 2 the numbers1 and2 are operands and the operator is the addition operator (+).

The meaning of an operator can vary depending on what kind of values its operands are. For example, the plus operator can be used with other kinds of values besides numbers:

1 + 2                   // numeric addition: 3 #time(12,23,0) + #duration(0,0,2,0)                         // time arithmetic: #time(12,25,0)

Another example of an operator with operand-depending meaning is the combination operator (&):

"A" & "BC"              // text concatenation: "ABC" {1} & {2, 3}            // list concatenation: {1, 2, 3} [ a = 1 ] & [ b = 2 ]   // record merge: [ a = 1, b = 2 ]

Note that some operators don't support all combinations of values. For example:

1 + "2"  // error: adding number and text isn't supported

Expressions that, when evaluated, encounter undefined operator conditions evaluate toerrors.

Metadata

Metadata is information about a value that's associated with a value. Metadata is represented as a record value, called ametadata record. The fields of a metadata record can be used to store the metadata for a value.

Every value has a metadata record. If the value of the metadata record hasn't been specified, then the metadata record is empty (has no fields).

Metadata records provide a way to associate additional information with any kind of value in an unobtrusive way. Associating a metadata record with a value doesn't change the value or its behavior.

A metadata record valuey is associated with an existing valuex using the syntaxx meta y. For example, the following code associates a metadata record withRating andTags fields with the text value"Mozart":

"Mozart" meta [ Rating = 5, Tags = {"Classical"} ]

For values that already carry a non-empty metadata record, the result of applying meta is that of computing the record merge of the existing and the new metadata record. For example, the following two expressions are equivalent to each other and to the previous expression:

("Mozart" meta [ Rating = 5 ]) meta [ Tags = {"Classical"} ] "Mozart" meta ([ Rating = 5 ] & [ Tags = {"Classical"} ])

A metadata record can be accessed for a given value using theValue.Metadata function. In the following example, the expression in theComposerRating field accesses the metadata record of the value in theComposer field, and then accesses theRating field of the metadata record.

[     Composer = "Mozart" meta [ Rating = 5, Tags = {"Classical"} ],     ComposerRating = Value.Metadata(Composer)[Rating] // 5]

Let expression

Many of the examples shown so far have included all the literal values of the expression in the result of the expression. Thelet expression allows a set of values to be computed, assigned names, and then used in a subsequent expression that precedes thein. For example, in our sales data example, you could do:

let     Sales2007 =          [              Year = 2007,              FirstHalf = 1000,              SecondHalf = 1100,             Total = FirstHalf + SecondHalf // 2100         ],     Sales2008 =          [              Year = 2008,              FirstHalf = 1200,              SecondHalf = 1300,             Total = FirstHalf + SecondHalf // 2500         ],    TotalSales = Sales2007[Total] + Sales2008[Total]in    TotalSales  // 4600

The result of the above expression is a number value (4600) that's computed from the values bound to the namesSales2007 andSales2008.

If expression

Theif expression selects between two expressions based on a logical condition. For example:

if 2 > 1 then    2 + 2else      1 + 1

The first expression (2 + 2) is selected if the logical expression (2 > 1) is true, and the second expression (1 + 1) is selected if it's false. The selected expression (in this case2 + 2) is evaluated and becomes the result of theif expression (4).

Errors

Anerror is an indication that the process of evaluating an expression couldn't produce a value.

Errors are raised by operators and functions encountering error conditions or by using the error expression. Errors are handled using thetry expression. When an error is raised, a value is specified that can be used to indicate why the error occurred.

let Sales =     [         Revenue = 2000,         Units = 1000,         UnitPrice = if Units = 0 then error "No Units"                    else Revenue / Units     ],     UnitPrice = try Number.ToText(Sales[UnitPrice]),    Result = "Unit Price: " &         (if UnitPrice[HasError] then UnitPrice[Error][Message]        else UnitPrice[Value])in    Result

The above example accesses theSales[UnitPrice] field and formats the value producing the result:

"Unit Price: 2"

If theUnits field had been zero, then theUnitPrice field would have raised an error, which would have been handled by thetry. The resulting value would then have been:

"Unit Price: No Units"

Atry expression converts proper values and errors into a record value that indicates whether thetry expression handled an error, or not, and either the proper value or the error record it extracted when handling the error. For example, consider the following expression that raises an error and then handles it right away:

try error "negative unit count"

This expression evaluates to the following nested record value, explaining the[HasError],[Error], and[Message] field lookups in the previous unit-price example.

[     HasError = true,     Error =         [             Reason = "Expression.Error",             Message = "negative unit count",             Detail = null         ] ]

A common case is to replace errors with default values. Thetry expression can be used with an optionalotherwise clause to achieve just that in a compact form:

try error "negative unit count" otherwise 42 // 42

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?