library(tidyverse)
library(readxl)
library(unpivotr)
path = "Power Query/PQ_Challenge_193.xlsx"
input = read_xlsx(path, range = "A1:I6", col_names = FALSE)
test = read_xlsx(path, range = "A12:F24")
result = input %>%
as_cells() %>%
behead("up-left", "Quarter") %>%
behead("up", "Category") %>%
behead("left", "Persons") %>%
select(Persons, Quarter, Category, chr) %>%
pivot_wider(names_from = Category, values_from = chr) %>%
mutate(across(c(Sales, Bonus), as.numeric),
Total = Sales + Bonus) %>%
pivot_longer(cols = Sales:Total, names_to = "Category", values_to = "Value") %>%
pivot_wider(names_from = Quarter, values_from = Value) %>%
mutate(across(c(Q1:Q4), cumsum), .by = Category) %>%
mutate(Persons = accumulate(Persons, ~ paste(.x, .y, sep = ", "))[match(Persons, unique(Persons))], .by = Category) %>%
mutate(Persons = ifelse(Category == "Sales", Persons, NA_character_))
identical(result, test)
#> [1] TRUEExcel BI - PowerQuery Challenge 193
excel-challenges
power-query
Transpose the problem table into result table. Here A, B means sum of A & B.

Challenge Description
Transpose the problem table into result table. Here A, B means sum of A & B.
Solutions
Logic:
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
import numpy as np
path = "PQ_Challenge_193.xlsx"
input = pd.read_excel(path, sheet_name=0, usecols="A:I", nrows=6, header=None)
test = pd.read_excel(path, sheet_name=0, usecols="A:F", skiprows=11, nrows=13)
input.iloc[0] = input.iloc[0].ffill()
input.columns = input.iloc[0] + " " + input.iloc[1]
input = input.drop([0, 1])
input = input.rename(columns={"Quarters Persons": "Persons"})
for i in range(1, 5):
input[f"Q{i} Total"] = input.filter(like=f"Q{i}").sum(axis=1)
input["Total"] = input.filter(like="Q").sum(axis=1)
input = input.melt(id_vars=["Persons"], value_vars=input.filter(like="Q").columns, var_name="Quarter", value_name="Value")
input["Value"] = pd.to_numeric(input["Value"], errors="coerce")
input[["Quarter", "Category"]] = input["Quarter"].str.split(" ", n=1, expand=True)
input["Category"] = pd.Categorical(input["Category"], categories=["Sales", "Bonus", "Total"], ordered=True)
input = input.sort_values(["Persons", "Quarter", "Category"])
input = input.pivot_table(index=["Persons", "Category"], columns="Quarter", values="Value", aggfunc="first").reset_index()
input[["Q1", "Q2", "Q3", "Q4"]] = input.groupby("Category")[["Q1", "Q2", "Q3", "Q4"]].cumsum()
input = input.rename_axis(None, axis=1)
input["Persons"] = input.groupby("Category")["Persons"].transform(lambda x: x.cumsum().str.join(", "))
input.loc[input.index % 3 != 0, "Persons"] = np.NaN
input["Category"] = input["Category"].astype(str)
print(input.equals(test)) # TrueLogic:
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.