library(tidyverse)
library(readxl)
path = "Power Query/PQ Challenge_194.xlsx"
input = read_xlsx(path, range = "A1:D10")
test = read_xlsx(path, range = "F1:I10")
result = input %>%
pivot_longer(cols = -c(1), names_to = "Amt", values_to = "Value") %>%
mutate(val = lag(Value, default = 0),
diff = Value - val) %>%
select(-c(Value, val)) %>%
pivot_wider(names_from = Amt, values_from = diff)
identical(result, test)
# [1] TRUEExcel BI - PowerQuery Challenge 194
excel-challenges
power-query
Date Amt1 Amt2 Amt3 RESULT PROBLEM

Challenge Description
Date Amt1 Amt2 Amt3 RESULT PROBLEM
Solutions
Logic:
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
path = 'PQ Challenge_194.xlsx'
input = pd.read_excel(path, usecols = "A:D")
test = pd.read_excel(path, usecols = "F:I")
test.columns = input.columns
result = input.melt(id_vars=['Date'], var_name='Amt', value_name='Value') \
.sort_values(['Date', 'Amt']).reset_index(drop = True) \
.assign(val=lambda x: x['Value'].shift(fill_value=0),
diff=lambda x: x['Value'] - x['val']) \
.drop(columns=['Value', 'val']) \
.pivot(index='Date', columns='Amt', values='diff').reset_index(drop=False)
print(result.equals(test)) # TrueLogic:
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.