library(tidyverse)
library(readxl)
path = "Power Query/PQ_Challenge_219.xlsx"
input = read_excel(path, range = "A1:B7")
test = read_excel(path, range = "D1:F12")
devices = c("Laptop", "Desktop", "Mobile")
result = input %>%
separate_rows(Machine, sep = ", ") %>%
separate(Machine, into = c("Device", "OS"), sep = " - ",remove = FALSE) %>%
mutate(OS = case_when(
is.na(OS) & Device %in% devices ~ lead(OS,1),
is.na(OS) & !Device %in% devices ~ Device,
TRUE ~ OS),
Device = case_when(
!Device %in% devices ~ lag(Device,1),
TRUE ~ Device)) %>%
select(-Machine)
identical(result, test)
#> [1] TRUEExcel BI - PowerQuery Challenge 219
excel-challenges
power-query
Name Machine Device OS Roy Laptop - Windows10

Challenge Description
Name Machine Device OS Roy Laptop - Windows10
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_219.xlsx"
input = pd.read_excel(path, usecols="A:B", nrows=6)
test = pd.read_excel(path, usecols="D:F", nrows=12).rename(columns=lambda x: x.replace(".1", "")).apply(lambda x: x.str.strip() if x.dtype == "object" else x)
devices = ["Laptop", "Desktop", "Mobile"]
input = input.assign(Machine=input["Machine"].str.split(", ")).explode("Machine")
input[["Device", "OS"]] = input["Machine"].str.split(" - ", expand=True)
input["OS"] = np.where(input["OS"].isnull(), np.where(input["Device"].isin(devices), input["OS"].shift(-1), input["Device"]), input["OS"])
input["Device"] = np.where(input["Device"].isin(devices), input["Device"], input["Device"].shift(1))
input = input.drop("Machine", axis=1).reset_index(drop=True)
print(input.equals(test)) # TrueLogic:
Reads the workbook range needed for the challenge
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 easy to moderate:
- The transformation rule is readable, but the final layout still requires a careful implementation.