PowerQuery Puzzle solved with R
#151–152
Puzzles
Author: ExcelBI
All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.
Puzzle #151
Today we simplified log of people work. They just written down date and time of start and end. Not really helpful, but fortunatelly their manager gave us certain rules: there are no overtime or weekend work, and prepare timetable when exactly each day they were suppose to work.
Lets utilize hms
package and sequences to look for how many hours should be paid.
Loading libraries and data
library(tidyverse)
library(readxl)
library(hms)
test = read_excel("Power Query/PQ_Challenge_151.xlsx", range = "G1:H6") %>%
janitor::clean_names()
read_excel_range <- function(file, range) {
read_excel(file, range = range) %>%
mutate(across(c(starts_with("Start Time"), starts_with("End Time")), as_hms),
across(c(starts_with("Start Date"), starts_with("End Date")), as_date)) %>%
janitor::clean_names()
}
input1 <- read_excel_range("Power Query/PQ_Challenge_151.xlsx", "A1:E6")
input2 <- read_excel_range("Power Query/PQ_Challenge_151.xlsx", "A9:D14")
Transformation
result <- input1 %>%
mutate(
start = as_datetime(start_date) + start_time,
end = as_datetime(end_date) + end_time,
datetime = map2(start, end, seq, by = "hour")
) %>%
unnest(datetime) %>%
mutate(
weekday = wday(datetime, week_start = 1),
time = as_hms(datetime)
) %>%
left_join(input2, by = "weekday") %>%
filter(datetime >= start & datetime <= end,
time >= start_time.y & time < end_time.y) %>%
group_by(employee) %>%
summarise(total_hours = n() %>% as.numeric())
Validation
identical(result, test)
#> [1] TRUE
Puzzle #152
Another they, another HR issue. Now we have to calculate how many different types of leave and how many days certain workers take. Some conditional expressions and we will have it covered. Let’s go.
Loading libraries and data
library(tidyverse)
library(readxl)
input = read_excel("Power Query/PQ_Challenge_152.xlsx", range = "A1:D17") %>%
janitor::clean_names()
test = read_excel("Power Query/PQ_Challenge_152.xlsx", range = "F1:I5") %>%
janitor::clean_names()
Transformation
result = input %>%
mutate(seq = map2(from_date, to_date, seq, by = "day")) %>%
unnest_longer(seq) %>%
select(-c(from_date, to_date)) %>%
mutate(value = 1) %>%
pivot_wider(names_from = type_of_leave, values_from = value, values_fill = 0) %>%
select(name, seq, ML, PL, CL) %>%
mutate(sum = ML + PL + CL,
concat = paste0(ML, PL, CL) %>% as.numeric(),
main_leave = case_when(sum == 1 & ML == 1 ~ "ML",
sum == 1 & PL == 1 ~ "PL",
sum == 1 & CL == 1 ~ "CL",
sum == 2 & concat >= 100 ~ "ML",
sum == 2 & concat < 100 ~ "PL",
sum == 3 ~ "ML",
TRUE ~ "NA"),
wday = wday(seq, week_start = 1)) %>%
filter(!wday %in% c(6, 7)) %>%
select(name, seq, main_leave) %>%
mutate(main_leave = str_to_lower(main_leave)) %>%
group_by(name, main_leave) %>%
summarise(days = n() %>% as.numeric()) %>%
ungroup() %>%
pivot_wider(names_from = main_leave, values_from = days, values_fill = 0)
Validation
identical(result, test)
#> [1] TRUE
Feel free to comment, share and contact me with advices, questions and your ideas how to improve anything. Contact me on Linkedin if you wish as well.