Excel BI - PowerQuery Challenge 212

excel-challenges
power-query
T2 has got the sales persons involved in a deal and Sales amount. Commission of sales persons involved should be calculated on the basis of commission percentages listed in T1.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 212

Challenge Description

T2 has got the sales persons involved in a deal and Sales amount. Commission of sales persons involved should be calculated on the basis of commission percentages listed in T1.

Solutions

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_212.xlsx"
T1 = read_excel(path, range = "A2:C7")
T2 = read_excel(path, range = "A11:E17")
test = read_excel(path, range = "H2:I7")

input = T2 %>%
  pivot_longer(cols = -c(1, 2), values_to = "Code") %>%
  left_join(T1, by = "Code") %>%
  na.omit() %>%
  mutate(Amount = Sales * Commission) %>%
  summarise(Amount = sum(Amount), .by = "Name") %>%
  arrange(desc(Amount)) 

identical(input, 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 = "PQ_Challenge_212.xlsx"
T1 = pd.read_excel(path, skiprows = 1, nrows = 5, usecols="A:C")
T2 = pd.read_excel(path, skiprows = 10, nrows = 6, usecols="A:E")
test = pd.read_excel(path, skiprows = 1, nrows = 5, usecols="H:I")
test.columns = test.columns.str.replace(".1", "")

result = T2.copy()
result = pd.melt(result, id_vars=["Deal", "Sales"], value_vars=["Code1", "Code2", "Code3"], var_name="Code", value_name="Value")\
            .dropna()\
            .merge(T1, left_on="Value", right_on="Code", how="left")\
            .assign(Amount = lambda x: x["Sales"] * x["Commission"])\
            .groupby("Name")["Amount"].sum()\
            .sort_values(ascending=False)\
            .reset_index(drop=False)

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