library(tidyverse)
library(readxl)
input = read_excel("Power Query/PQ_Challenge_144.xlsx", range = "A1:B16")
test = read_excel("Power Query/PQ_Challenge_144.xlsx", range = "E1:G16")
result = input %>%
group_by(Group) %>%
mutate(Half = ifelse(row_number() <= ceiling(n()/2), "First", "Second")) %>%
ungroup() %>%
group_by(Group, Half) %>%
mutate(`Running Total` = cumsum(Value)) %>%
ungroup() %>%
select(-Half)
identical(result, test)
#> [1] TRUEExcel BI - PowerQuery Challenge 144
excel-challenges
power-query
Group Divide the data set for a group in 2 halves. In case of odd number of entries say n, first half will be (n+1)/2 and second half will be (n-1)/2.

Challenge Description
Group Divide the data set for a group in 2 halves. In case of odd number of entries say n, first half will be (n+1)/2 and second half will be (n-1)/2.
Solutions
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
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_144.xlsx", usecols="A:B", nrows=16)
test = pd.read_excel("PQ_Challenge_144.xlsx", usecols="E:G", nrows=16)
result = input_data.copy()
group_pos = result.groupby("Group").cumcount() + 1
group_size = result.groupby("Group")["Value"].transform("size")
result["Half"] = ["First" if pos <= (size + 1) // 2 else "Second" for pos, size in zip(group_pos, group_size)]
result["Running Total"] = result.groupby(["Group", "Half"])["Value"].cumsum()
result = result.drop(columns="Half")
print(result.equals(test))Logic:
Reads the workbook range needed for the challenge
Aggregates or ranks values at the relevant grouping level
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.