Excel BI - PowerQuery Challenge 309

excel-challenges
power-query
Groups Group1 Group2 Group3 Group4 Group5
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 309

Challenge Description

Groups Group1 Group2 Group3 Group4 Group5

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/300-399/309/PQ_Challenge_309.xlsx"
input = read_excel(path, range = "A1:A20")
test  = read_excel(path, range = "C1:D6")

group_consecutive_under_limit = function(x, limit = 50) {
  group <- 1
  subtotal <- 0
  
  map2_dbl(x, seq_along(x), function(val, i) {
    if (subtotal + val > limit) {
      group <<- group + 1
      subtotal <<- val
    } else {
      subtotal <<- subtotal + val
    }
    group
  })
}

result = input %>%
  mutate(Groups = paste0("Group",group_consecutive_under_limit(Values, 50))) %>%
  summarise(Values = paste(Values, collapse = ", "), .by = Groups)

all.equal(result, 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

  • 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

# Read Excel file
path = "300-399/309/PQ_Challenge_309.xlsx"
input = pd.read_excel(path, usecols="A", nrows=20)
test = pd.read_excel(path, usecols="C:D", nrows=5)\
    .rename(columns=lambda x: x.replace('.1', ''))\
    .astype(str)

def group_consecutive_under_limit(x, limit=50):
    groups = []
    group_labels = []
    group = 1
    subtotal = 0
    for val in x:
        if subtotal + val > limit:
            group += 1
            subtotal = val
        else:
            subtotal += val
        groups.append(group)
        group_labels.append(f'Group{group}')
    return group_labels

input['Groups'] = group_consecutive_under_limit(input.iloc[:,0], 50)

result = (
    input
    .groupby('Groups', as_index=False)
    .agg({'Values': lambda x: ', '.join(map(str, x))})
)

print(result.equals(test)) # True
  • 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 easy to moderate:

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