Excel BI - PowerQuery Challenge 158

excel-challenges
power-query
Group Group 1 Group 2 Group 3 Transpose the Problem table into Result table.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 158

Challenge Description

Group Group 1 Group 2 Group 3 Transpose the Problem table into Result table.

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_158.xlsx", range = "A1:K5", 
                   col_names = T, .name_repair = "unique") 
test  = read_excel("Power Query/PQ_Challenge_158.xlsx", range = "A10:G17") %>%
  mutate(across(everything(), as.character))


r1 = input %>%
  pivot_longer(cols = -c(1), values_to = "value", names_to = "variable") %>%
  mutate(variable = if_else(str_starts(variable, "D"), variable, NA_character_)) %>%
  fill(variable, .direction = "down") %>%
  group_by(Dept) %>%
  nest()

headers = r1[[2]][[1]]$value

r2 = r1 %>%
  filter(Dept != "Group") %>%
  unnest(data) %>%
  mutate(headers = headers) %>%
  pivot_wider(names_from = headers, values_from = value) %>%
  filter(!is.na(`Emp ID`)) %>%
  select(Group = Dept, Dept = variable, everything()) %>%
  ungroup()

identical(r2, test)
# [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_data = pd.read_excel("PQ_Challenge_158.xlsx", usecols="A:K", nrows=5)
test = pd.read_excel("PQ_Challenge_158.xlsx", usecols="A:G", skiprows=9, nrows=8).astype(str)

long = input_data.melt(id_vars=input_data.columns[0], var_name="variable", value_name="value")
long["variable"] = long["variable"].where(long["variable"].astype(str).str.startswith("D"))
long["variable"] = long["variable"].ffill()
dept = long.columns[0]
headers = long.loc[long[dept] == "Group", "value"].tolist()

result = long.loc[long[dept] != "Group"].copy()
result["headers"] = headers * (len(result) // len(headers))
result = result.pivot(index=[dept, "variable"], columns="headers", values="value").reset_index()
result = result[result["Emp ID"].notna()].rename(columns={dept: "Group", "variable": "Dept"})
result = result.astype(str)

print(result.equals(test))
  • 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.