Excel BI - PowerQuery Challenge 159

excel-challenges
power-query
Name Year Month Sales Lisa Smith
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 159

Challenge Description

Name Year Month Sales Lisa Smith

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_159.xlsx", range = "A1:D19")
test  = read_excel("Power Query/PQ_Challenge_159.xlsx", range = "F1:I73")

calendar = input %>%
  select(-c(Sales,Month)) %>%
  group_by(Name) %>%
  expand_grid(Y = unique(Year), M = 1:12) %>%
  distinct() %>%
  filter(Y == Year) %>%
  select(Name, Year, Month = M) %>%
  ungroup()

result = calendar %>%
  left_join(input, by = c("Name", "Year", "Month")) %>%
  replace_na(list(Sales = 100))

identical(result, test)
# [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

  • Strengths:

    • The R solution stays close to the workbook logic and keeps the transformation compact.
  • Areas for Improvement:

    • The code assumes the workbook layout and selected ranges remain stable.
  • Gem:

    • The best part of the solution is choosing the right intermediate shape before formatting the final output.
import pandas as pd

input_data = pd.read_excel("PQ_Challenge_159.xlsx", usecols="A:D", nrows=19)
test = pd.read_excel("PQ_Challenge_159.xlsx", usecols="F:I", nrows=73)

calendar = []
for name, g in input_data.groupby("Name"):
    for year in sorted(g["Year"].unique()):
        for month in range(1, 13):
            calendar.append({"Name": name, "Year": year, "Month": month})
calendar = pd.DataFrame(calendar)
result = calendar.merge(input_data, on=["Name", "Year", "Month"], how="left")
result["Sales"] = result["Sales"].fillna(100)

print(result.equals(test))
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • Applies the rule iteratively until the output is complete

  • Strengths:

    • The Python version follows the same workbook rule in a direct pandas-oriented implementation.
  • Areas for Improvement:

    • As with the R version, any workbook layout change would require small adjustments.
  • Gem:

    • The implementation stays close to the source challenge instead of adding unnecessary abstraction.

Difficulty Level

This task is moderate:

  • It combines reshaping, grouping, or parsing steps that are common in Power Query style problems.

  • The main challenge is reproducing the workbook output structure exactly.