Excel BI - PowerQuery Challenge 179

excel-challenges
power-query
Team Player Runs Scored Player1 Player2 Player3
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 179

Challenge Description

Team Player Runs Scored Player1 Player2 Player3

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_179.xlsx", range = "A1:C10")
test  = read_excel("Power Query/PQ_Challenge_179.xlsx", range = "E1:K4")

r1 = input %>%
  select(-`Runs Scored`) %>%
  mutate(player = paste0("Player",row_number()), .by = Team) %>%
  pivot_wider(names_from = player, values_from = Player)

r2 = input %>%
  mutate(max = max(`Runs Scored`), .by = Team) %>%
  filter(`Runs Scored` == max) %>%
  summarise(`Highest Scoring Player` = paste0(Player, collapse = ", "),
            `Highest Score` = unique(`Runs Scored`), .by = Team)

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

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

input = pd.read_excel("PQ_Challenge_179.xlsx",  usecols="A:C", nrows=10)
test = pd.read_excel("PQ_Challenge_179.xlsx",  usecols="E:K", nrows=3)
test = test.rename(columns={"Team.1": "Team"}).sort_values("Team").reset_index(drop=True)

r1 = input.copy()
r1 = r1[["Team", "Player"]]
r1["row"] = r1.groupby("Team").cumcount()+1
r1 = r1.pivot(index="Team", columns="row", values="Player").add_prefix("Player").reset_index()

r2 = input.copy()
r2["Max"] = r2.groupby("Team")["Runs Scored"].transform("max")
r2 = r2[r2["Runs Scored"] == r2["Max"]]
r2 = r2.groupby("Team").agg({"Player": lambda x: ", ".join(x), "Max": "first"}).reset_index()

result = r1.merge(r2, on="Team", how="left")
result.columns = test.columns

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

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