Excel BI - PowerQuery Challenge 207

excel-challenges
power-query
Name Sun Mon Tue Wed Thu
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 207

Challenge Description

Name Sun Mon Tue Wed Thu

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_207.xlsx"
input = read_excel(path, range = "A2:H13")
test  = read_excel(path, range = "K2:P9")

r1 = input %>%
  pivot_longer(names_to = "Day of Week",  values_to = "Value", cols = -c(1)) 

r1$`Day of Week` = factor(r1$`Day of Week`, 
                          levels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), 
                          ordered = TRUE)

r2 = r1 %>%
  filter(Value == "Y") %>%
  mutate(nr = row_number(), .by = `Day of Week`) %>%
  select(-Value) %>%
  pivot_wider(names_from = nr, values_from = Name, names_glue = "Name{nr}") %>%
  complete(`Day of Week`) %>%
  mutate(`Day of Week` = as.character(`Day of Week`))

all.equal(r2, 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_207.xlsx"
input = pd.read_excel(path, usecols="A:H", skiprows=1)
test = pd.read_excel(path,  usecols="K:P", skiprows=1, nrows = 7)

r2 = (
    input.melt(id_vars=["Name"], var_name="Day of Week", value_name="Value")
    .query('Value == "Y"')
    .groupby("Day of Week", observed=False)
    .apply(lambda x: x.assign(nr=x.groupby("Day of Week", observed=False).cumcount() + 1))
    .drop("Value", axis=1)
    .pivot(index="Day of Week", columns="nr", values="Name")
    .add_prefix("Name")
    .reindex(["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"])
    .reset_index()
    .astype({"Day of Week": str})
    .rename_axis(None, axis=1)
)
print(r2.equals(test)) # 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 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.