Excel BI - PowerQuery Challenge 180

excel-challenges
power-query
Emp-Month Sales Emp Total Sales Max Sales Change From - To Months
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 180

Challenge Description

Emp-Month Sales Emp Total Sales Max Sales Change From - To Months

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_180.xlsx", range = "A1:B28")
test  = read_excel("Power Query/PQ_Challenge_180.xlsx", range = "D1:G4")

result = input %>%
  mutate(Emp = ifelse(is.na(Sales), `Emp-Month`, NA_character_)) %>%
  fill(Emp) %>%
  filter(!is.na(Sales)) %>%
  mutate(lag_sales = lag(Sales, 1, default = 0),
         lag_month = lag(`Emp-Month`, 1, default = ""),
         total = sum(Sales), 
         change = abs(lag_sales - Sales),
         max_change = max(change),
         .by = Emp) %>%
  filter(change == max_change) %>%
  select(Emp, `Total Sales` = total, `Max Sales Change` = max_change, lag_month, `Emp-Month`) %>%
  unite("From - To Months", lag_month, `Emp-Month`, sep = " - ")

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

    • Reads the workbook range needed for the challenge

    • Builds helper columns that drive the final output

  • 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 = pd.read_excel("PQ_Challenge_180.xlsx", sheet_name="Sheet1", usecols="A:B")
test = pd.read_excel("PQ_Challenge_180.xlsx", sheet_name="Sheet1", usecols="D:G", nrows=3)

result = input.copy()
result["Emp"] = result.apply(lambda row: row["Emp-Month"] if pd.isna(row["Sales"]) else None, axis=1)
result["Emp"] = result["Emp"].fillna(method="ffill")
result = result[~result["Sales"].isna()]
result["lag_sales"] = result.groupby("Emp")["Sales"].shift(1).fillna(0)
result["lag_month"] = result.groupby("Emp")["Emp-Month"].shift(1).fillna("")
result["total"] = result.groupby("Emp")["Sales"].transform("sum")
result["change"] = abs(result["lag_sales"] - result["Sales"])
max_change = result.groupby("Emp")["change"].transform("max")
result = result[result["change"] == max_change]
result = result[["Emp", "total", "change", "lag_month", "Emp-Month"]]
result.columns = ["Emp", "Total Sales", "Max Sales Change", "lag_month", "Emp-Month"]
result["From - To Months"] = result["lag_month"] + " - " + result["Emp-Month"]
result = result.drop(columns=["lag_month", "Emp-Month"])
result[["Total Sales","Max Sales Change"]] = result[["Total Sales","Max Sales Change"]].astype("int64")
result = result.reset_index(drop=True)

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

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

  • 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 easy to moderate:

  • The transformation rule is readable, but the final layout still requires a careful implementation.