Excel BI - PowerQuery Challenge 214

excel-challenges
power-query
Zoo Animals Count Zoo1 Zoo1 Count Zoo2
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 214

Challenge Description

Zoo Animals Count Zoo1 Zoo1 Count Zoo2

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_214.xlsx"
input = read_excel(path, range = "A1:C13")
test  = read_excel(path, range = "E1:J7")

result = input %>%
  arrange(Animals, .by = Zoo) %>%
  mutate(nr = row_number(), .by = Zoo) %>%
  pivot_wider(
    names_from = Zoo,
    values_from = c(Animals, Count),
    names_glue = "{.value}_{Zoo}"
  ) %>%
  select(contains("Zoo1"), contains("Zoo2"), contains("Zoo3"))

colnames(result) = colnames(test)
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_214.xlsx"
input = pd.read_excel(path, usecols="A:C")
test = pd.read_excel(path, usecols="E:J", nrows=6)

test[test.columns[test.columns.str.contains("Count")]] = test[test.columns[test.columns.str.contains("Count")]].astype("float64")

result = input.sort_values(by=["Animals", "Zoo"]).assign(nr=lambda x: x.groupby("Zoo").cumcount() + 1).pivot(index="nr", columns="Zoo", values=["Animals", "Count"]).reset_index(drop=True)
result.columns = [' '.join(col).strip() for col in result.columns.values]
result = result[["Animals Zoo1", "Count Zoo1", "Animals Zoo2", "Count Zoo2", "Animals Zoo3", "Count Zoo3"]]
result.columns = test.columns
result[result.columns[result.columns.str.contains("Count")]] = result[result.columns[result.columns.str.contains("Count")]].astype("float64")

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

    • Builds helper columns that drive the final output

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