Excel BI - PowerQuery Challenge 240

excel-challenges
power-query
Transpose the problem table into result table as shown.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 240

Challenge Description

Transpose the problem table into result table as shown.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_240.xlsx"
input = read_excel(path, range = "A1:C8")
test  = read_excel(path, range = "E1:J8")

result = input %>%
  separate_rows(Items, sep = ", ") %>%
  separate(Items, into = c("Item", "Quantity"), sep = ": ") %>%
  mutate(Quantity = as.numeric(Quantity)) %>%
  pivot_wider(names_from = Item, values_from = Quantity) %>%
  select(Supplier, Date, Bread, Coke, Milk, Rice) %>%
  arrange(Supplier, desc(Date))

all.equal(result, test, check.attributes = FALSE)
  • 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 = "PQ_Challenge_240.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=8)
test = pd.read_excel(path, usecols="E:J", nrows=8)

input = input.assign(Items=input['Items'].str.split(', ')).explode('Items')
input[['Item', 'Quantity']] = input.pop('Items').str.split(': ', expand=True)
input['Quantity'] = pd.to_numeric(input['Quantity'])
result = input.pivot_table(index=['Supplier', 'Date'], columns='Item', values='Quantity', fill_value=0).reset_index()
result = result[['Supplier', 'Date', 'Bread', 'Coke', 'Milk', 'Rice']].sort_values(by=['Supplier', 'Date'], ascending=[True, False])

# Almost equal. Misgtake in source file.
  • 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 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.