Excel BI - PowerQuery Challenge 295

excel-challenges
power-query
Transpose the data as shown from hierarchal table to result table.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 295

Challenge Description

Transpose the data as shown from hierarchal table to result table.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/200-299/295/PQ_Challenge_295.xlsx"
input = read_excel(path, range = "A1:B18")
test = read_excel(path, range = "D1:G12")

result = input %>%
  mutate(Serial = as.character(Serial)) %>%
  mutate(level = str_count(Serial, "\\.")) %>%
  mutate(
    Names1 = ifelse(str_count(Serial, "\\.") == 0, Names, NA_character_),
    Names2 = ifelse(str_count(Serial, "\\.") == 1, Names, NA_character_),
    Names3 = ifelse(str_count(Serial, "\\.") == 2, Names, NA_character_)
  ) %>%
  mutate(first_digit = substr(Serial, 1, 1)) %>%
  group_by(first_digit) %>%
  fill(Names1, Names2, .direction = "down") %>%
  fill(Names2, Names3, .direction = "up") %>%
  select(Serial = first_digit, Names1, Names2, Names3) %>%
  mutate(Serial = as.integer(Serial)) %>%
  distinct()

all.equal(result, test, check.attributes = FALSE)
# [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

    • Uses direct pattern parsing where the workbook encodes logic in text

  • 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
import numpy as np
import re

path = "200-299/295/PQ_Challenge_295.xlsx"
input = pd.read_excel(path, usecols="A:B", nrows=18)
test = pd.read_excel(path, usecols="D:G", nrows=11)
test.columns = [col.replace('.1', '') for col in test.columns]

df = input.copy()
df['Serial'] = df['Serial'].astype(str)
df['level'] = df['Serial'].str.count(r'\.')
for i in range(3):
    df[f'Name{i+1}'] = np.where(df['level'] == i, df['Names'], pd.NA)

df['first_digit'] = df['Serial'].str[0]

df[['Name1', 'Name2']] = df.groupby('first_digit')[['Name1', 'Name2']].ffill()
df[['Name2', 'Name3']] = df.groupby('first_digit')[['Name2', 'Name3']].bfill()

result = df[['first_digit', 'Name1', 'Name2', 'Name3']].copy()
result = result.rename(columns={'first_digit': 'Serial'})
result['Serial'] = result['Serial'].astype(int)
result = result.drop_duplicates().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

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