Excel BI - PowerQuery Challenge 264

excel-challenges
power-query
Row_Col C1 C2 C3 C4 R1
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 264

Challenge Description

Row_Col C1 C2 C3 C4 R1

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_264.xlsx"
input = read_excel(path, range = "A1:E7")
test  = read_excel(path, range = "A11:F17")

result = input %>%
  pivot_longer(-c(1), names_to = "col", values_to = "Alphabet", values_drop_na = T) %>%
  unite("Address", Row_Col, col, sep = "") %>%
  arrange(Alphabet, Address) %>%
  mutate(rn = row_number(), .by = Alphabet) %>%
  pivot_wider(names_from = rn, names_glue = "Address{rn}", values_from = Address)

all.equal(result, test, check.attributes = F)
#> [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

    • 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

path = "PQ_Challenge_264.xlsx"
input = pd.read_excel(path, usecols="A:E", nrows=7)
test = pd.read_excel(path,  usecols="A:F", skiprows=10, nrows=7)

input_long = input.melt(id_vars=input.columns[0], var_name="col", value_name="Alphabet").dropna()
input_long['Address'] = input_long[input.columns[0]].astype(str) + input_long['col']
input_long = input_long.sort_values(by=['Alphabet', 'Address'])
input_long['rn'] = input_long.groupby('Alphabet').cumcount() + 1

result = input_long.pivot(index='Alphabet', columns='rn', values='Address').reset_index()
result.columns = ['Alphabet'] + [f'Address{col}' for col in result.columns if col != 'Alphabet']

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

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