Excel BI - PowerQuery Challenge 241

excel-challenges
power-query
Group Group1 Group2 Group3 Group4 Transpose the problem table into result table as shown.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 241

Challenge Description

Group Group1 Group2 Group3 Group4 Transpose the problem table into result table as shown.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_241.xlsx"
input = read_excel(path, range = "A1:F5")
test  = read_excel(path, range = "A9:F20")

result = input %>%
  pivot_longer(-Group, names_to = "Name", values_to = "Value") %>%
  separate_rows(Value, sep = ", ") %>%
  mutate(rn = row_number(), .by = c(Group, Name)) %>%
  pivot_wider(names_from = Name, values_from = Value) %>%
  select(-rn)

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

    • 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_241.xlsx"
input = pd.read_excel(path,  usecols="A:F", nrows=5)
test = pd.read_excel(path, usecols="A:F", skiprows=8, nrows=12)

result = (input.melt(id_vars=["Group"], var_name="Name", value_name="Value")
          .assign(Value=lambda df: df["Value"].str.split(", "))
          .explode("Value")
          .assign(rn=lambda df: df.groupby(["Group", "Name"]).cumcount() + 1)
          .pivot(index=["Group", "rn"], columns="Name", values="Value")
          .reset_index()
          .drop(columns="rn"))

result = result.rename_axis(None, axis=1)
result = result.rename(columns={"Group": "Date"})

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.