library(tidyverse)
library(readxl)
path = "PQ_Challenge_283.xlsx"
input = read_excel(path, range = "A1:C10")
test = read_excel(path, range = "E1:I4")
result = input %>%
pivot_wider(names_from = Dept, values_from = Amount, names_prefix = "Dept ") %>%
select(ID, order(colnames(.)))
all.equal(result, test)
#> [1] TRUEExcel BI - PowerQuery Challenge 283
excel-challenges
power-query
Transpose the table as shown. ID and Department names should be sorted.

Challenge Description
Transpose the table as shown. ID and Department names should be sorted.
Solutions
Logic:
Reads the workbook range needed for the challenge
Reshapes the data into the structure required by the result table
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 = "200-299/283/PQ_Challenge_283.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=10)
test = pd.read_excel(path, usecols="E:I", nrows=3).rename(columns=lambda x: x.split('.')[0])
test.iloc[:, 1:4] = test.iloc[:, 1:4].astype('float64')
result = input.pivot(index='ID', columns='Dept', values='Amount').reset_index()
result.columns = ['ID'] + [f"Dept {col}" for col in result.columns[1:]]
result = result[['ID'] + sorted(result.columns[1:])]
print(result.equals(test)) # TrueLogic:
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.