library(tidyverse)
library(readxl)
path = "Power Query/200-299/298/PQ_Challenge_298.xlsx"
input = read_excel(path, range = "A1:E5")
test = read_excel(path, range = "G1:I15")
in1 = input %>%
select(Company = 1, Subtype = 2, Price = 3) %>%
mutate(Type = "Software")
in2 = input %>%
select(Company = 1, Subtype = 4, Price = 5) %>%
mutate(Type = "Hardware")
result = bind_rows(in1, in2) %>%
filter(!is.na(Subtype)) %>%
mutate(across(everything(), as.character)) %>%
mutate(Order = row_number()) %>%
pivot_longer(cols = -c(Company, Order), names_to = "Classification", values_to = "Value") %>%
arrange(desc(Company) , Order,desc(Classification)) %>%
mutate(cclass = row_number(),
Classification = ifelse(Classification == "Subtype", "Sub type", Classification),
.by = c("Company", "Value")) %>%
filter(cclass == 1, !is.na(Value)) %>%
select(-Order, -cclass)
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEExcel BI - PowerQuery Challenge 298
excel-challenges
power-query
Transpose

Challenge Description
Transpose
Solutions
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
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/298/PQ_Challenge_298.xlsx"
input = pd.read_excel(path, usecols="A:E", nrows=5)
test = pd.read_excel(path, usecols="G:I", nrows=15)
in1 = input.iloc[:, [0,1,2]].copy()
in1.columns = ['Company', 'Subtype', 'Price']
in1['Type'] = 'Software'
in2 = input.iloc[:, [0,3,4]].copy()
in2.columns = ['Company', 'Subtype', 'Price']
in2['Type'] = 'Hardware'
result = pd.concat([in1, in2], ignore_index=True)
result = result[result['Subtype'].notna()]
result['Order'] = range(1, len(result)+1)
result = result.melt(id_vars=['Company','Order'], var_name='Classification', value_name='Value')
result = result.sort_values(['Company', 'Order', 'Classification'], ascending=[False, True, False])
result['Classification'] = result['Classification'].replace({'Subtype': 'Sub type'})
result['cclass'] = result.groupby(['Company','Value']).cumcount()+1
result = result[(result['cclass']==1) & (result['Value'].notna())]
result = result.drop(['Order','cclass'], axis=1).reset_index(drop=True)
print(result.equals(test))
#> [1] 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
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.