Excel BI - PowerQuery Challenge 148

excel-challenges
power-query
Fruits Apple Banana Dragonfruit Mango Mulberry
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 148

Challenge Description

Fruits Apple Banana Dragonfruit Mango Mulberry

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_148.xlsx", range = "A1:A12")
test  = read_excel("Power Query/PQ_Challenge_148.xlsx", range = "C1:N12") 

result = input %>%
  separate_rows(Fruits, sep = ", ") %>%
  mutate(Fruits = str_remove_all(Fruits, " ")) %>%
  group_by(Fruits) %>%
  summarise(Count = n()) %>%
  ungroup() %>%
  mutate(Fruits2 = Fruits) %>%
  pivot_wider(names_from = Fruits2, values_from = Count) 

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

    • Aggregates or ranks values at the relevant grouping level

    • 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

input_data = pd.read_excel("PQ_Challenge_148.xlsx", usecols="A", nrows=12)
test = pd.read_excel("PQ_Challenge_148.xlsx", usecols="C:N", nrows=12)

result = (
    input_data.assign(Fruits=input_data["Fruits"].str.split(","))
    .explode("Fruits")
)
result["Fruits"] = result["Fruits"].str.replace(" ", "", regex=False)
result = result.groupby("Fruits", as_index=False).size().rename(columns={"size": "Count"})
result["Fruits2"] = result["Fruits"]
result = result.pivot(index="Count", columns="Fruits2", values="Count").reset_index(drop=True)

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