Excel BI - PowerQuery Challenge 292

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

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 292

Challenge Description

Transpose the table as shown.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/200-299/292/PQ_Challenge_292.xlsx"
input = read_excel(path, range = "A1:K4")
test = read_excel(path, range = "A8:F14")

input2 = input %>%
  pivot_longer(cols = -c(1, 2)) %>%
  separate(name, into = c("name", "Item"), sep = "-") %>%
  fill(Item) %>%
  mutate(name = str_remove(name, "\.{3}\d{1,2}")) %>%
  pivot_wider(
    names_from = name,
    values_from = value,
    values_fill = list(value = 0)
  ) %>%
  arrange(Item, desc(Customer)) %>%
  mutate(`Item Total` = ItemA + ItemB + ItemC) %>%
  select(Customer, Org, Item, `Item Total`, Credit, Debit)

all.equal(input2, test)
# [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 = "200-299/292/PQ_Challenge_292.xlsx"
input = pd.read_excel(path, sheet_name=0, usecols="A:K", nrows=4)
test = pd.read_excel(path, sheet_name=0, usecols="A:F", skiprows=7, nrows=7)

df = input.melt(id_vars=input.columns[:2], var_name='name', value_name='value')
df[['name', 'Item']] = df['name'].str.split('-', expand=True)
df['Item'] = df['Item'].ffill()
df['name'] = df['name'].str.replace(r'\.{1}\d{1,2}', '', regex=True)
df_wide = df.pivot_table(index=['Customer', 'Org', 'Item'], columns='name', values='value', fill_value=0).reset_index()
df_wide = df_wide.astype({col: 'int' for col in df_wide.columns if col not in ['Customer', 'Org', 'Item']})
df_wide = df_wide.sort_values(['Item', 'Customer'], ascending=[True, False]).reset_index(drop=True)
df_wide.columns.name = None
df_wide['Item Total'] = df_wide.get('ItemA', 0) + df_wide.get('ItemB', 0) + df_wide.get('ItemC', 0)
result = df_wide[['Customer', 'Org', 'Item', 'Item Total', 'Credit', 'Debit']]

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

    • Applies the rule iteratively until the output is complete

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