Excel BI - PowerQuery Challenge 199

excel-challenges
power-query
Extract part no. and dates from the problem table into result table as shown. Sort on part no. and date.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 199

Challenge Description

Extract part no. and dates from the problem table into result table as shown. Sort on part no. and date.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_199.xlsx"
input = read_excel(path, range = "A1:A5")
test  = read_excel(path, range = "C1:D8")

pattern_no = "\\d{3}"
pattern_date = "\\d{1,2}/+\\d{1,2}/+\\d{2}"

result = input %>%
  mutate(`Part No.` = str_extract_all(String, pattern_no),
         Date = str_extract_all(String, pattern_date)) %>%
  unnest(Date, `Part No.`) %>%
  mutate(Date = str_replace_all(Date, "//", "/")) %>%
  select(-String) %>%
  mutate(`Part No.` = as.numeric(`Part No.`),
         Date = as.POSIXct(Date, format = "%m/%d/%y", tz = "UTC")) %>%
  arrange(`Part No.`, Date) 
  
  
identical(result, test)
# [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

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

    • Builds helper columns that drive the final output

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

  • 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 = "PQ_Challenge_199.xlsx"
input = pd.read_excel(path, usecols="A", nrows = 4)
test = pd.read_excel(path, usecols="C:D", nrows = 8)

pattern_no = r"\d{3}"
pattern_date = r"\d{1,2}/+\d{1,2}/+\d{2}"

result = input.copy()
result['Part No.'] = result['String'].str.findall(pattern_no)
result['Date'] = result['String'].str.findall(pattern_date)
result = result.explode('Part No.').explode('Date')
result['Date'] = result['Date'].str.replace("//", "/")
result = result.drop(columns=['String'])
result['Part No.'] = pd.to_numeric(result['Part No.'])
result['Date'] = pd.to_datetime(result['Date'], format="%m/%d/%y")
result = result.sort_values(['Part No.', 'Date']).reset_index(drop=True)

print(result.equals(test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge
  • 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.