Omid - Challenge 287

data-challenges
advanced-exercises
🔰 Challenge 287: Transformation!
Published

March 24, 2026

Illustration for Omid - Challenge 287

Challenge Description

🔰 Challenge 287: Transformation!

Solutions

library(tidyverse)
library(readxl)

path = "files/200-299/287/CH-287 Transformation.xlsx"
input = read_excel(path, range = "B2:E6")
test  = read_excel(path, range = "I2:K15")

result = input %>%
  pivot_longer(cols = everything(), names_to = "Date", values_to = "Value", values_drop_na = T) %>%
  mutate(Date = str_sub(Date, 1, 11)) %>%
  separate(Value, into = c("Product", "Value"), sep = "-") %>%
  mutate(Value = as.numeric(Value),
         Date = anytime::anydate(Date) %>% as.POSIXct(format = "%d/%m/%Y", tz = "UTC")) 

all.equal(result, test, check.attributes = F)
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

    • Builds the intermediate columns that drive the final result

    • Parses the text patterns directly instead of relying on manual cleanup

  • 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 = "200-299/287/CH-287 Transformation.xlsx"

input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=4)
test = pd.read_excel(path, usecols="I:K", skiprows=1, nrows=13)

result = (
    input.melt(var_name="Date", value_name="Value")
    .dropna(subset=["Value"])
    .assign(
        Date=lambda df: pd.to_datetime(df["Date"].astype(str).str[:11], format="%d/%b/%Y"),
        Product=lambda df: df["Value"].str.split("-", n=1).str[0],
        Value=lambda df: pd.to_numeric(df["Value"].str.split("-", n=1).str[1], errors="coerce"),
    )
    .sort_values(["Product", "Date"])
    .reset_index(drop=True)[["Date", "Product", "Value"]]
)

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

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

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