Excel BI - PowerQuery Challenge 244

excel-challenges
power-query
Merge both problem tables into one by summing marks for common entries. Subjects and Students should be in sorted order.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 244

Challenge Description

Merge both problem tables into one by summing marks for common entries. Subjects and Students should be in sorted order.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_244.xlsx"
input1 = read_excel(path, range = "A1:F6")
input2 = read_excel(path, range = "A9:F12")
test  = read_excel(path, range = "I1:O7")

I3 = bind_rows(input1, input2) %>%
  pivot_longer(cols = -c(Student), names_to = "Subject", values_to = "Score") %>%
  na.omit() %>%
  pivot_wider(names_from = "Subject", values_from = "Score", values_fn = list(Score = sum)) %>%
  select(Student, sort(colnames(.), decreasing = FALSE))

all.equal(I3, test)
# [1] TRUE
  • 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 = "PQ_Challenge_244.xlsx"
input1 = pd.read_excel(path, usecols="A:F", nrows=6)
input2 = pd.read_excel(path, usecols="A:F", skiprows=8, nrows=4)
test = pd.read_excel(path, usecols="I:O", nrows=7).rename(columns=lambda x: x.split('.')[0])

melted = pd.concat([input1, input2]).melt(id_vars='Student', var_name="Subject", value_name="Score").dropna(subset=['Score'])
pivoted = melted.pivot_table(index='Student', columns='Subject', values='Score', aggfunc='sum').reset_index()
pivoted.columns.name = None

print(all(pivoted == test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

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