Excel BI - PowerQuery Challenge 216

excel-challenges
power-query
Transpose the table as shown.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 216

Challenge Description

Transpose the table as shown.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_216.xlsx"
input = read_excel(path, range = "A1:E6")
test  = read_excel(path, range = "A11:E16")

result = input %>%
  pivot_longer(everything(), names_to = "Column", values_to = "Item") %>%
  mutate(Column = str_remove(Column, "Column"), 
         item_n = str_remove(Item, "Item") %>% as.numeric()) %>%
  arrange(Column) %>%
  mutate(rn = row_number(), .by = Column) %>%
  mutate(Column_label = paste0("Items ", min(item_n, na.rm = TRUE), " - ", max(item_n, na.rm = TRUE)), .by = rn) %>%
  select(Column_label, Item, Column) %>%
  pivot_wider(names_from = Column_label, values_from = Item) %>%
  select(-Column)

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

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

  • 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_216.xlsx"
input = pd.read_excel(path, usecols="A:E", nrows=5)
test  = pd.read_excel(path, usecols="A:E", skiprows=10, nrows=5)

result = input.melt(var_name='Column', value_name='Item')
result['Column'] = result['Column'].str.replace('Column', '')
result['item_n'] = result['Item'].str.extract(r'(\d+)', expand=False).astype('Int64')

result['rn'] = result.groupby('Column').cumcount() + 1
result['Column_label'] = result.groupby('rn')['item_n'].transform(lambda x: f"Items {x.min()} - {x.max()}")

result = result.pivot(index='Column', columns='Column_label', values='Item').reset_index(drop=True)result = result[['Items 1 - 4', 'Items 5 - 9', 'Items 10 - 11', 'Items 12 - 14', 'Items 15 - 15']]
  • 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

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