Omid - Challenge 44

data-challenges
advanced-exercises
🔰 Calculate the total sales per product in each region for the entire spring season as the result table.
Published

March 24, 2026

Illustration for Omid - Challenge 44

Challenge Description

🔰 Calculate the total sales per product in each region for the entire spring season as the result table.

Solutions

library(tidyverse)
library(readxl)

input1 = read_excel("files/CH-044 Combine Tables.xlsx", range = "C3:F6")
input2 = read_excel("files/CH-044 Combine Tables.xlsx", range = "C9:G12")
input3 = read_excel("files/CH-044 Combine Tables.xlsx", range = "C15:F18")

test   = read_excel("files/CH-044 Combine Tables.xlsx", range = "J2:O6")

result = map_dfr(list(input1, input2, input3), ~.x %>% pivot_longer(cols = -1)) %>%
  pivot_wider(names_from = name, values_from = value, values_fn = sum, values_fill = 0) %>%
  select(colnames(test))

identical(result, test)
# [1] TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

  • 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

input1 = pd.read_excel("CH-044 Combine Tables.xlsx", usecols = "C:F", nrows = 4, skiprows=2)
input2 = pd.read_excel("CH-044 Combine Tables.xlsx", usecols="C:G", skiprows = 8, nrows = 4)
input3 = pd.read_excel("CH-044 Combine Tables.xlsx", usecols="C:F", skiprows = 14, nrows = 4)
test = pd.read_excel("CH-044 Combine Tables.xlsx", usecols="J:O", skiprows = 1, nrows = 5)

input1 = input1.melt(id_vars="Regions")
input2 = input2.melt(id_vars="Regions")
input3 = input3.melt(id_vars="Regions")
result = pd.concat([input1, input2, input3])
result = result.groupby(["Regions", "variable"]).sum().reset_index()
result = result.pivot(index="Regions", columns="variable", values="value").reset_index().fillna(0)
result[result.columns[1:]] = result[result.columns[1:]].astype('int64')
result.columns.name = None

print(result.equals(test)) # True
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

    • Aggregates or ranks values at the relevant grouping level

  • 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 core logic is clear, but the correct transformation pattern is not obvious from the raw input.

  • The challenge combines multiple reshaping, grouping, or parsing steps.