Excel BI - PowerQuery Challenge 265

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

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 265

Challenge Description

Transpose the problem table into result table.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_265.xlsx"
input = read_excel(path, range = "A1:G6") 
test  = read_excel(path, range = "A10:C21")

result = input %>%
  pivot_longer(-Factory, names_to = c(".value", "set"), names_pattern = "([a-zA-Z]+)(\\d+)") %>%
  select(-set) %>%
  drop_na()

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

  • 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_265.xlsx"
input = pd.read_excel(path, usecols="A:G", nrows=6)
test = pd.read_excel(path, usecols="A:C", skiprows=9, nrows=12).sort_values(by = ["Factory", 'Part']).reset_index(drop=True)

melted = input.melt(id_vars='Factory', value_vars=['Part1', 'Part2', 'Part3', 'Price1', 'Price2', 'Price3'],
                    var_name='Column', value_name='Value')
melted[['Type', 'Set']] = melted['Column'].str.extract(r'([a-zA-Z]+)(\d+)')
melted = melted.drop(columns=['Column', 'Set']).dropna().reset_index(drop=True)
melted['RowNumber'] = melted.groupby('Type').cumcount() + 1
pivoted = melted.pivot(index=['RowNumber', 'Factory'], columns='Type', values='Value').sort_values(by=["Factory", 'Part']).reset_index()
pivoted = pivoted.drop(columns=['RowNumber']).astype({'Price': 'int64'}).rename_axis(None, axis=1)
print(pivoted.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

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