library(tidyverse)
library(readxl)
path = "files/200-299/265/CH-265 Table Transformation.xlsx"
input = read_excel(path, range = "B2:D9")
test = read_excel(path, range = "F2:I8")
result = input %>%
mutate(Region = ifelse(str_detect(Column1, "Region"), Column1, NA)) %>%
fill(Region) %>%
filter(!duplicated(Column1),
Column1 != Region) %>%
setNames(c("Product","Jan","Feb","Region")) %>%
slice(-1) %>%
pivot_longer(cols = c("Jan", "Feb"),
names_to = "Month",
values_to = "Value") %>%
relocate(Region, .before = Product) %>%
mutate(Value = as.numeric(Value))
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEOmid - Challenge 265
data-challenges
advanced-exercises
🔰 Table Transformation!

Challenge Description
🔰 Table Transformation!
Solutions
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/265/CH-265 Table Transformation.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=8)
test = pd.read_excel(path, usecols="F:I", skiprows=1, nrows=6)
input['Region'] = input['Column1'].where(input['Column1'].str.contains('Region'), pd.NA)
input['Region'] = input['Region'].ffill()
input = input[~input['Column1'].duplicated()]
input = input[input['Column1'] != input['Region']]
input.columns = ["Product", "Jan", "Feb", "Region"]
input = input.iloc[1:]
result = input.melt(id_vars=['Region', 'Product'], value_vars=['Jan', 'Feb'],
var_name='Month', value_name='Value')
result = result[['Region', 'Product', 'Month', 'Value']]\
.sort_values(by=['Region', 'Product', 'Month'],ascending=[True, True, False]).reset_index(drop=True)
result['Value'] = pd.to_numeric(result['Value'])
print(result.equals(test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Reshapes the data into the grain required by the task
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.