Excel BI - PowerQuery Challenge 322

excel-challenges
power-query
For year 2024 only (not for 2023 and 2025), pivot total number of working days for each employee. Month names and employee names should be sorted.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 322

Challenge Description

For year 2024 only (not for 2023 and 2025), pivot total number of working days for each employee. Month names and employee names should be sorted.

Solutions

library(tidyverse)
library(readxl)
Sys.setlocale("LC_TIME", "C")

path = "Power Query/300-399/322/PQ_Challenge_322.xlsx"
input = read_excel(path, range = "A1:C20")
test  = read_excel(path, range = "E1:J12")

result = input %>%
  rowwise() %>%
  mutate(date = list(seq(`From Date`, `To Date`, by = "day"))) %>%
  unnest(date) %>%
  separate_longer_delim(Employees, delim = ", ") %>%
  ungroup() %>%
  filter(year(date) == 2024) %>%
  select(date, Employees) %>% 
  mutate(Month = month(date, label = TRUE, abbr = FALSE),
         Month = factor(Month, levels = month.name, ordered = TRUE),
         is_weekend = if_else(wday(date) %in% c(1, 7), "Weekend", "Weekday")) %>%
  filter(is_weekend == "Weekday") %>%
  count(Month, Employees, name = "Count") %>%
  complete(Month, Employees, fill = list(Count = 0))  %>%
  pivot_wider(names_from = Employees, values_from = Count, values_fill = 0) %>%
  filter(Month != "October") %>%
  mutate(Month = as.character(Month)) 

all.equal(result, test, check.attributes = FALSE)
# 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
import numpy as np
from pandas.tseries.offsets import DateOffset

path = "300-399/322/PQ_Challenge_322.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=20)
test = pd.read_excel(path, usecols="E:J", nrows=11)

df = input.copy()
df['date'] = [pd.date_range(f, t) for f, t in zip(df['From Date'], df['To Date'])]
df = df.explode('date')
df = df[df['date'].dt.year == 2024]
df = df.assign(Employees=df['Employees'].str.split(', ')).explode('Employees')
df['Month'] = df['date'].dt.month_name()
df = df[df['date'].dt.weekday < 5]
result = df.groupby(['Month', 'Employees']).size().unstack(fill_value=0)
order = ['January','February','March','April','May','June','July','August','September','October','November','December']
result = result.reindex(order).fillna(0).astype(int).reset_index()
result = result[result['Month'] != 'October'].reset_index(drop=True)

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

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • Builds helper columns that drive the final output

    • Applies the rule iteratively until the output is complete

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