Excel BI - PowerQuery Challenge 326

excel-challenges
power-query
Splitter Split the Data in Table1 on the basis of splitters given in Table2. The split has to be case insensitive. Generate headers dynamically.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 326

Challenge Description

Splitter Split the Data in Table1 on the basis of splitters given in Table2. The split has to be case insensitive. Generate headers dynamically.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/300-399/326/PQ_Challenge_326.xlsx"
input1 = read_excel(path, range = "A2:B8")
input2 = read_excel(path, range = "A11:B21")
test  = read_excel(path, range = "D2:I8")

delims = input2 %>%
  group_by(ID) %>%
  summarise(
    pattern = str_c("(?i)", str_c(Splitter, collapse = "|"))
  )

result = input1 %>%
  left_join(delims, by = "ID") %>%
  mutate(
    split_col = map2(Data, pattern, ~str_split(.x, .y, simplify = TRUE)[1,]),
    Data = map(split_col, ~.x[.x != ""])
  ) %>%
  unnest_wider(Data, names_sep = "") %>%
  select(ID, starts_with("Data")) 

all.equal(result, test)
# [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

    • Aggregates or ranks values at the relevant grouping level

    • Builds helper columns that drive the final output

  • Strengths:

    • The R solution stays close to the workbook logic and keeps the transformation compact.
  • Areas for Improvement:

    • The code assumes the workbook layout and selected ranges remain stable.
  • Gem:

    • The best part of the solution is choosing the right intermediate shape before formatting the final output.
import pandas as pd
import re

path = "300-399/326/PQ_Challenge_326.xlsx"
input1 = pd.read_excel(path, usecols="A:B", skiprows=1, nrows=7)
input2 = pd.read_excel(path, usecols="A:B", skiprows=10, nrows=11)
test = pd.read_excel(path, usecols="D:I", skiprows=1, nrows=7).rename(columns=lambda c: c.replace('.1', ''))

delims = input2.groupby("ID")["Splitter"].apply(lambda x: "(?i)" + "|".join(map(re.escape, x)))
result = input1.assign(pattern=input1["ID"].map(delims))
result["split_col"] = result.apply(lambda r: [s for s in re.split(r["pattern"], r["Data"]) if s], axis=1)
max_len = result["split_col"].str.len().max()
for i in range(max_len):
    result[f"Data{i+1}"] = result["split_col"].apply(lambda x: x[i] if i < len(x) else None)
final = result[["ID"] + [f"Data{i+1}" for i in range(max_len)]]
print(final.equals(test))
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • Builds helper columns that drive the final output

    • Uses direct pattern parsing where the workbook encodes logic in text

  • Strengths:

    • The Python version follows the same workbook rule in a direct pandas-oriented implementation.
  • Areas for Improvement:

    • As with the R version, any workbook layout change would require small adjustments.
  • Gem:

    • The implementation stays close to the source challenge instead of adding unnecessary abstraction.

Difficulty Level

This task is moderate:

  • It combines reshaping, grouping, or parsing steps that are common in Power Query style problems.

  • The main challenge is reproducing the workbook output structure exactly.