forked from datasciencedojo/meetup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#======================================================================================= | ||
# | ||
# File: CustomerQuery.R | ||
# Author: Dave Langer | ||
# Description: This code illustrates querying a SQL Server database via the RODBC | ||
# package for the "Introduction to R Visualization with Power BI " Meetup | ||
# dated 03/15/2017. More details on the Meetup are available at: | ||
# | ||
# https://www.meetup.com/Data-Science-Dojo-Toronto/events/237952698/ | ||
# | ||
# The code in this file leverages data from Microsoft's Wide World | ||
# Importers sample database available at: | ||
# | ||
# https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0 | ||
# | ||
# NOTE - This file is provided "As-Is" and no warranty regardings its contents are | ||
# offered nor implied. USE AT YOUR OWN RISK! | ||
# | ||
#======================================================================================= | ||
|
||
|
||
# Uncomment and run these lines of code to install required packages | ||
#install.packages("RODBC") | ||
|
||
library(RODBC) | ||
|
||
# Open connection using Windows ODBC DSN | ||
dbhandle <- odbcConnect("RConnection") | ||
|
||
# Query database for a denormalized view of [Fact][Sale] data | ||
dataset <- sqlQuery(dbhandle, | ||
"SELECT [C].[CustomerID] | ||
,[C].[CustomerName] | ||
,[C].[BuyingGroupID] | ||
,[C].[DeliveryMethodID] | ||
,[C].[DeliveryCityID] | ||
,[C].[DeliveryAddressLine1] | ||
,[C].[DeliveryAddressLine2] | ||
,[CITY].[CityName] | ||
,[P].[StateProvinceCode] | ||
,[C].[DeliveryPostalCode] | ||
,[CC].[CustomerCategoryName] | ||
,[BG].[BuyingGroupName] | ||
,[O].[OrderID] | ||
,[O].[OrderDate] | ||
,[OL].[OrderLineID] | ||
,[OL].[Quantity] | ||
,[OL].[UnitPrice] | ||
,[OL].[Quantity] * [OL].[UnitPrice] AS [LineTotal] | ||
,[SC].[SupplierCategoryName] | ||
FROM [WideWorldImporters].[Sales].[Customers] C | ||
INNER JOIN [WideWorldImporters].[Sales].[CustomerCategories] CC ON ([C].[CustomerCategoryID] = [CC].[CustomerCategoryID]) | ||
LEFT OUTER JOIN [WideWorldImporters].[Sales].[BuyingGroups] BG ON ([C].[BuyingGroupID] = [BG].[BuyingGroupID]) | ||
INNER JOIN [WideWorldImporters].[Sales].[Orders] O ON ([C].[CustomerID] = [O].[CustomerID]) | ||
INNER JOIN [WideWorldImporters].[Sales].[OrderLines] OL ON ([O].[OrderID] = [OL].[OrderID]) | ||
INNER JOIN [WideWorldImporters].[Warehouse].[StockItems] SI ON ([OL].[StockItemID] = [SI].[StockItemID]) | ||
INNER JOIN [WideWorldImporters].[Purchasing].[Suppliers] S ON ([SI].[SupplierID] = [S].[SupplierID]) | ||
INNER JOIN [WideWorldImporters].[Purchasing].[SupplierCategories] SC ON ([S].[SupplierCategoryID] = [SC].[SupplierCategoryID]) | ||
INNER JOIN [WideWorldImporters].[Application].[Cities] CITY ON ([C].[DeliveryCityID] = [CITY].[CityID]) | ||
INNER JOIN [WideWorldImporters].[Application].[StateProvinces] P ON ([CITY].[StateProvinceID] = [P].[StateProvinceID])", | ||
stringsAsFactors = FALSE) | ||
|
||
#Close DB connection | ||
odbcClose(dbhandle) | ||
|
||
|
||
# Save off data frame in .RData binary format | ||
save(dataset, file = "CustomerData.RData") | ||
|
||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#======================================================================================= | ||
# | ||
# File: CustomerVisualizations.R | ||
# Author: Dave Langer | ||
# Description: This code illustrates R visualizaions used in the "Introduction to R | ||
# Visualization with Power BI " Meetup dated 03/15/2017. More details on | ||
# the Meetup are available at: | ||
# | ||
# https://www.meetup.com/Data-Science-Dojo-Toronto/events/237952698/ | ||
# | ||
# The code in this file leverages data from Microsoft's Wide World | ||
# Importers sample Data Warehouse available at: | ||
# | ||
# https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0 | ||
# | ||
# NOTE - This file is provided "As-Is" and no warranty regardings its contents are | ||
# offered nor implied. USE AT YOUR OWN RISK! | ||
# | ||
#======================================================================================= | ||
|
||
|
||
# Uncomment and run these lines of code to install required packages | ||
#install.packages("dplyr") | ||
#install.packages("lubridate") | ||
#install.packages("ggplot2") | ||
#install.packages("scales") | ||
#install.packages("qcc") | ||
|
||
|
||
# NOTE - Change your working directory as needed | ||
load("CustomerData.RData") | ||
|
||
|
||
# Preprocessing to make dataset look like Power BI | ||
library(dplyr) | ||
library(lubridate) | ||
dataset <- dataset %>% | ||
mutate(BuyingGroupName = ifelse(is.na(BuyingGroupName), "", BuyingGroupName), | ||
Year = year(dataset$OrderDate), | ||
Month = month(dataset$OrderDate, label = TRUE)) | ||
|
||
|
||
#============================================================================= | ||
# | ||
# Visualization #1 - Aggregaed dynamic bar charts by Customer Category | ||
# | ||
#============================================================================= | ||
|
||
library(dplyr) | ||
library(ggplot2) | ||
library(scales) | ||
|
||
|
||
# Get total revenue by Buying Group and Customer Catetory | ||
customer.categories <- dataset %>% | ||
group_by(BuyingGroupName, CustomerCategoryName) %>% | ||
summarize(TotalRevenue = sum(LineTotal)) | ||
|
||
|
||
# Format visualization title string dynamically | ||
title.str.1 <- paste("Total Revenue for", | ||
dataset$Year[1], | ||
"by Customer Category and Buying Group", | ||
sep = " ") | ||
|
||
|
||
# Plot | ||
ggplot(customer.categories, aes(x = reorder(CustomerCategoryName, TotalRevenue), y = TotalRevenue, fill = BuyingGroupName)) + | ||
theme_bw() + | ||
coord_flip() + | ||
geom_bar(stat = "identity") + | ||
scale_y_continuous(labels = comma) + | ||
labs(x = "Customer Category", | ||
y = "Total Revenue", | ||
fill = "Buying Group", | ||
title = title.str.1) | ||
|
||
|
||
|
||
|
||
#============================================================================= | ||
# | ||
# Visualization #2 - Aggregaed dynamic bar charts by Customer Category | ||
# | ||
#============================================================================= | ||
|
||
library(dplyr) | ||
library(qcc) | ||
|
||
Year1 <- min(dataset$Year) | ||
Year2 <- max(dataset$Year) | ||
|
||
totals <- dataset %>% | ||
filter(Year == Year1| Year == Year2 ) %>% | ||
group_by(Year, Month) %>% | ||
summarize(TotalRevenue = sum(LineTotal)) %>% | ||
mutate(Label = paste(Month, Year, sep = "-")) | ||
|
||
# Make labels pretty with dummy vars | ||
Revenue.Group.1 <- totals$TotalRevenue[1:12] | ||
Revenue.Group.2 <- totals$TotalRevenue[13:24] | ||
|
||
title.str <- paste("Process Behavior Chart - ", Year1, " and ", Year2, " ", | ||
dataset$CustomerCategoryName[1], " Total Revnue for Buying Group '", | ||
dataset$BuyingGroupName[1], "'", sep = "") | ||
|
||
blank.super.qcc <- qcc(Revenue.Group.1, type = "xbar.one", | ||
newdata = Revenue.Group.2, | ||
labels = totals$Label[1:12], | ||
newlabels = totals$Label[13:24], | ||
title = title.str, | ||
ylab = "Total Revenue", xlab = "Month-Year") | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Introduction to R Visualizations in Microsoft Power BI | ||
|
||
GitHub Repository for the 03/15/2017 Meetup titled "[Introduction to R Visualizations in Microsoft Power BI](https://www.meetup.com/Data-Science-Dojo-Toronto/events/237952698/)". | ||
|
||
These materials make extensive use of Microsoft's [Wide World Importers](https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0) SQL Server 2016 sample database. | ||
|
||
Additionally, the following are required to use the files for the Meetup: | ||
|
||
* [Power BI Desktop](https://www.microsoft.com/en-us/download/details.aspx?id=45331) | ||
* [The R programming language](https://cran.rstudio.com/) | ||
* The [dplyr](https://cran.r-project.org/web/packages/dplyr/index.html), [lubridate](https://cran.r-project.org/web/packages/lubridate/index.html), [ggplot2](https://cran.r-project.org/web/packages/ggplot2/index.html), [scales](https://cran.r-project.org/web/packages/scales/index.html), and [qcc](https://cran.r-project.org/web/packages/qcc/index.html) packages. | ||
|
||
While not required, [RStudio](https://www.rstudio.com/products/rstudio/download/) is highly recommended. | ||
|