FormulaCamp
Completed
9. Power Query · Sources, Errors, and M · 15 min

8. The M Language: How to Read a Query

Lesson 8 of 8
    Lesson theory

    Every Power Query step is a line of M. You can see the whole query in the Advanced Editor (Home › Advanced Editor):

    let
        Source = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter=",", Encoding=65001]),
        Headers = Table.PromoteHeaders(Source),
        Typed = Table.TransformColumnTypes(Headers, {{"Amount", type number}}),
        PaidOnly = Table.SelectRows(Typed, each [Status] = "Paid"),
        Totals = Table.Group(PaidOnly, {"City"}, {{"Amount", each List.Sum([Amount]), type number}})
    in
        Totals
    • let … in: the steps are variables, each one refers to the previous; after in comes the step to return;
    • each [Status] = "Paid" — a short function applied to every row, and [Status] is a field of the current row;
    • M is case-sensitive: table.selectrows won't work, and "Paid" ≠ "paid";
    • parameters (Home › Manage Parameters) replace hard-coded values, such as a folder path or a filter threshold;
    • try [Price] * 1 otherwise 0 — catches an error inside an expression.

    In the trainer, the formula of each step is shown above the preview; the trainer can't edit M directly.

    Test yourself

    1. What does the query return if the line after in says Typed, but the last step is Totals?
    2. What does each [Amount] > 1000 mean in Table.SelectRows?
    3. Why did Table.SelectRows(Typed, each [City] = "chicago") find nothing, although the data has "Chicago"?
    4. How can people change the folder path of the reports without editing the code?
    5. Which expression replaces a conversion error with zero?