library(tidyverse)
library(readxl)
path = "Power Query/PQ_Challenge_221.xlsx"
input = read_excel(path, range = "A1:C20")
test = read_excel(path, range = "E1:J20")
result = input %>%
mutate(Project_Index = as.numeric(as.factor(Project))) %>%
mutate(Task_Index = as.numeric(paste0(Project_Index,".",as.numeric(as.factor(Task)))) , .by = Project) %>%
mutate(Activity_Index = paste0(Task_Index,".", as.numeric(as.factor(Activity))), .by = c(Project, Task))
all.equal(result, test)
# [1] TRUEExcel BI - PowerQuery Challenge 221
excel-challenges
power-query
Project Task Activity Project_Index Task_Index Activity_Index

Challenge Description
Project Task Activity Project_Index Task_Index Activity_Index
Solutions
Logic:
Reads the workbook range needed for the challenge
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 = "PQ_Challenge_221.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=20)
test = pd.read_excel(path, usecols="E:J", nrows=20).rename(columns=lambda x: x.replace('.1', ''))
test["Task_Index"] = test["Task_Index"].astype(str)
input['Project_Index'] = (input['Project'].astype('category').cat.codes + 1).astype(np.int64)
input['Task_Index'] = (input['Project_Index'].astype(str) + "." +
input.groupby('Project')['Task']
.transform(lambda x: (x.astype('category').cat.codes + 1).astype(str)))
input['Activity_Index'] = (input['Task_Index'] + "." +
input.groupby(['Project', 'Task'])['Activity']
.transform(lambda x: (x.astype('category').cat.codes + 1).astype(str)))
print(input.equals(test)) # TrueLogic:
Reads the workbook range needed for the challenge
Aggregates or ranks values at the relevant grouping level
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 easy to moderate:
- The transformation rule is readable, but the final layout still requires a careful implementation.