Excel BI - PowerQuery Challenge 223

excel-challenges
power-query
Group Transpose the problem table into result table.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 223

Challenge Description

Group Transpose the problem table into result table.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_223.xlsx"
input = read_excel(path, range = "A1:D14")
test  = read_excel(path, range = "F1:J8")

result = input %>%
  unite("Code", c("Type", "Code"), sep = "") %>%
  mutate(col = ifelse(row_number() %% 2 == 0, 2, 1), 
         row = (row_number() + 1) %/% 2,
         .by = Group) %>%
  pivot_wider(names_from = col, values_from = c(Code, Value), names_sep = "") %>%
  select(-row)

all.equal(result, test)
#> [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_223.xlsx"
input = pd.read_excel(path, usecols="A:D", nrows=14, dtype={'Group': str, 'Type': str, 'Code': str, 'Value': int})
test = pd.read_excel(path, usecols="F:J", nrows=7).rename(columns=lambda x: x.replace('.1', '')).replace('NA', pd.NA)
test[['Value1', 'Value2']] = test[['Value1', 'Value2']].astype('Int64')

input['Code'] = input['Type'] + input['Code']
input = input.drop(columns=['Type'])
input['col'] = input.groupby('Group').cumcount().mod(2).add(1)
input['row'] = input.groupby('Group').cumcount().floordiv(2).add(1)

result = input.pivot(index=['Group', 'row'], columns='col', values=['Code', 'Value'])
result.columns = [f'{col[0]}{col[1]}' for col in result.columns]
result = result.reset_index().drop(columns=['row'])
result[['Value1', 'Value2']] = result[['Value1', 'Value2']].astype('Int64')

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.