Omid - Challenge 137

data-challenges
advanced-exercises
🔰 Table Transformation!
Published

March 24, 2026

Illustration for Omid - Challenge 137

Challenge Description

🔰 Table Transformation!

Solutions

library(tidyverse)
library(readxl)
library(janitor)

path = "files/CH-137 Table Transformation.xlsx"
input = read_excel(path, range = "C2:D17")
test  = read_excel(path, range = "F2:H12") %>% mutate(Date = as.Date(Date))

result = input %>%
  mutate(Date = ifelse(is.na(`Column 2`), as.numeric(`Column 1`), NA)) %>%
  fill(Date) %>%
  na.omit() %>%
  transmute(Date = excel_numeric_to_date(Date), Product = `Column 1`, Quantity = `Column 2`)

all.equal(result, test, check.attributes = FALSE)
# [1] TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • 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-137 Table Transformation.xlsx"

input = pd.read_excel(path, usecols="C:D", skiprows=1, nrows=15)
test = pd.read_excel(path, usecols="F:H", skiprows=1, nrows=10, parse_dates=['Date'], dtype={'Quantity': 'float64'})

input['Date'] = input.apply(lambda row: row['Column 1'] if pd.isna(row['Column 2']) else None, axis=1)
input['Date'] = input['Date'].ffill()
input = input.dropna()

result = input[['Date', 'Column 1', 'Column 2']].rename(columns={'Column 1': 'Product', 'Column 2': 'Quantity'}).reset_index(drop=True)

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

    • Reads the workbook ranges needed for the challenge
  • 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.