Excel BI - PowerQuery Challenge 261

excel-challenges
power-query
Transpose the given data from Problem Table into Result table as shown
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 261

Challenge Description

Transpose the given data from Problem Table into Result table as shown

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_261.xlsx"
input = read_excel(path, range = "A1:B13")
test  = read_excel(path, range = "D1:F12")

result = input %>%
  mutate(Country = ifelse(Data1 == "Country", Data2, NA),
         State = ifelse(Data1 == "State", Data2, NA),
         Cities = ifelse(Data1 == "Cities", Data2, NA)) %>%
  fill(Country, State) %>%
  select(-Data1, -Data2) %>%
  filter(!is.na(Cities)) %>%
  separate_rows(Cities, sep = ", ") %>%
  mutate(across(c(Country, State, Cities), str_trim)) %>%
  mutate(Country = ifelse(row_number() == 1, Country, NA), .by = Country) %>%
  mutate(State = ifelse(row_number() == 1, State, NA), .by = State)

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

    • Reads the workbook range needed for the challenge

    • 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

path = "PQ_Challenge_261.xlsx"

input = pd.read_excel(path, usecols="A:B", nrows=13)
test  = pd.read_excel(path, usecols="D:F", nrows=11)

result = input.copy()
for col in ["Country","State","Cities"]:
    result[col] = np.where(result["Data1"].eq(col), result["Data2"], np.nan)
result[["Country","State"]] = result[["Country","State"]].ffill()
result = result.drop(columns=["Data1","Data2"]).dropna(subset=["Cities"]).reset_index(drop=True)

result = (
    result.assign(Cities=result["Cities"].str.split(", "))
          .explode("Cities")
          .assign(
              Country=lambda df: df["Country"].str.strip(),
              State=lambda df: df["State"].str.strip(),
              Cities=lambda df: df["Cities"].str.strip()
          )
          .reset_index(drop=True)
)
for col in ["Country","State"]:
    result[col] = (
        result.groupby(col)[col]
              .apply(lambda x: x.where(x.index == x.index[0]))
              .droplevel(0)
    )

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

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