library(tidyverse)
library(readxl)
path = "Power Query/PQ_Challenge_255.xlsx"
input = read_excel(path, range = "A1:C14")
test = read_excel(path, range = "E1:J4")
result = input %>%
pivot_wider(names_from = "Task", values_from = "Date Time") %>%
mutate(across(`2`:`6`,
~if_else(!is.na(.),
round(as.numeric(difftime(., `1`, units = "hours")),2),
NA_real_),
.names = "{.col}-1")) %>%
select(-c(`1`:`6`)) %>%
rename(Task = Ticket)
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEExcel BI - PowerQuery Challenge 255
excel-challenges
power-query
Transpose the data given in problem table to result table.

Challenge Description
Transpose the data given in problem table to result table.
Solutions
Logic:
Reads the workbook range needed for the challenge
Reshapes the data into the structure required by the result table
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 = "PQ_Challenge_255.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=14)
test = pd.read_excel(path, usecols="E:J", nrows=3)
input = input.pivot(index='Ticket', columns='Task', values='Date Time')
for col in range(2, 7):
input[f'{col}-1'] = input.apply(
lambda row: round((row[col] - row[1]).total_seconds() / 3600, 2) if pd.notna(row[col]) else None, axis=1
)
input.drop(columns=range(1, 7), inplace=True)
input.reset_index(inplace=True)
input.rename(columns={'Ticket': 'Task.1'}, inplace=True)
print(input.equals(test)) # TrueLogic:
Reads the workbook range needed for the challenge
Reshapes the data into the structure required by the result table
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 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.