Excel BI - PowerQuery Challenge 142

excel-challenges
power-query
Employee Start Time End Time Time Count A
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 142

Challenge Description

Employee Start Time End Time Time Count A

Solutions

library(tidyverse)
library(readxl)
library(lubridate)

input = read_excel("Power Query/PQ_Challenge_142.xlsx", range = "A1:C4") 
  %>% janitor::clean_names()
test = read_excel("Power Query/PQ_Challenge_142.xlsx", range = "E1:F49")

input <- input %>%
  mutate(interval = interval(ymd_hms(start_time), ymd_hms(end_time)))

quarter_table <- tibble(
  interval = interval(
    seq(ymd_hms("1899-12-31 09:00:00"), 
        ymd_hms("1899-12-31 20:45:00"), 
        by = "15 mins"),
    seq(ymd_hms("1899-12-31 09:14:59"), 
        ymd_hms("1899-12-31 20:59:59"), 
        by = "15 mins")
  )
)
head_count <- quarter_table %>%
  mutate(
    Count = map_dbl(interval, ~sum(int_overlaps(.x, input$interval))),
    Time = paste(format(int_start(interval), "%I:%M:%S %p"), 
                 format(int_end(interval), "%I:%M:%S %p"), 
                 sep = " - ")
  ) %>%
  select(Time, Count)
  • Logic:

    • Reads the workbook range needed for the challenge

    • 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

input_data = pd.read_excel("PQ_Challenge_142.xlsx", usecols="A:C", nrows=4)
input_data.columns = [c.strip().lower().replace(" ", "_") for c in input_data.columns]
test = pd.read_excel("PQ_Challenge_142.xlsx", usecols="E:F", nrows=49)

intervals = list(zip(pd.to_datetime(input_data["start_time"]), pd.to_datetime(input_data["end_time"])))
starts = pd.date_range("1899-12-31 09:00:00", "1899-12-31 20:45:00", freq="15min")
ends = pd.date_range("1899-12-31 09:14:59", "1899-12-31 20:59:59", freq="15min")

rows = []
for start, end in zip(starts, ends):
    count = sum((start <= e) and (end >= s) for s, e in intervals)
    rows.append({
        "Time": f"{start.strftime('%I:%M:%S %p')} - {end.strftime('%I:%M:%S %p')}",
        "Count": count,
    })

result = pd.DataFrame(rows)
print(result.equals(test))
  • Logic:

    • Reads the workbook range needed for the challenge

    • 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 easy to moderate:

  • The transformation rule is readable, but the final layout still requires a careful implementation.