Excel BI - PowerQuery Challenge 313

excel-challenges
power-query
Calculate Total amount for every customer and also insert a Total Sale row for the store.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 313

Challenge Description

Calculate Total amount for every customer and also insert a Total Sale row for the store.

Solutions

library(tidyverse)
library(readxl)
library(janitor)

path = "Power Query/300-399/313/PQ_Challenge_313.xlsx"
input = read_excel(path, range = "A1:B18")
test  = read_excel(path, range = "D1:E5")

result = input %>%
  mutate(group = cumsum(Data1 == "Customer")) %>%
  pivot_wider(
    names_from = Data1,
    values_from = Data2,
    values_fill = NA
  ) %>%
  mutate(across(-c(group, Customer), as.numeric)) %>%
  mutate(`Total Amount` = Quantity * (Price -rowSums(across(starts_with("Disc")), na.rm = T) - rowSums(across(starts_with("Tax")), na.rm = T))) %>%
  select(Customer, `Total Amount`) %>%
  adorn_totals("row", name = "Total Sale")
  
all.equal(result, test, check.attributes = FALSE) 
# > 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
import numpy as np

path = "300-399/313/PQ_Challenge_313.xlsx"
input = pd.read_excel(path, usecols="A:B", nrows=18)
test = pd.read_excel(path, usecols="D:E", nrows=4)

pivoted = input.assign(group=(input['Data1'] == "Customer").cumsum()) \
    .pivot(index='group', columns='Data1', values='Data2') \
    .reset_index(drop=True)
pivoted.iloc[:, 1:] = pivoted.iloc[:, 1:].apply(pd.to_numeric, errors='coerce')

pivoted['Total Amount'] = (
    pivoted['Quantity'] * (
        pivoted['Price']
        - pivoted.filter(like='Disc').sum(axis=1, skipna=True)
        - pivoted.filter(like='Tax').sum(axis=1, skipna=True)
    )
).astype('int64')

result = pivoted[['Customer', 'Total Amount']]
result.loc[len(result)] = ['Total Sale', result['Total Amount'].sum()]

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