Excel BI - PowerQuery Challenge 350

excel-challenges
power-query
Sullivan Group
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 350

Challenge Description

Sullivan Group

Solutions

library(tidyverse)
library(readxl)

path <- "Power Query/300-399/350/PQ_Challenge_350.xlsx"
input <- read_excel(path, range = "A1:I122")
input2 <- read_excel(path, range = "K2", col_names = FALSE) %>% pull(1)
input3 <- read_excel(path, range = "K4", col_names = FALSE) %>% pull(1)
test <- read_excel(path, range = "M1:V8")

result = input %>%
  select(
    `Mat Code` = `Material code`,
    `Mat Name` = `Material name`,
    Day,
    `Weight Net`
  ) %>%
  mutate(weight = `Weight Net` / 1000) %>%
  filter(Day >= input2 & Day <= input3) %>%
  select(-`Weight Net`) %>%
  pivot_wider(names_from = Day, values_from = weight, values_fn = sum) %>%
  janitor::adorn_totals(where = "col", name = "Grand Total")

all.equal(result, test, check.attributes = FALSE)
# TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

    • 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/350/PQ_Challenge_350.xlsx"
input_df = pd.read_excel(path, sheet_name=0, usecols="A:I", nrows=121)
input2 = pd.read_excel(path, usecols="K", skiprows=1, nrows=1, header=None).iloc[0, 0]
input3 = pd.read_excel(path, usecols="K", skiprows=3, nrows=1, header=None).iloc[0, 0]
test = pd.read_excel(path, usecols="M:V", nrows=7).fillna(0)

result = input_df.rename(columns={'Material code': 'Mat Code','Material name': 'Mat Name'})[['Mat Code','Mat Name','Day','Weight Net']]
result = result[(result['Day'] >= input2) & (result['Day'] <= input3)]
result['weight'] = result['Weight Net'] / 1000
result = result.drop(columns='Weight Net').pivot_table(index=['Mat Code','Mat Name'],columns='Day',values='weight',aggfunc='sum',fill_value=0).reset_index()
result['Grand Total'] = result.iloc[:,2:].sum(1)
result = result.set_index(result.columns[0]).loc[test.iloc[:,0]].reset_index().round(2)
test = test.round(2)

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

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

  • 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.