Excel BI - PowerQuery Challenge 336

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

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 336

Challenge Description

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

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/300-399/336/PQ_Challenge_336.xlsx"
input = read_excel(path, range = "A1:I5")
test  = read_excel(path, range = "A9:F17")

result = input %>%
  pivot_longer(cols = -Persons,
               names_to = c("Category", "Quarter"),
               names_sep = "-") %>%
  mutate(value = cumsum(value), .by = c(Category, Quarter),
         Persons = accumulate(Persons, ~ str_c(.x, .y, sep = " & "))) %>%
  pivot_wider(names_from = Quarter, values_from = value) %>%
  mutate(Persons = ifelse(row_number() == 1, Persons, NA_character_), .by = Persons) 

all.equal(result, test)
# [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
import numpy as np

path = "300-399/336/PQ_Challenge_336.xlsx"
input = pd.read_excel(path, usecols="A:I", nrows=5)
test = pd.read_excel(path, usecols="A:F", skiprows=8, nrows=9)
input_long = input.melt(id_vars='Persons', var_name='Category_Quarter', value_name='value')
input_long[['Category', 'Quarter']] = input_long['Category_Quarter'].str.split('-', expand=True)

input_long['value'] = input_long.groupby(['Category', 'Quarter'])['value'].cumsum()
def accumulate_persons(persons):
    acc = ""
    return [acc := p if not acc else f"{acc} & {p}" for p in persons]
input_long['Persons'] = input_long.groupby(['Category', 'Quarter'])['Persons'].transform(accumulate_persons)
result = (input_long.pivot_table(index=['Persons', 'Category'], columns='Quarter', values='value')
          .reset_index()
          .sort_values(['Persons', 'Category'], ascending=[True, False])
          .reset_index(drop=True))
result.loc[result['Category'] == 'Bonus', 'Persons'] = np.nan
for col in result.columns:
    if str(col).startswith('Q'):
        result[col] = result[col].astype('int64')
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

    • Aggregates or ranks values at the relevant grouping level

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