Excel BI - PowerQuery Challenge 239

excel-challenges
power-query
Transpose the problem table into result table and calculate the drop in sales amount from one quarter to previous quarter. Rank them on the basis of total drop for a person (total drop is sum of column I for a person).
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 239

Challenge Description

Transpose the problem table into result table and calculate the drop in sales amount from one quarter to previous quarter. Rank them on the basis of total drop for a person (total drop is sum of column I for a person).

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_239.xlsx"
input = read_excel(path, range = "A1:E4")
test  = read_excel(path, range = "G1:J10")

result = input %>%
  pivot_longer(cols = -c(1), names_to = "quarter", values_to = "sales") %>%
  mutate(`QtQ Drop` = paste0(lead(quarter),"-",quarter),
         Amount = lead(sales) - sales,
         tot_amount = sum(Amount, na.rm = TRUE),
         .by = Name) %>%
  mutate(Rank = dense_rank(-tot_amount),
         Name = ifelse(quarter == "Q1", Name, NA),
         Rank = ifelse(quarter == "Q1", Rank, NA)) %>%
  filter(!is.na(Amount)) %>%
  select(Name, `QtQ Drop`, Amount, Rank)

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

  • 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_239.xlsx"
input = pd.read_excel(path, usecols="A:E", nrows=3)
test = pd.read_excel(path, usecols="G:J", nrows=9).rename(columns=lambda x: x.split('.')[0])

input['rownumber'] = input.reset_index().index + 1
input_long = input.melt(id_vars=['Name', 'rownumber'], var_name='quarter', value_name='sales')

input_long['QtQ Drop'] = input_long.groupby('Name')['quarter'].shift(-1) + "-" + input_long['quarter']
input_long['Amount'] = input_long.groupby('Name')['sales'].shift(-1) - input_long['sales']
input_long['Rank'] = input_long.groupby('Name')['Amount'].transform('sum').rank(method='dense', ascending=False)

input_long = input_long.sort_values(by=['rownumber', 'quarter', "Rank"])

input_long.loc[input_long['quarter'] != 'Q1', ['Name', 'Rank']] = None

result = input_long.dropna(subset=['Amount']).loc[:, ['Name', 'QtQ Drop', 'Amount', 'Rank']].reset_index(drop=True)
result['Amount'] = result['Amount'].astype('int64')

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

    • Aggregates or ranks values at the relevant grouping level

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