library(tidyverse)
library(readxl)
path = "files/CH-96 Top Products.xlsx"
input = read_excel(path, range = "B2:D20")
test = read_excel(path, range = "I2:K11")
result = input %>%
mutate(Month = month(Date)) %>%
summarise(Quantity = sum(Quantity), .by = c(Product, Month)) %>%
mutate(Rank = dense_rank(desc(Quantity)), .by = Month) %>%
mutate(Product = ifelse(Rank < 3, Product, "Other")) %>%
arrange(Month, Rank) %>%
summarise(Quantity = sum(Quantity), .by = c(Product, Month)) %>%
mutate(`% of Month sales` = Quantity / sum(Quantity), .by = Month) %>%
select(Month, Product, `% of Month sales`)
identical(result, test)
# [1] TRUEOmid - Challenge 96
data-challenges
advanced-exercises
🔰 In the Question table, daily sales information is provided.

Challenge Description
🔰 In the Question table, daily sales information is provided.
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
Builds the intermediate columns that drive the final result
Strengths:
- The R solution stays close to the workbook rule and keeps the transformation compact.
Areas for Improvement:
- The code assumes the sheet structure and source ranges remain stable.
Gem:
- The strongest part of the solution is choosing the right intermediate representation before shaping the final output.
import pandas as pd
path = "CH-96 Top Products.xlsx"
input = pd.read_excel(path, skiprows=1, usecols="B:D", nrows=19)
test = pd.read_excel(path, skiprows=1, usecols="I:K", nrows=9)\
.rename(columns={"Product.1":"Product", "% of Month sales":"Percent"})\
.sort_values(["Month","Product"])\
.reset_index(drop=True)
result = input.assign(Month=input['Date'].dt.month) \
.groupby(['Product', 'Month']) \
.agg(Quantity=('Quantity', 'sum')) \
.reset_index() \
.assign(Rank=lambda x: x.groupby('Month')['Quantity'].rank(method='dense', ascending=False)) \
.assign(Product=lambda x: x.apply(lambda row: row['Product'] if row['Rank'] < 3 else 'Other', axis=1)) \
.sort_values(['Month', 'Rank']) \
.groupby(['Product', 'Month']) \
.agg(Quantity=('Quantity', 'sum')) \
.sort_values(["Month","Product"])\
.reset_index() \
.assign(Percent=lambda x: x['Quantity'] / x.groupby('Month')['Quantity'].transform('sum')) \
.loc[:, ['Month', 'Product', 'Percent']]
print(all(result == test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
Builds the intermediate columns that drive the final result
Strengths:
- The Python version follows the same rule in a direct dataframe-oriented implementation.
Areas for Improvement:
- The code assumes the workbook layout remains stable, so any sheet redesign would require small adjustments.
Gem:
- The implementation stays close to the original workbook rule instead of adding unnecessary abstraction.
Difficulty Level
This task is moderate:
- The business rule is readable, but the workbook still requires careful implementation to reach the expected layout.