Excel BI - PowerQuery Challenge 288

excel-challenges
power-query
1. summarise by month, country and project
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 288

Challenge Description

  1. summarise by month, country and project

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/200-299/288/PQ_Challenge_288.xlsx"
input1 = read_excel(path, range = "A2:F102")
input2 = read_excel(path, range = "H2:K65")
test = read_excel(path, range = "M2:P14")

xrates = input2 %>%
  pivot_longer(cols = -1, names_to = "Currency", values_to = "xrate")

result = input1 %>%
  mutate(Product = str_to_lower(str_remove_all(Product, " "))) %>%
  left_join(xrates, by = c("Date", "Currency")) %>%
  mutate(
    rev = round(Unit_Price * xrate * Quantity, 2),
    month = month(Date, label = T, locale = "en_US.UTF-8")
  ) %>%
  summarise(rev = sum(rev), .by = c(month, Product, Country)) %>%
  pivot_wider(names_from = "month", values_from = "rev") %>%
  arrange(Country, Product) %>%
  select(Country, Product, Mar, Apr)

all.equal(result, test, chech.artributes = F)
# 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

    • 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 = "200-299/288/PQ_Challenge_288.xlsx"
input1 = pd.read_excel(path, usecols="A:F", skiprows=1, nrows=100)
input2 = pd.read_excel(path, usecols="H:K", skiprows=1, nrows=63)
test = pd.read_excel(path, usecols="M:P", skiprows=1, nrows=12).rename(columns=lambda x: x.replace('.1', ''))
test[['Mar', 'Apr']] = test[['Mar', 'Apr']].applymap(lambda x: str(int(-(-x // 1))) if pd.notnull(x) else x)

xrates = input2.melt(id_vars=input2.columns[0], var_name="Currency", value_name="xrate")
input1['Product'] = input1['Product'].str.replace(' ', '').str.lower()
df = input1.merge(xrates, left_on=['Date', 'Currency'], right_on=[input2.columns[0], 'Currency'])
df['rev'] = (df['Unit_Price'] * df['xrate'] * df['Quantity']).round(2)
df['month'] = pd.to_datetime(df['Date']).dt.strftime('%b')

result = (
    df.groupby(['month', 'Product', 'Country'], as_index=False)['rev'].sum()
    .pivot(index=['Country', 'Product'], columns='month', values='rev')
    .reset_index()[['Country', 'Product', 'Mar', 'Apr']]
    .sort_values(['Country', 'Product'])
)
result[['Mar', 'Apr']] = result[['Mar', 'Apr']].applymap(lambda x: str(int(-(-x // 1))) if pd.notnull(x) else x)
result.columns.name = None

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.