Excel BI - PowerQuery Challenge 177

excel-challenges
power-query
Name Classs Subject Marks1 Marks2 Marks3
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 177

Challenge Description

Name Classs Subject Marks1 Marks2 Marks3

Solutions

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_177.xlsx", range = "A1:F10")
test  = read_excel("Power Query/PQ_Challenge_177.xlsx", range = "H1:M10")

result = input %>%
  rowwise() %>%
  mutate(Result = if_else(any(c(Marks1, Marks2, Marks3) < 40), "Fail", "Pass"),
         total = Marks1 + Marks2 + Marks3) %>%
  ungroup() %>%
  mutate(average = mean(total), 
         rn = row_number(),
         .by = Name) 

aux_rank = result %>%
  select(Name, average) %>%
  distinct() %>%
  mutate(Rank = rank(-average))

result2 = result %>%
  left_join(aux_rank, by = "Name") %>%
  select(Name, Classs, Subject, `Total Marks` = total, Result, Rank, rn) %>%
  mutate(Name = ifelse(rn == 1, Name, NA_character_),
         Classs = ifelse(rn == 1, Classs, NA_real_),
         Rank = ifelse(rn == 1, Rank, NA_integer_)) %>%
  select(-rn)

identical(result2, test) 
#> [1] TRUE
  • 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_177.xlsx", usecols = "A:F", nrows = 10)
test = pd.read_excel("PQ_Challenge_177.xlsx", usecols = "H:M", nrows = 10)
test.columns = test.columns.str.replace('.1', '')
# replace all Nas with empty string for comparison purposes
test = test.fillna('')

result = input.assign(Result=input.apply(lambda row: "Fail" if any(row[['Marks1', 'Marks2', 'Marks3']] < 40) else "Pass", axis=1),
                      total=input[['Marks1', 'Marks2', 'Marks3']].sum(axis=1)) \
              .assign(average=lambda df: df.groupby('Name')['total'].transform('mean'),
                      rn=lambda df: df.groupby('Name').cumcount() + 1)
aux_rank = result[['Name', 'average']].drop_duplicates() \
                                      .assign(Rank=lambda df: df['average'].rank(ascending=False))
result2 = result.merge(aux_rank, on=['Name', 'average'], how='left') \
                .drop(columns=['Marks1', 'Marks2', 'Marks3', 'average']) \
                .rename(columns={'total': 'Total Marks'})
result2[['Rank', 'Name', 'Classs']] = result2[['Rank', 'Name', 'Classs']].where(result2['rn'] == 1, "")
result2 = result2.drop(columns='rn')
result2 = result2[['Name', 'Classs', 'Subject', 'Total Marks', 'Result', 'Rank']]

print(result2.equals(test)) # True
  • 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

    • Applies the rule iteratively until the output is complete

  • 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.