library(tidyverse)
library(readxl)
input = read_excel("Power Query/PQ_Challenge_185.xlsx", range = "A1:B13")
test = read_excel("Power Query/PQ_Challenge_185.xlsx", range = "D1:F13")
result = input %>%
mutate(Index = map_dbl(Emp, ~ which(unique(Emp) == .x)[1]), .by = Group)
all.equal(result, test)
# [1] TRUE
result2 = input %>%
mutate(Index = dense_rank(factor(Emp, levels = unique(Emp))), .by = Group)
all.equal(result2, test)
# [1] TRUE
result3 = input %>%
mutate(Index = as.integer(factor(Emp, levels = unique(Emp))), .by = Group)
all.equal(result3, test)
# [1] TRUEExcel BI - PowerQuery Challenge 185
excel-challenges
power-query
For each group, generate the index. For same employee in a group, Index will remain same.

Challenge Description
For each group, generate the index. For same employee in a group, Index will remain same.
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
input = pd.read_excel("PQ_Challenge_185.xlsx", sheet_name="Sheet1", usecols="A:B")
test = pd.read_excel("PQ_Challenge_185.xlsx", sheet_name="Sheet1", usecols="D:F")
test.columns = test.columns.str.replace('.1', '')
input['Index'] = input.groupby('Group ')['Emp'].transform(lambda x: pd.factorize(x)[0]+1)
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.