Power QueryExercises

Library loans

A month of library loans across three branches, some still out. Summarise loans and average days out per branch and genre.

beginner ยท Skills: Group by with several aggregations, Durations, Nulls in averages ยท Updated 2026-11-04

Data files

  • loans.csv โ€” LoanID, Branch, Genre, LoanDate, ReturnDate (blank while on loan).Download CSV (1.1 KB)

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.

Loans by branch and genre loans-summary.pq

Download .pq

Loans, still on loan, and average days out for returned books. Returns 10 rows: Branch, Genre, Loans, OnLoan, AvgDaysOut.

// Loans by branch and genre
// Pack: Library loans (Power Query Exercises, https://powerqueryexercises.com/downloads/library-loans)
//
// 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:
//   Loans          the pack file loans.csv -> File.Contents(PackFolder & "loans.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
// 10 rows with columns Branch, Genre, Loans, OnLoan, AvgDaysOut.
// 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\library-loans\",
    Loans = File.Contents(PackFolder & "loans.csv"),
Source = Table.PromoteHeaders(Csv.Document(Loans, [Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv]), [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(Source, {{"LoanDate", type date}, {"ReturnDate", type date}}, "en-US"),
    #"Days out" = Table.AddColumn(#"Changed Type", "DaysOut", each if [ReturnDate] = null then null else Duration.Days([ReturnDate] - [LoanDate]), Int64.Type),
    #"Grouped" = Table.Group(#"Days out", {"Branch", "Genre"}, {{"Loans", each Table.RowCount(_), Int64.Type}, {"OnLoan", each List.Count(List.Select([ReturnDate], (d) => d = null)), Int64.Type}, {"AvgDaysOut", each Number.Round(List.Average(List.RemoveNulls([DaysOut])) ?? 0, 1), type number}}),
    #"Sorted" = Table.Sort(#"Grouped", {{"Branch", Order.Ascending}, {"Genre", 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. Remove List.RemoveNulls from the average and see whether the result changes (List.Average ignores nulls, but an explicit filter documents the intent).

Related lessons

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