Excel BI - PowerQuery Challenge 249

excel-challenges
power-query
Transpose problem table into result table. Best student will be that student who has scored the highest marks.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 249

Challenge Description

Transpose problem table into result table. Best student will be that student who has scored the highest marks.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_249.xlsx"
input = read_excel(path, range = "A1:C7")
test  = read_excel(path, range = "A11:E14")

r1 = input %>%
  mutate(rn = row_number(), .by = Class) %>%
  select(-Marks) %>%
  pivot_wider(names_from = rn, values_from = Student, names_glue = "Student{rn}") 

r2 = input %>%
  summarise(`Best Student` = paste0(Student, collapse = ", "), .by = c(Class, Marks)) %>%
  slice_max(order_by = Marks, n = 1, by = Class) %>%
  select(-Marks)

result = r1 %>%
  left_join(r2, by = "Class")

all.equal(result, test, check.attributes = FALSE)
#> [1] 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

    • 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 = "PQ_Challenge_249.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=7)
test = pd.read_excel(path, usecols="A:E", skiprows=10, nrows=4)

r1 = (input.assign(rn=input.groupby('Class').cumcount() + 1)
    .pivot(index='Class', columns='rn', values='Student')
    .reset_index()
    .rename(columns=lambda x: f'Student{x}' if isinstance(x, int) else x))

r2 = (input.groupby(['Class', 'Marks'])['Student']
    .apply(lambda x: ', '.join(x))
    .reset_index()
    .sort_values('Marks', ascending=False)
    .drop_duplicates('Class')
    .rename(columns={'Student': 'Best Student'})
    .drop(columns='Marks'))

result = r1.merge(r2, on='Class')

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

    • Builds helper columns that drive the final output

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