Excel BI - PowerQuery Challenge 279

excel-challenges
power-query
Group Transpose the problem table into result table.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 279

Challenge Description

Group Transpose the problem table into result table.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_279.xlsx"
input = read_excel(path, range = "A1:E8")
test = read_excel(path, range = "G1:J14")

result = input %>%
  pivot_longer(cols = Code1:Value2, names_to = c(".value", "pair_id"),
               names_pattern = "([A-Za-z]+)(\\d+)") %>%
  select(-pair_id) %>%
  na.omit() %>%
  separate(Code, into = c("Type", "Code"), sep = 2)

all.equal(result, test, check.attributes = FALSE)
  • 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_279.xlsx"
input = pd.read_excel(path, usecols="A:E", nrows=8)
test = pd.read_excel(path, usecols="G:J", nrows=14).rename(columns=lambda x: x.rstrip('.1'))

df_long = pd.wide_to_long(input.reset_index(), ["Code", "Value"], ["Group", "index"], "idx", "", "\d+").reset_index()
df_long = df_long.dropna(subset=["Code", "Value"])
df_long[["Type", "Code"]] = df_long["Code"].str.extract(r"([A-Z]+)(\d+)")
result = df_long[["Group", "Type", "Code", "Value"]]
result["Value"] = result["Value"].astype(int)
result["Code"] = result["Code"].astype(int)

print(result.equals(test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge

    • Uses direct pattern parsing where the workbook encodes logic in text

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