Excel BI - PowerQuery Challenge 197

excel-challenges
power-query
Month Item Store Stock IN Stock OUT Start Stock
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 197

Challenge Description

Month Item Store Stock IN Stock OUT Start Stock

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_197.xlsx"

input = read_xlsx(path, range = "A1:E21")
test  = read_xlsx(path, range = "H1:N21")

result <- input %>%
  group_by(Item, Store) %>%
  mutate(data = accumulate(`Stock IN` - `Stock OUT`, `+`),
         `Start Stock` = lag(data, default = first(`Stock IN`)),
         `End Stock` = data) %>%
  ungroup() %>%
  select(-data) 

identical(result, test)
#> [1] TRUE
  • Logic:

    • 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 = "PQ_Challenge_197.xlsx"

input = pd.read_excel(path, usecols="A:E")
test  = pd.read_excel(path, usecols="H:N")
test.columns = test.columns.str.replace(".1", "")

input["Month"] = pd.to_datetime(input["Month"], format="%b").dt.month
input = input.sort_values(["Store", "Item", "Month"]).reset_index(drop=True)
input["Row"] = input.groupby(["Store", "Item"]).cumcount()+1

for i in range(len(input)):
    if input.loc[i, "Row"] == 1:
        input.loc[i, "Start Stock"] = input.loc[i, "Stock IN"]
        input.loc[i, "End Stock"] = input.loc[i, "Stock IN"] - input.loc[i, "Stock OUT"]
    else:
        input.loc[i, "Start Stock"] = input.loc[i-1, "End Stock"]
        input.loc[i, "End Stock"] = input.loc[i, "Start Stock"] - input.loc[i, "Stock OUT"] + input.loc[i, "Stock IN"]

input["Month"] = pd.to_datetime(input["Month"], format="%m").dt.strftime("%b")
input["Start Stock"] = input["Start Stock"].astype("int64")
input["End Stock"] = input["End Stock"].astype("int64")

result = test.merge(input, on=["Store", "Item", "Month", "Stock IN"], how="left", suffixes=("_test", ""))
result = result[["Month","Item","Store",   "Stock IN", "Stock OUT", "Start Stock", "End Stock"]]

print(result.equals(test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

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