Excel BI - PowerQuery Challenge 304

excel-challenges
power-query
Transpose the problem table into result table. Here A, B means sum of A & B. Transpose the problem table into result table. Here A, B means sum of A & B.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 304

Challenge Description

Transpose the problem table into result table. Here A, B means sum of A & B. Transpose the problem table into result table. Here A, B means sum of A & B.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/300-399/304/PQ_Challenge_304.xlsx"
input = read_excel(path, range = "A1:F9")
test  = read_excel(path, range = "A14:I18")

result = input %>%
  fill(Persons) %>%
  pivot_longer(cols = -c(Persons,Category), names_to = "Quarter", values_to = "Value") %>%
  arrange(Persons, Quarter, desc(Category)) %>%
  unite("Category_Quarter", Category, Quarter, sep = "-") %>%
  pivot_wider(names_from = Category_Quarter, values_from = Value) %>%
  mutate(across(contains("Q"), ~ . - lag(.x, default = 0)),
         Persons = str_sub(Persons, -1, -1)) 

all.equal(result, test, check.attributes = FALSE)
# > [1] 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

    • Uses direct pattern parsing where the workbook encodes logic in text

  • 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 = "300-399/304/PQ_Challenge_304.xlsx"
input = pd.read_excel(path, usecols="A:F", nrows=9)
test  = pd.read_excel(path, usecols="A:I", skiprows=13, nrows=5)

result = (
    input.ffill()
    .melt(id_vars=['Persons', 'Category'], var_name='Quarter', value_name='Value')
    .assign(Category_Quarter=lambda d: d['Category'] + '-' + d['Quarter'])
    .pivot(index='Persons', columns='Category_Quarter', values='Value')
    .reset_index()
)
result.update(result.filter(like='Q').sub(result.filter(like='Q').shift(fill_value=0)))
result['Persons'] = result['Persons'].astype(str).str[-1]
result.columns.name = None
ordered_cols = ['Persons'] + [f"{cat}-{qtr}" for qtr in ['Q1','Q2','Q3','Q4'] for cat in ['Sales','Bonus'] if f"{cat}-{qtr}" in result.columns]

result = result[ordered_cols]

print(result.equals(test))
  • 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

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