Excel BI - Excel Challenge 894

excel-challenges
excel-formulas
🔰 Split and stack the text and values as shown.
Published

March 24, 2026

Illustration for Excel BI - Excel Challenge 894

Challenge Description

🔰 Split and stack the text and values as shown.

Solutions

library(tidyverse)
library(readxl)

path <- "Excel/800-899/894/894 Split Stack.xlsx"
input <- read_excel(path, range = "A2:A22")
test <- read_excel(path, range = "C2:E33")

result = input %>%
  separate(
    1,
    into = c("first", "rest"),
    sep = ",",
    extra = "merge",
    fill = "right"
  ) %>%
  separate_wider_delim(
    rest,
    delim = ",",
    names_sep = "_",
    too_few = "align_start"
  ) %>%
  separate_longer_delim(c(rest_1, rest_2), delim = "|") %>%
  rename(TicketID = first, Items = rest_1, Costs = rest_2) %>%
  mutate(TicketID = as.numeric(TicketID), Costs = as.numeric(Costs))

all.equal(result, test, check.attributes = FALSE)
# [1] TRUE
  • Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Parse the packed text or string structure.
  • 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 = "Excel/800-899/894/894 Split Stack.xlsx"
input = pd.read_excel(path, usecols="A", skiprows=1, nrows=20)
test = pd.read_excel(path, usecols="C:E", skiprows=1, nrows=32)

split = input['TicketID,Items,Costs'].str.split(',', n=2, expand=True)
split.columns = ['TicketID', 'Items', 'Costs']
split = split.assign(
    Items=split['Items'].str.split('|'),
    Costs=split['Costs'].str.split('|')
).explode(['Items', 'Costs'])
split['TicketID'] = pd.to_numeric(split['TicketID'], errors='coerce')
split['Costs'] = pd.to_numeric(split['Costs'], errors='coerce')
result = split.reset_index(drop=True)

print(result.equals(test))
# True

The Python version mirrors the same workbook logic with a concise, direct implementation.

Difficulty Level

Easy / Medium

The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.