Excel BI - PowerQuery Challenge 183

excel-challenges
power-query
Transpose the problem table into result table.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 183

Challenge Description

Transpose the problem table into result table.

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_183.xlsx", range = "A1:F5")
test  = read_excel("Power Query/PQ_Challenge_183.xlsx", range = "H1:K24") %>%
  mutate(Rental = as.integer(Rental))

result = input %>%
  unite("OYQ", Year, Quarter, sep = " ") %>%
  mutate(OYQ = yq(OYQ)) %>%
  rowwise() %>%
  mutate(quarters = list(seq.Date(from = as.Date(OYQ), by = "quarter", length.out = `Total Periods`))) %>%
  ungroup() %>%
  unnest(quarters) %>%
  mutate(Year = year(quarters), 
         Quarter = paste0("Q",quarter(quarters)),
         rn = row_number(),
         roll_year = (rn - 1) %/% 4 ,
         .by = Vendor) %>%
  mutate(Rental = round(Rental * (1 + `% Hike Yearly`/100)^roll_year) %>% as.integer()) %>%
  select(Vendor, Year, Quarter, Rental) 

identical(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
from datetime import datetime
import numpy as np

input = pd.read_excel("PQ_Challenge_183.xlsx",  nrows=4, usecols="A:F")
test = pd.read_excel("PQ_Challenge_183.xlsx",  nrows=24, usecols="H:K")
test.columns = ["Vendor","Year", "Quarter", "Rental"]
test["Rental"] = test["Rental"].astype(int)

result = input.copy()
result["QuarterFM"] = np.where(result["Quarter"] == "Q1", "01", np.where(result["Quarter"] == "Q2", "04", np.where(result["Quarter"] == "Q3", "07", "10")))
result["Date"] = pd.to_datetime(result["Year"].astype(str) + result["QuarterFM"], format="%Y%m")
result = result.loc[result.index.repeat(result["Total Periods"])].reset_index(drop=True)
result["Row"] = result.groupby("Vendor").cumcount().astype(int)
result["roll_year"] = result["Row"] // 4
result = result[["Vendor", "Rental", "% Hike Yearly", "Row", "roll_year", "Date"]]
result["Date"] = result["Date"] + pd.to_timedelta(result["Row"]*3*31, unit='D')
result["Date"] = result["Date"] + pd.offsets.MonthEnd(1)
result["Year"] = result["Date"].dt.year.astype("int64")
result["Quarter"] = "Q" + result["Date"].dt.quarter.astype(str)
result["Rental"] = result["Rental"] * (1 + result["% Hike Yearly"]/100) ** result["roll_year"]
result["Rental"] = result["Rental"].round(0).astype(int)
result = result[["Vendor", "Year", "Quarter", "Rental"]]
print(result.equals(test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

  • 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 easy to moderate:

  • The transformation rule is readable, but the final layout still requires a careful implementation.