Excel BI - PowerQuery Challenge 367

excel-challenges
power-query
Flight_Code Route Departure Aircraft Load_Factor Fuel_Price
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 367

Challenge Description

Flight_Code Route Departure Aircraft Load_Factor Fuel_Price

Solutions

library(tidyverse)
library(readxl)

path <- "Power Query/300-399/367/PQ_Challenge_367.xlsx"
input1 <- read_excel(path, range = "A1:F20")
input2 <- read_excel(path, range = "H1:T13")
test <- read_excel(path, range = "H17:I21")

r1 = input1 %>%
  separate_wider_delim(
    Aircraft,
    ":",
    names = c("Aircraft", "No.Passengers")
  ) %>%
  mutate(
    Route = str_replace(Route, "-(\\w+)-", "-\\1:\\1-"),
    `No.Passengers` = as.numeric(No.Passengers),
    Operator = str_sub(Flight_Code, 1, 2)
  ) %>%
  separate_longer_delim(Route, ":") %>%
  separate_wider_delim(Route, "-", names = c("From", "To"))

r2 = input2 %>%
  pivot_longer(cols = -1, names_to = "To", values_to = "Distance")

result = r1 %>%
  left_join(r2, by = c("To" = "To", "From" = "Airport")) %>%
  mutate(Cost = `No.Passengers` * Load_Factor * Fuel_Price * Distance) %>%
  summarise(`Total Cost` = round(sum(Cost, na.rm = T), 0), .by = "Operator")

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

path = "Power Query/300-399/367/PQ_Challenge_367.xlsx"
input1 = pd.read_excel(path, usecols="A:F", nrows=20)
input2 = pd.read_excel(path, usecols="H:T", nrows=13)
test = pd.read_excel(path, usecols="H:I", nrows=4, skiprows=16)

r1 = (
    input1
    .assign(
        **{"No.Passengers": lambda df: pd.to_numeric(df["Aircraft"].str.extract(r":(\d+)$", expand=False))},
        Route=lambda df: df["Route"].str.replace(r"-(\w+)-", r"-\1:\1-", regex=True),
        Operator=lambda df: df["Flight_Code"].str[:2]
    )
    .assign(Route=lambda df: df["Route"].str.split(":"))
    .explode("Route")
    .assign(
        From=lambda df: df["Route"].str.split("-").str[0],
        To=lambda df: df["Route"].str.split("-").str[1]
    )
    .drop(columns="Route")
)

r2 = (
    input2
    .melt(id_vars=input2.columns[0], var_name="To", value_name="Distance")
    .rename(columns={input2.columns[0]: "Airport"})
)

result = (
    r1
    .merge(r2, left_on=["From", "To"], right_on=["Airport", "To"], how="left")
    .assign(Cost=lambda df: df["No.Passengers"] * df["Load_Factor"] * df["Fuel_Price"] * df["Distance"])
    .groupby("Operator", as_index=False)
    .agg(Total_Cost=("Cost", lambda s: round(s.sum(skipna=True), 0)))
    .rename(columns={"Total_Cost": "Total Cost"})
)

print((result == test).all().all())
# Output: 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.