Excel BI - PowerQuery Challenge 191

excel-challenges
power-query
Extract the words which are either numbers followed by all uppercase English letters or vice versa. There may be special characters in the words. The output need to be shown in uppercase English letters_numbers
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 191

Challenge Description

Extract the words which are either numbers followed by all uppercase English letters or vice versa. There may be special characters in the words. The output need to be shown in uppercase English letters_numbers

Solutions

library(tidyverse)
library(readxl)
library(rebus)

path = "Power Query/PQ_Challenge_191.xlsx"
input = read_excel(path, range = "A1:A11")
test = read_excel(path, range = "A1:B11") 

pattern1 = "\\b[A-Z]+(?:[!@#$%^&*_+=]*[A-Z]*)*[!@#$%^&*_+=]*[0-9]+(?:[!@#$%^&*_+=]*[0-9]*)*\\b"
pattern2 = "\\b[0-9]+(?:[!@#$%^&*_+=]*[0-9]*)*[!@#$%^&*_+=]*[A-Z]+(?:[!@#$%^&*_+=]*[A-Z]*)*\\b"

order_chars = function(text) {
  text = str_replace_all(text, "[^[:alnum:]]", "")
  letters = str_extract_all(text, "[A-Z]")[[1]] %>% paste0(collapse = "")
  numbers = str_extract_all(text, "[0-9]")[[1]] %>% paste0(collapse = "")
  result = paste0(letters, "_", numbers)
  return(result)
}


result = input %>%
  mutate(pat1 = str_extract_all(Text, pattern1),
         pat2 = str_extract_all(Text, pattern2)) %>%
  mutate(ext = map2(pat1, pat2, ~c(.x, .y))) %>%
  select(-c(pat1, pat2)) %>%
  unnest(ext, keep_empty = T) %>%
  mutate(result = map_chr(ext, order_chars)) %>%
  group_by(Text) %>%
  summarise(`Answer Expected` = paste0(result, collapse = ", ")) %>%
  mutate(`Answer Expected` = if_else(`Answer Expected` == "NA_NA", NA_character_, `Answer Expected`))

res = left_join(test, result, by = c("Text" = "Text"))
  • 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 = "PQ_Challenge_191.xlsx"
input = pd.read_excel(path, usecols="A", nrows=10)
test = pd.read_excel(path, usecols="A:B", nrows=10)

pattern1 = r"\b[A-Z]+(?:[!@#$%^&*_+=]*[A-Z]*)*[!@#$%^&*_+=]*[0-9]+(?:[!@#$%^&*_+=]*[0-9]*)*\b"
pattern2 = r"\b[0-9]+(?:[!@#$%^&*_+=]*[0-9]*)*[!@#$%^&*_+=]*[A-Z]+(?:[!@#$%^&*_+=]*[A-Z]*)*\b"

def order_chars(text):
    text = str(text)  # Convert to string
    text = re.sub(r"[^a-zA-Z0-9]", "", text)
    letters = re.findall(r"[A-Z]", text)
    numbers = re.findall(r"[0-9]", text)
    result = "".join(letters) + "_" + "".join(numbers)
    return result

result = input.copy()
result["pat1"] = result["Text"].str.findall(pattern1)
result["pat2"] = result["Text"].str.findall(pattern2)
result["ext"] = result.apply(lambda row: row["pat1"] + row["pat2"], axis=1)
result = result.explode("ext")
result["result"] = result["ext"].apply(order_chars)
result = result.groupby("Text").agg({"result": lambda x: ", ".join(x)}).reset_index()
result["Answer Expected"] = result["result"].apply(lambda x: x if x != "_" else None)
result = result.drop(columns=["result"])
result = pd.merge(test, result, on="Text", how="left")
print(result)
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • 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.