library(tidyverse)
library(readxl)
input = read_excel("Power Query/PQ_Challenge_189.xlsx", range = "A1:B11")
test = read_excel("Power Query/PQ_Challenge_189.xlsx", range = "D1:F8")
result = input %>%
mutate(Result = if_else(lead(Code) == "Yes", "Pass", NA)) %>%
mutate(Result = if_else(is.na(Result) & str_detect(Code, "\\d"), "Fail", Result)) %>%
filter(!is.na(Result)) %>%
mutate(Code = as.numeric(Code))
identical(result, test)
# [1] TRUEExcel BI - PowerQuery Challenge 189
excel-challenges
power-query
Seq Code Result Fail Pass Yes

Challenge Description
Seq Code Result Fail Pass Yes
Solutions
Logic:
Reads the workbook range needed for the challenge
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
input = pd.read_excel("PQ_Challenge_189.xlsx", usecols="A:B", nrows = 10)
test = pd.read_excel("PQ_Challenge_189.xlsx", usecols="D:F", nrows = 7)
test.columns = test.columns.str.replace(".1","")
result = input.copy()
result["Result"] = result["Code"].shift(-1).eq("Yes").replace({True: "Pass", False: pd.NA})
result["Result"] = result["Result"].fillna(result["Code"].apply(lambda x: "Fail" if re.search(r"\d", str(x)) else pd.NA))
result = result.dropna(subset=["Result"]).reset_index(drop=True)
result["Code"] = pd.to_numeric(result["Code"])
print(result.equals(test)) # TrueLogic:
Reads the workbook range needed for the challenge
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.