Excel BI - PowerQuery Challenge 236

excel-challenges
power-query
Transpose the data as shown
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 236

Challenge Description

Transpose the data as shown

Solutions

library(tidyverse)
  library(readxl)
  
  path = "Power Query/PQ_Challenge_236.xlsx"
  input = read_excel(path, range = "A1:F5")
  test  = read_excel(path, range = "I1:J16")  %>%
    mutate(
      Data2 = case_when(
        Data1 == "Date" & !is.na(as.numeric(Data2)) ~ as.character(as.Date(as.numeric(Data2), origin = "1899-12-30")),
        TRUE ~ as.character(Data2)
    )
  )
  
  result = input %>%
    mutate(across(everything(), as.character)) %>%
    mutate(nr = c(1,1,2,2)) %>%
    pivot_longer(names_to = "Data1", values_to = "Data2", cols = -nr) %>%
    na.omit() %>% 
    distinct() %>%
    select(-nr) 

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_236.xlsx"

input = pd.read_excel(path, usecols="A:F", nrows=4)
test = pd.read_excel(path, usecols="I:J", nrows=16)

result = input.T.values.tolist()
result = list(zip(*result))
result = [item for sublist in result for item in sublist]

result = pd.DataFrame(result, columns=["Data2"])
result["Data1"] = input.columns.tolist() * 4
result = result[["Data1", "Data2"]]
result = result.dropna()

result["Count"] = result.groupby("Data2").cumcount() + 1
result = result[~((result["Count"] == 2) & (result["Data1"] == "Hall"))]
result = result.drop(columns="Count").reset_index(drop=True)

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