library(tidyverse)
library(readxl)
path = "Power Query/PQ_Challenge_200.xlsx"
input1 = read_excel(path, range = "A1:D6")
input2 = read_excel(path, range = "F1:I6")
test = read_excel(path, range = "A11:E17")
in1 = input1 %>%
pivot_longer(cols = -c(1), names_to = "subject", values_to = "score")
in2 = input2 %>%
pivot_longer(cols = -c(1), names_to = "subject", values_to = "score")
result = bind_rows(in1, in2) %>%
summarise(max = max(score), .by = c("subject", "Student")) %>%
pivot_wider(names_from = "subject", values_from = "max") %>%
arrange(Student)
result = result %>%
select(Student, sort(names(result)[2:5]))
identical(result, test)
# [1] TRUEExcel BI - PowerQuery Challenge 200
excel-challenges
power-query
Student Biology Physics Chemistry Ecology John

Challenge Description
Student Biology Physics Chemistry Ecology John
Solutions
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
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_200.xlsx"
input1 = pd.read_excel(path, usecols="A:D", nrows = 5)
input2 = pd.read_excel(path, usecols="F:I", nrows=5)
input2.columns = input2.columns.str.replace(".1", "")
test = pd.read_excel(path, usecols="A:E", skiprows = 10, nrows = 6)
in1 = input1.melt(id_vars=["Student"], var_name="subject", value_name="score")
in2 = input2.melt(id_vars=["Student"], var_name="subject", value_name="score")
result = pd.concat([in1, in2]).groupby(["subject", "Student"]).agg(max_score=("score", "max")).reset_index()
result = result.pivot(index="Student", columns="subject", values="max_score").reset_index()
result = result.sort_values("Student")
result.columns.name = None
result.iloc[:, 1:] = result.iloc[:, 1:].fillna(0).astype("float64")
test.iloc[:, 1:] = test.iloc[:, 1:].fillna(0).astype("float64")
print(result.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
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.