Power QueryExercises

Meter readings to usage

Cumulative utility meter readings, out of order. Sort per meter and turn readings into usage between consecutive readings.

advanced · Skills: Sorting within groups, Index columns, Previous-row calculations · Updated 2026-11-04

Data files

  • meter-readings.csv — Meter, ReadOn (month end), Reading (cumulative kWh), deliberately unsorted.Download CSV (400 B)

Queries

Validated in the Power Query Exercises simulator against the files above. Paste them into Excel or Power BI yourself; the header comments explain each source binding.

Usage between readings monthly-usage.pq

Download .pq

Per meter: readings in date order with the usage since the previous reading. Returns 18 rows: Meter, ReadOn, Reading, Seq, Usage.

// Usage between readings
// Pack: Meter readings to usage (Power Query Exercises, https://powerqueryexercises.com/downloads/meter-readings-usage)
//
// Source bindings
// In the Power Query Exercises simulator each name below is bound directly to a pack file. In Excel or
// Power BI Desktop the first steps of this query do that binding instead:
//   Readings       the pack file meter-readings.csv -> File.Contents(PackFolder & "meter-readings.csv")
// Set PackFolder to the folder where you extracted the pack (keep the trailing backslash).
//
// Validated in the Power Query Exercises simulator (engine 0.1.0) against this pack's files; it returns
// 18 rows with columns Meter, ReadOn, Reading, Seq, Usage.
// It has not been run in Excel or Power BI by us. This is query text, not a ready-to-refresh workbook.
// Independent practice material; not affiliated with or endorsed by Microsoft.
let
    PackFolder = "C:\PQE\meter-readings-usage\",
    Readings = File.Contents(PackFolder & "meter-readings.csv"),
Source = Table.TransformColumnTypes(Table.PromoteHeaders(Csv.Document(Readings, [Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv])), {{"ReadOn", type date}, {"Reading", Int64.Type}}, "en-US"),
    #"Per meter" = Table.Group(Source, {"Meter"}, {{"Rows", each Table.AddIndexColumn(Table.Sort(_, {{"ReadOn", Order.Ascending}}), "Seq", 1, 1), type table}}),
    #"Usage" = Table.AddColumn(#"Per meter", "WithUsage", each
        let t = [Rows], prev = Table.RenameColumns(Table.SelectColumns(Table.TransformColumns(t, {{"Seq", (n) => n + 1}}), {"Seq", "Reading"}), {{"Reading", "Previous"}}),
            j = Table.ExpandTableColumn(Table.NestedJoin(t, {"Seq"}, prev, {"Seq"}, "P", JoinKind.LeftOuter), "P", {"Previous"})
        in Table.AddColumn(j, "Usage", (r) => if r[Previous] = null then null else r[Reading] - r[Previous])),
    #"Combined" = Table.Combine(#"Usage"[WithUsage]),
    #"Sorted" = Table.Sort(Table.RemoveColumns(#"Combined", {"Previous"}), {{"Meter", Order.Ascending}, {"ReadOn", Order.Ascending}})
in
    #"Sorted"

Follow along in a host

Follow along in Excel

  1. Download the pack and extract the zip, for example to C:\PQE\<pack name>\.
  2. In Excel (Microsoft 365 or 2016+ on Windows), go to Data → Get Data → From Other Sources → Blank Query.
  3. In the Power Query Editor choose Home → Advanced Editor, select everything and paste the contents of the .pq file.
  4. Change the PackFolder line to the folder you extracted to, then choose Done.
  5. If Excel asks about privacy levels or file access, allow access to that folder. Then choose Home → Close & Load.

Follow along in Power BI Desktop

  1. Download the pack and extract the zip.
  2. In Power BI Desktop choose Home → Transform data to open the Power Query Editor, then Home → New Source → Blank Query.
  3. Choose Home → Advanced Editor, paste the contents of the .pq file and change the PackFolder line to your folder.
  4. Choose Done, check the preview against the expected rows in the query header, then Close & Apply.

Then try this

  1. The first reading of each meter has no Usage (null): there's nothing to subtract from.
  2. Shuffle the CSV rows again and refresh: the result doesn't change, because the query sorts inside each meter.

Related lessons

Original fictional data, free to use for learning and teaching. Power Query Exercises is not affiliated with or endorsed by Microsoft.