library(tidyverse)
library(readxl)
path = "Excel/800-899/824/824 Group By Consecutive Dates.xlsx"
input = read_excel(path, range = "A2:B15")
test = read_excel(path, range = "D2:E6")
result = input %>%
mutate(Dates = ymd(Dates)) %>%
mutate(group = cumsum(Dates - lag(Dates, default = first(Dates)) > 1)) %>%
summarise(`Date Range` = ifelse(min(Dates) == max(Dates),
as.character(min(Dates)),
paste(min(Dates), max(Dates), sep = " To ")),
Total = sum(Value, na.rm = T), .by = group) %>%
select(-group)
all.equal(result, test)
# [1] TRUEExcel BI - Excel Challenge 824
excel-challenges
excel-formulas
🔰 Answer Expected Dates Value Date Range Total 2024-01-01 2024-01-01 To 2024-01-03 2024-01-02 2024-01-06 To 2024-01-10 2024-01-03

Challenge Description
🔰 Answer Expected Dates Value Date Range Total 2024-01-01 2024-01-01 To 2024-01-03 2024-01-02 2024-01-06 To 2024-01-10 2024-01-03
Solutions
- Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Aggregate or rank the data at the required grouping level; Apply the business rule conditions explicitly.
- Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
- Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
- Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
import pandas as pd
path = "800-899/824/824 Group By Consecutive Dates.xlsx"
input = pd.read_excel(path, usecols="A:B", skiprows=1, nrows=14)
test = pd.read_excel(path, usecols="D:E", skiprows=1, nrows=4)
input['Dates'] = pd.to_datetime(input['Dates'])
input['group'] = (input['Dates'].diff().dt.days.gt(1)).cumsum()
result = (
input.groupby('group')
.agg(DateRange=('Dates', lambda g: f"{g.min().date()}" if g.min() == g.max() else f"{g.min().date()} To {g.max().date()}"),
Total=('Value', 'sum'))
.rename(columns={'DateRange': 'Date Range'})
.reset_index(drop=True)
)
print(result.equals(test)) # TrueThe Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.
Difficulty Level
Easy / Medium
The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.