Excel BI - PowerQuery Challenge 351

excel-challenges
power-query
ID Name Department Quarter Sales Annual Target
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 351

Challenge Description

ID Name Department Quarter Sales Annual Target

Solutions

library(tidyverse)
library(readxl)

path <- "Power Query/300-399/351/PQ_Challenge_351.xlsx"
input1 <- read_excel(path, range = "A1:F21")
input2 <- read_excel(path, range = "H1:I6")
test <- read_excel(path, range = "H12:J17")

result = input1 %>%
  summarise(
    Sales = sum(Sales),
    `Annual Target` = first(`Annual Target`),
    .by = c(ID, Name, Department)
  ) %>%
  left_join(input2, by = c("Department")) %>%
  mutate(
    `Annual Bonus` = ifelse(
      (Sales - `Annual Target`) > 0,
      (Sales - `Annual Target`) * `Base Bonus Rate`,
      0
    )
  ) %>%
  select(ID, Name, `Annual Bonus`)

all.equal(result, test)
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • 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

path = "Power Query/300-399/351/PQ_Challenge_351.xlsx"
input1 = pd.read_excel(path, usecols="A:F", nrows=21)
input2 = pd.read_excel(path, usecols="H:I", nrows=5).rename(columns=lambda col: col.replace('.1', ''))
test = pd.read_excel(path, usecols="H:J", skiprows=11, nrows=5)

result = (
    input1.groupby(["ID", "Name", "Department"], as_index=False)
    .agg({"Sales": "sum", "Annual Target": "first"})
    .merge(input2, on="Department", how="left")
)
result["Annual Bonus"] = (
    ((result["Sales"] - result["Annual Target"]).clip(lower=0) * result["Base Bonus Rate"]).astype('int64')
)
result = result[["ID", "Name", "Annual Bonus"]]

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.