Excel BI - PowerQuery Challenge 143

excel-challenges
power-query
Emp Index Value A C D
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 143

Challenge Description

Emp Index Value A C D

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_143.xlsx", range = "A1:C21")
test  = read_excel("Power Query/PQ_Challenge_143.xlsx", range = "F1:H7")

result = input %>%
  group_by(Emp, Value) %>%
  mutate(rn = row_number()) %>%
  filter(rn == 2 | (rn == max(rn) & rn > 2)) %>%
  select(-rn) %>%
  ungroup()

identical(result, test)
#> [1] 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

input_data = pd.read_excel("PQ_Challenge_143.xlsx", usecols="A:C", nrows=21)
test = pd.read_excel("PQ_Challenge_143.xlsx", usecols="F:H", nrows=7)

result = input_data.copy()
result["rn"] = result.groupby(["Emp", "Value"]).cumcount() + 1
group_sizes = result.groupby(["Emp", "Value"])["rn"].transform("max")
result = result[(result["rn"] == 2) | ((result["rn"] == group_sizes) & (result["rn"] > 2))].drop(columns="rn").reset_index(drop=True)

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

    • Reads the workbook range needed for the challenge

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