Excel BI - Excel Challenge 667

excel-challenges
excel-formulas
🔰 Date Sales Answer Expected Month-Day Mon Tue Wed Thu Fri Sat
Published

March 24, 2026

Illustration for Excel BI - Excel Challenge 667

Challenge Description

🔰 Date Sales Answer Expected Month-Day Mon Tue Wed Thu Fri Sat

Solutions

library(tidyverse)
library(readxl)

path = "Excel/667 Pivot Problem.xlsx"
input = read_excel(path, range = "A1:B21")
test  = read_excel(path, range = "D2:J11")

result = input %>%
       mutate(`Month-Day` = month(Date, label = TRUE, abbr = TRUE, locale = "en"),
                             wday = wday(Date, label = TRUE, abbr = TRUE, week_start = 1, locale = "en")) %>%
       select(-Date) %>%
       mutate(across(c(`Month-Day`, wday), as.factor)) %>%
       summarise(Sales = paste(Sales, collapse = ", "), .by = c('Month-Day', wday)) %>%
       pivot_wider(names_from = wday, values_from = Sales, names_sort = TRUE) %>%
       mutate(`Month-Day` = as.character(`Month-Day`))

all.equal(result, test, check.attributes = FALSE)
#> [1] TRUE
  • Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Aggregate or rank the data at the required grouping level; Reshape the result into the workbook output format.
  • Strengths: The reshaping step mirrors the workbook output closely instead of forcing extra post-processing.
  • 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 last reshape turns a raw transformation into something that already looks like a report.
import pandas as pd
import numpy as np

path = "667 Pivot Problem.xlsx"
input = pd.read_excel(path, usecols="A:B", nrows=21)
test = pd.read_excel(path, usecols="D:J", skiprows=1, nrows=9)
test.update(test.select_dtypes(include=[np.number]).applymap(lambda x: str(int(x)) if not pd.isna(x) else np.NaN))

input['Month-Day'] = input['Date'].dt.strftime('%b')
input['wday'] = input['Date'].dt.strftime('%a')

result = input.drop(columns=['Date']).astype({'Month-Day': 'category', 'wday': 'category'})
result = result.groupby(['Month-Day', 'wday'], observed=False)['Sales'].apply(lambda x: ', '.join(map(str, x))).unstack().reset_index()
result = result[['Month-Day', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']]

month_order = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
result['Month-Day'] = pd.Categorical(result['Month-Day'], categories=month_order, ordered=True)
result = result.sort_values('Month-Day').reset_index(drop=True)
result['Month-Day'] = result['Month-Day'].astype(str)

print(result.equals(test)) # True

The Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.

Difficulty Level

Medium

The individual steps are manageable, but the correct transformation pattern is not obvious from the raw data.