Excel BI - PowerQuery Challenge 251

excel-challenges
power-query
Pivot the problem table into result table. In result
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 251

Challenge Description

Pivot the problem table into result table. In result

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_251.xlsx"
input = read_excel(path, range = "A1:E21")
test  = read_excel(path, range = "G1:O6")

result = bind_rows(
  input %>% select(`Emp ID` = 1, Attr = 2, Val = 3),
  input %>% select(`Emp ID` = 1, Attr = 4, Val = 5)
) %>%
  na.omit() %>%
  pivot_wider(names_from = Attr, values_from = Val) %>%
  separate(`Full Name`, into = c("First Name", "Last Name"), sep = " ") %>%
  select(`Emp ID`, `First Name`, `Last Name`, `Gender`, `Date of Birth`, Weight, Salary, State,Sales) %>%
  mutate(across(c(Weight, Salary, Sales), as.numeric),
         `Date of Birth` = as.POSIXct(janitor::excel_numeric_to_date(as.numeric(`Date of Birth`)))) 

all.equal(result, test, check.attributes = FALSE)
#> [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_251.xlsx"
input = pd.read_excel(path, sheet_name=0, usecols="A:E", nrows=21)
test = pd.read_excel(path, sheet_name=0, usecols="G:O", nrows=5).rename(columns=lambda x: x.split('.')[0])\
    .sort_values("Emp ID").reset_index(drop=True)

input_long = pd.concat([
    input[['Emp ID', 'Attribute1', 'Value1']].rename(columns={'Attribute1': 'Attr', 'Value1': 'Val'}),
    input[['Emp ID', 'Attribute2', 'Value2']].rename(columns={'Attribute2': 'Attr', 'Value2': 'Val'})
])

input_long = input_long.dropna()
result = input_long.pivot(index='Emp ID', columns='Attr', values='Val').reset_index()
result[['First Name', 'Last Name']] = result['Full Name'].str.split(' ', expand=True)
result = result[['Emp ID', 'First Name', 'Last Name', 'Gender', 'Date of Birth', 'Weight', 'Salary', 'State', 'Sales']]
result[['Weight', 'Salary', 'Sales']] = result[['Weight', 'Salary', 'Sales']].apply(pd.to_numeric)
result['Date of Birth'] = pd.to_datetime(result['Date of Birth'], errors='coerce')
result = result.sort_values(by = 'Emp ID').reset_index(drop=True)
result.index.name = None

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

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