Excel BI - PowerQuery Challenge 248

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 248

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/PQ_Challenge_248.xlsx"
input = read_excel(path, sheet = 2, range = "A1:F9")
test  = read_excel(path, sheet = 2, range = "A13:I17")

result = input %>%
  fill(Persons, .direction = "down") %>%
  pivot_longer(-c(Persons, Category), names_to = "Quarter", values_to = "Value") %>%
  unite("Category_Quarter", Quarter, Category, sep = " ") %>%
  pivot_wider(names_from = Category_Quarter, values_from = Value) %>%
  arrange(-`Q1 Sales`) %>%
  mutate(across(starts_with("Q"), ~(. = . - lead(., default = 0))),
         Quarters = str_sub(Persons, -1, -1)) %>%
  select(Quarters, starts_with("Q1"), starts_with("Q2"), starts_with("Q3"), starts_with("Q4")) %>%
  arrange(Quarters)

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 = "PQ_Challenge_248.xlsx"
input = pd.read_excel(path, sheet_name=1, usecols="A:F", nrows=9)
test = pd.read_excel(path, sheet_name=1, usecols="A:I", skiprows=12, nrows=5)

input['Persons'].ffill(inplace=True)
input_long = input.melt(id_vars=['Persons', 'Category'], var_name='Quarter', value_name='Value')
input_long['Category_Quarter'] = input_long['Quarter'] + ' ' + input_long['Category']
result = input_long.pivot(index='Persons', columns='Category_Quarter', values='Value').reset_index()

result = result.sort_values(by='Q1 Sales', ascending=False)
result.update(result.filter(like='Q').sub(result.filter(like='Q').shift(-1, fill_value=0)))

result['Quarters'] = result['Persons'].str[-1]
zipped_columns = [val for pair in zip(sorted([col for col in result.columns if 'Sales' in col]), 
                                      sorted([col for col in result.columns if 'Bonus' in col])) for val in pair]

result = result[['Quarters'] + zipped_columns].sort_values(by='Quarters').reset_index(drop=True)
result.columns.name = None

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

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