Excel BI - PowerQuery Challenge 362

excel-challenges
power-query
Create the result table showing Played matches, Won matches, Drawn matches, Lost matches, GF (Goals For), GA (Goals Against) and Points.
Published

March 24, 2026

Illustration for Excel BI - PowerQuery Challenge 362

Challenge Description

Create the result table showing Played matches, Won matches, Drawn matches, Lost matches, GF (Goals For), GA (Goals Against) and Points.

Solutions

library(tidyverse)
library(readxl)

path <- "Power Query/300-399/362/PQ_Challenge_362.xlsx"
input <- read_excel(path, range = "A1:C51")
test <- read_excel(path, range = "E1:L7")

result = input %>%
  rowwise() %>%
  mutate(
    teams = list(str_split(Teams, "-"))[[1]],
    scores = list(str_split(Score, "-"))[[1]]
  ) %>%
  ungroup() %>%
  unnest_wider(c(teams, scores), names_sep = "_") %>%
  select(-c(1:3)) %>%
  pivot_longer(
    cols = c(starts_with("teams")),
    names_to = "venue",
    values_to = "Team"
  ) %>%
  mutate(
    GF = ifelse(venue == "teams_1", as.numeric(scores_1), as.numeric(scores_2)),
    GA = ifelse(venue == "teams_1", as.numeric(scores_2), as.numeric(scores_1)),
    Result = case_when(GF > GA ~ "W", GF < GA ~ "L", TRUE ~ "D")
  ) %>%
  summarise(
    Played = n(),
    Won = sum(Result == "W"),
    Drawn = sum(Result == "D"),
    Lost = sum(Result == "L"),
    GF = sum(GF),
    GA = sum(GA),
    Points = Won * 3 + Drawn * 1,
    .by = Team
  ) %>%
  arrange(desc(Points), desc(GF - GA), desc(GF)) %>%
  mutate(across(where(is.integer), as.numeric))

all.equal(result, test)
#> [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data into the structure required by the result table

    • 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

path = "Power Query\\300-399\\362\\PQ_Challenge_362.xlsx"
input = pd.read_excel(path, usecols="B:C", nrows=50) 
test = pd.read_excel(path, usecols = "E:L", nrows=6)

def standings(df):
    h = df['Teams'].str.split('-', expand=True)
    s = df['Score'].str.split('-', expand=True).astype(int)
    
    data = pd.concat([
        pd.DataFrame({'Team': h[0], 'GF': s[0], 'GA': s[1]}),
        pd.DataFrame({'Team': h[1], 'GF': s[1], 'GA': s[0]})
    ])
    
    data['Result'] = data['GF'].gt(data['GA']).map({True: 'W', False: 'L'}).where(data['GF'] != data['GA'], 'D')
    
    return (data.groupby('Team')
               .agg(Played=('Result', 'size'),
                    Won=('Result', lambda x: x.eq('W').sum()),
                    Drawn=('Result', lambda x: x.eq('D').sum()),
                    Lost=('Result', lambda x: x.eq('L').sum()),
                    GF=('GF', 'sum'),
                    GA=('GA', 'sum'))
               .assign(Points=lambda x: x['Won']*3 + x['Drawn'])
               .sort_values(['Points', 'GF', 'GA'], ascending=[False, False, True])
               .reset_index())

result = standings(input)

print(result.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

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