Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Last active January 30, 2021 22:10
Show Gist options
  • Select an option

  • Save johnmackintosh/1f55dc1321cf1253268b79d3f163a385 to your computer and use it in GitHub Desktop.

Select an option

Save johnmackintosh/1f55dc1321cf1253268b79d3f163a385 to your computer and use it in GitHub Desktop.
library(lubridate)
library(dplyr)
library(tidyr)
library(readxl)
library(purrr)
library(here)
setwd(here("2021-03"))
wb <- "PD 2021 Wk 3 Input.xlsx"
all_sheets <- wb %>%
excel_sheets() %>%
set_names()
data <- map_dfr(all_sheets,
~ read_excel(wb, sheet = .x),
.id = "sheet") %>%
rename(Store = sheet)
store_data <- data %>%
gather('metric','Products_Sold', -c(Store, Date)) %>%
separate(.,metric, sep = ' - ', into = c('Customer_Type','Product')) %>%
mutate(Quarter = quarter(Date))
out1 <- store_data %>%
group_by(Product, Quarter) %>%
summarise(`Products Sold` = sum(Products_Sold), .groups = 'keep')
out2 <- store_data %>%
group_by(Store, Customer_Type, Product) %>%
summarise(`Products Sold` = sum(Products_Sold), .groups = 'keep')
write.csv(out1,'Product_Quarter_Output.csv')
write.csv(out2,'Store_Customer_Product_Output.csv')
@johnmackintosh

johnmackintosh commented Jan 30, 2021

Copy link
Copy Markdown
Author

image

power bi output 1 - sales by quarter and product

@johnmackintosh

johnmackintosh commented Jan 30, 2021

Copy link
Copy Markdown
Author

image
power bi output 2 - sales by store, product and customer type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment