library(tidyverse)
library(readxl)
input = read_excel("Power Query/PQ_Challenge_170.xlsx", range = "A1:C92")
test = read_excel("Power Query/PQ_Challenge_170.xlsx", range = "E1:H3")
result = input %>%
mutate(week_part = ifelse(wday(Date) %in% c(1, 7), "Weekend", "Weekday")) %>%
summarise(total = sum(Sale),
.by = c(week_part, Item)) %>%
mutate(min = min(total),
max = max(total),
full_total = sum(total),
.by = c(week_part)) %>%
filter(total == min | total == max) %>%
mutate(min_max = ifelse(total == min, "min", "max")) %>%
select(-c(total, min, max)) %>%
pivot_wider(names_from = min_max, values_from = Item, values_fn = list(Item = list)) %>%
mutate(min = map_chr(min, ~paste(.x, collapse = ", ")),
max = map_chr(max, ~paste(.x, collapse = ", ")))
colnames(result) <- colnames(test)
identical(result, test)
# [1] TRUEExcel BI - PowerQuery Challenge 170

Challenge Description
Calculate the sum of sale for Weekdays and Weekends. Also list down the highest and lowest selling items on the basis of total sale
Solutions
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 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 = pd.read_excel("PQ_Challenge_170.xlsx", sheet_name="Sheet1", usecols="A:C")
test = pd.read_excel("PQ_Challenge_170.xlsx", sheet_name="Sheet1", usecols="E:H", nrows=2)
result = input.copy()
result["week_part"] = result["Date"].apply(lambda x: "Weekend" if pd.to_datetime(x).weekday() in [5, 6] else "Weekday")
result["total"] = result.groupby(["week_part", "Item"])["Sale"].transform("sum")
result["min"] = result.groupby(["week_part"])["total"].transform("min")
result["max"] = result.groupby(["week_part"])["total"].transform("max")
result["full_total"] = result.groupby(["week_part"])["Sale"].transform("sum")
result = result.drop(columns=["Date"])
result["min_max"] = result.apply(lambda row: "min" if row["total"] == row["min"] else ("max" if row["total"] == row["max"] else "none"), axis=1)
result = result[result["min_max"] != "none"].reset_index(drop=True)
result = result[["Item", "week_part", "min_max", "full_total"]].drop_duplicates().reset_index(drop=True)
result["Item"] = result.groupby(["full_total", "min_max", "week_part"])["Item"].transform(lambda x: ", ".join(x))
result = result.drop_duplicates().reset_index(drop=True)
result = result.pivot(index=["week_part", "full_total"], columns="min_max", values="Item").reset_index()
result.columns = test.columns
print(result.equals(test)) # TrueLogic:
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
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.