Excel BI - PowerQuery Challenge 238

excel-challenges
power-query
Item Store Status A Closed Closed-All 1
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 238

Challenge Description

Item Store Status A Closed Closed-All 1

Solutions

library(tidyverse)
library(readxl)
library(glue)

path = "Power Query/PQ_Challenge_238.xlsx"
input = read_excel(path, range = "A1:C12")
test  = read_excel(path, range = "E1:F6")

result = input %>%
  pivot_wider(names_from = Status, values_from = Store, values_fn = length) %>%
  mutate(Status = case_when(
    is.na(Open) & !is.na(Closed) ~ glue("Closed-All {Closed}"),
    !is.na(Open) & is.na(Closed) ~ glue("Open-All {Open}"),
    TRUE ~ glue("Open-{Open}, Closed-{Closed}")
  ) %>% as.character()) %>%
  select(Item, Status)

all.equal(result, test, check.attributes = FALSE)
#> [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

  • 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

path = "PQ_Challenge_238.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=12)
test = pd.read_excel(path, usecols="E:F", nrows=5).rename(columns=lambda x: x.split('.')[0])

result = input.pivot_table(index='Item', columns='Status', values='Store', aggfunc='size', fill_value=0).reset_index()
def determine_size(row):
    return f"Closed-All {row['Closed']}" if row['Open'] == 0 else f"Open-All {row['Open']}" if row['Closed'] == 0 else f"Open-{row['Open']}, Closed-{row['Closed']}"

result['Status'] = result.apply(determine_size, axis=1)
result = result[['Item', 'Status']]
result = result.rename_axis(None, axis=1)

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

    • Reads the workbook range needed for the challenge

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

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