library(tidyverse)
library(readxl)
path = "files/CH-158 Filter the last transaction in mounth.xlsx"
input = read_excel(path, range = "B2:D14")
test = read_excel(path, range = "F2:H6")
result = input %>%
group_by(`Product ID`, month = month(Date)) %>%
filter(Date == max(Date)) %>%
ungroup() %>%
select(-month)
all.equal(result, test)
# [1] TRUEOmid - Challenge 158
data-challenges
advanced-exercises
🔰 : Filter Last Transaction!

Challenge Description
🔰 : Filter Last Transaction!
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
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-158 Filter the last transaction in mounth.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=12)
test = pd.read_excel(path, usecols="F:H", skiprows=1, nrows=4).rename(columns=lambda x: x.split('.')[0])
input['Date'] = pd.to_datetime(input['Date'])
input['month'] = input['Date'].dt.month
result = input.loc[input.groupby(['Product ID', 'month'])['Date'].idxmax()].sort_values(by='Date').reset_index(drop=True).drop(columns=['month'])
print(result.equals(test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
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.