library(tidyverse)
library(readxl)
library(gt)
path = "Power Query/PQ_Challenge_215.xlsx"
input = read_excel(path, range = "A1:E20")
test = read_excel(path, range = "G1:J15")
result = input %>%
mutate(out_day = case_when(
!is.na(`Paid Date`) ~ NA_real_,
`Due Date` > today() ~ 0,
TRUE ~ as.numeric(difftime(today(), `Due Date`, units = "days"))
)) %>%
filter(!is.na(out_day)) %>%
arrange(`Branch ID`, Customer, `Due Date`) %>%
select(-`Paid Date`) %>%
group_by(`Branch ID`) %>%
gt() %>%
# change column names
cols_label(Customer = "Branch ID / Customer",
`Due Date` = "Due Date",
`Loan Amt` = "Total Loan Amount",
out_day = "Total Outstanding Days")
resultExcel BI - PowerQuery Challenge 215

Challenge Description
Assuming today’s date is 7-Sep-24, pivot the table as shown for Outstanding loans. If Paid Date is not blank, it means that loan is not outstanding. For outstanding loans, outstanding days = Today’s date (7-Sep-24) - Due Date. If Due Date > Today’s Date then outstanding days is 0.
Solutions
Logic:
Reads the workbook range needed for the challenge
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
from datetime import datetime
path = "PQ_Challenge_215.xlsx"
input = pd.read_excel(path, usecols="A:E")
test = pd.read_excel(path, usecols="G:J", nrows=14)
today = pd.Timestamp(datetime.today().date())
result = input.assign(out_day=pd.Series(dtype=float))
result['out_day'] = result['Paid Date'].apply(lambda x: None if pd.isnull(x) else 0)
result['out_day'] = result['out_day'].combine_first(result['Due Date'].apply(lambda x: 0 if x > today else (today.date() - x.date()).days))
result = result[result['Paid Date'].isnull()]
result = result.sort_values(by=['Branch ID', 'Customer', 'Due Date']).reset_index(drop=True)
result = result.drop(columns=['Paid Date']).reset_index(drop=True)
result = result.set_index(['Branch ID', 'Customer'])
print(result)Logic:
Reads the workbook range needed for the challenge
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.