Excel BI - PowerQuery Challenge 320

excel-challenges
power-query
Transpose the table
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 320

Challenge Description

Transpose the table

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/300-399/320/PQ_Challenge_320.xlsx"
input = read_excel(path, range = "A1:C13")
test  = read_excel(path, range = "E1:I5")

result = input %>%
  fill(Customer) %>%
  filter(Customer != "Total") %>%
  pivot_wider(names_from = Quarter, 
              names_glue = "{Quarter} {.value}", 
              values_from = Amount)

all.equal(result, test) # 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 = "300-399/320/PQ_Challenge_320.xlsx"

input = pd.read_excel(path, usecols="A:C", nrows=13)
test = pd.read_excel(path, usecols="E:I", nrows=4).rename(columns=lambda c: c.replace('.1', ''))

input['Customer'] = input['Customer'].ffill()
input = input[input['Customer'] != 'Total']

result = input.pivot(index='Customer', columns='Quarter', values='Amount')
result.columns = [f"{col} Amount" for col in result.columns]
result = result.reset_index()

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.