Bollinger Bands analysis using Tableau

Download source files used in this article

Volatility analysis charts using Bollinger Bands are often used for trading decisions. Those who track stock price movements must be familiar with charts types shown below


Bollinger Bands are intervals drawn on price chart at standard deviation levels above and below the corresponding moving average.

Bollinger Bands consist of :

  • an N-period moving average (MA)
  • an upper band at K times an N-period standard deviation above the moving average (MA + Kσ)
  • a lower band at K times an N-period standard deviation below the moving average (MA − Kσ)

Let us see how can we develop this chart using Tableau.

In this example, I connect to a database table which has daily price information of a stock. Simple data source – dimension as Date and measure as price.

Step one is to create a line chart, as shown below


Next we need to add 4 calculated files

  • Moving Average
  • Standard Deviation
  • Upper Band
  • Lower Band


Note: The above formulas have used 20 as N-period (look back period) and 2 as K-times to multiply since these are  typical values used in real world. If we need to change these dynamically, we can consider using them as parameter fields.

Add ‘Measure Names‘ as filter to your sheet (select MA, LB and UB in the filter selection). Then drag and drop ‘Measure Values’ to Rows in the chart.


Choose ‘Dual Axis’ for Measure Values and synchronize the axis as next step.


We have the Bollinger Bands generated; however the color schemes are not intuitive. So lets modify that a little.

Here is the final chart


Download source files used in this article


Cluster Analysis using Tableau and R – Part-2

Related article : Cluster Analysis using Tableau and R – Part-1

We have performed clustering analysis from both Tableau and R in my previous post. Tableau uses k-means algorithm for cluster analysis which partitions the data into k clusters with a center or mean value of all the points in each. Clustering is based on the distance each measure lies from the center.

Let’s look into that in detail.

First generate a cluster scatter plot in Tableau as we did in part -1 using Iris data set.


Right click on the cluster that we added and choose Describe Clusters option


This provides you the clustering details given below


Now lets perform the k-means clustering from R and print the cluster

#copying iris to myiris variable
#Remove Species column
myiris$Species <- NULL
kmeans.result <- kmeans(myiris,3)

#print the cluster data


Check the cluster means against that of Tableau cluster centers. Aren’t these comparable? However Tableau clustering analysis is limited and the default one is k-means as compared to the number of packages and functions available in R to perform various types of clustering. I will dedicate a future article to cover cluster and fpc packages in R.

In this case, what options that a Tableau user has for extended/advanced clustering? The answer is R integration by calling R packages from Tableau using similar steps that I explained in this article.

How to integrate R with Tableau

We have seen how R can be integrated to your data science project using Power BI or Visual Studio(RTVS). Now its time to look at R integration with Tableau.

Before we get started with the steps, let us discuss how this is beneficial.

Tableau is a great visualization tool which helps you to understand your data, provide interactivity and assist in making business decisions. R integration is going to bring the capabilities of to your Tableau visualizations – such as statistical functions predictive analysis. The advantage of interactive visualizations in Tableau powered by the complex statistical analysis behind the scenes using R presents a strong case for data scientists to go for this integration.

I have added a high level representation below of this implementation. You can call R functions from Tableau and it passes the result back to Tableau which can be used to generate visualizations. You can utilize all packages (difficult to accomplish using Tableau scripting alone) that are running in R Server and generate visualizations using the resultant data (complex to accomplish using R alone).



As first step, make sure that RServe is running as a service that you can connect to. The screenshots below shows how to install RServe from RStudio.


Start the service


Now RServe is ready for connections. Go to Tableau and choose the option Help–> Settings and Performance–>Manage External Service Connection


In this case, my RServe is running on the same PC. So I selected localhost as server. Default RServe port is 6311. Leave that as is and test your connection as below


Above message confirms that you have established the connectivity with R service.

Next, we will look at an example calling R scripts from Tableau.

On-Premises Data Gateway for Power BI

We have seen different Power BI implementation options in my article here. If you do not want their application or database infrastructure to cloud will be looking for options for generating visualizations from Power BI service(cloud) connecting to on-premises data source (database such as SQL Server, Oracle or even structured files such as spreadsheets or csv files.

Note : Complete on-premises solution is available with the release of Power BI Report Server which is part of Power BI Premium. This article assumes the Power BI service implementation as below and gateway component is highlighted.

Power BI Gateway 1

On-premises data gateway allows Power BI service to connect to your database to refresh dataset. Gateway allows both scheduled refresh and live/direct query.  Time for looking at the steps on how to install and configure the gateway.

Download the standalone data gateway and start the service in on-premises. To download, use the option below after logon to your accountPower BI Gateway Installation_1

After installation, you have to sign in using your Azure account and specify a name to your gateway so that Power BI service tie it to your Power BI account.

Power BI Gateway Installation_3

Power BI Gateway Installation_4

Make sure that On-Premises Data Gateway is running as a windows service in your PC. Ideally this will be one of your on-premises server with connectivity to your database.

Go to Power BI web login, select ‘Manage Gateway’ option as shown below

Power BI Gateway Installation_5

Note that your Data Gateway is already listed there. You need to create a data source and provide authentication details for database connectivity (Windows User details in case of a file data source such as excel spreadsheet)

Power BI Gateway Installation_6

Next, go to your workspace, select the data set and go to ‘settings’ as shown below

Power BI Gateway Installation_7

Under ‘Gateway connection’ section, select your data gateway and click ‘apply’

Power BI Gateway Installation_8

Under ‘Scheduled refresh’ section, turn on ‘Keep your data up to date’ and specify refresh frequency as below

Power BI Gateway Installation_9

That completes it. Your dataset is ready for scheduled refresh connecting to your on-premises data source!

For verifying, you can update data in SQL Server database (local server) and wait for the refresh to happen.

Power BI Gateway Installation_11

Once complete, launch your visualization which uses the dataset and see your updated data reflecting there!

Power BI Gateway Installation_10

Data Analytics – The Power BI way

In my previous articles related to BI, we have explored extensively on Tableau and SAP Business Objects. While I continue the series, it would be unfair if I don’t cover another ’emerging’ solution – Microsoft Power BI (in fact it is a market leading BI solution already, refer to my post for 2017 BI trends here Gartner Magic Quadrant for BI and Analytics)

Those who are familiar with Tableau will find themselves home with Power BI in many ways. However Power BI is unique in terms of features, the publish options, pricing and visualization options such as custom visualizations.

Power BI was completely cloud based solution until recently – i.e, until the release of Power BI Report Server in this month (June 2017).  In the cloud based solution, you have to publish your visualizations to Power BI Service on cloud. You have alternative to Power BI desktop for development in the form of Power BI Service Web Interface which provides similar options for creating and customizing visualizations.

I have sketched a high level cloud based (Power BI Service) implementation architecture below  connecting to on-premises database to understand the different components better.

Power BI Architecture

Implementation architecture using Power BI Service on cloud connecting to on-premises database

The implementation architecture below shows the on-premises solution using Power BI Report Server. In this implementation, development and publishing do not require cloud. Note that the development require installation of custom release version of Power BI Desktop (Power BI Desktop optimized for Power BI Report Server)


PowerBI Architecture_OnPremise

On-Premises implementation architecture using Power BI Report Server

In terms of connectivity, Power BI can connect to almost every data source. However the type and location of data should decide your design and type of connectivity.

On-Premises Database connectivity from Power BI Service : If you have an on-premises database, Power BI service allows the connectivity using gateways. The options are (a) data imported to Power BI service or (b) use direct query or live connections. We will explore these implementation models in detail in the upcoming articles.


Custom Visuals : This is really great feature that Microsoft lets Power BI developers to create and publish their custom visualizations which can be added to your visualization tray and integrate with your reports/dashboards. Thanks for the support to creativity from developer community.

R Integration to Power BI :  Similar to Tableau R integration, we can connect to R for performing complex predictive analysis.


I will cover R integration details and many more in upcoming posts.

Data Analytics using Tableau – Trends & Trend Lines

In my previous blog, I had posted a bar chart showing declining trend in sales of a company over the years. If you notice, it had a trend line to aid visual analytics.


Another example given below shows profit trends by increase in sales for each category.


Trend lines are great visual tools for quick analysis. In the above diagram, its very easy to judge that the increase in sales for Supplies and Tables does not help increasing profit. The same judgement could not have been derived that easy from the clustered circles if the trend lines were not present. The steps to add Trend Lines are explained below.

I have created a scatter plot for Sales vs Profit using the Global Superstore database. Notice the “analytics” tab highlighted below.


Drag ‘Trend Line’ from the options to the visualization area and choose the line type. In this example, I have selected Linear model.



Additional options are available at (Right click -> Trend Lines-> Edit Trend Lines). For example, you can choose to show only one line (uncheck show trend line per color) and view for each category interactively for focused view/analysis as shown below



R Programming using RTVS – Part 4

This article assumes that you have RTVS extension added to your VS IDE.  You can refer my previous blogs on installation & configuration of RTVS.

  1. R Support in Visual Studio (RTVS)
  2. R Programming using RTVS – Part 2
  3. R Programming using RTVS – Part 3
1. Basic knowledge of R programming
2. RTVS Extension

Creating R projects using RTVS

Create a project in visual studio using R template.


Open the Script.R file from solution explorer and type in your R program. In the example below, we create a simple line graph that compares sales and profit performance of a company over a period of 5 years.

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)
#range for axis
rnge <- range(0, sales, profit)
#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")
#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])
#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)
#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

Place your cursor in the first line of code and use Ctrl+Enter to step through each line to R Interactive and execute. You can see each line execution results to the R Plot window as shown below


Here is the final plot view


Given below is an updated source which generate the output as PDF file

#Define Data
sales <- c(2000, 3500, 3000, 4700, 6000)
profit <- c(1450, 2600, 750, 2000, 3750)

#File name to save output
pdf(file = "sales_and_profit_byYr.pdf", height = 4, width = 5)

#range for axis
rnge <- range(0, sales, profit)

#Plot sales values
plot(sales, type = "o", col = "green", ylim = rnge, axes = FALSE, ann = FALSE)
#draw profit values as line
lines(profit, type = "o", pch = 22, lty = 2, col = "red")

#draw X axis and y axis
axis(1, at = 1:5, lab = c("2013", "2014", "2015", "2016", "2017"))
axis(2, las = 1, at = 1000 * 0:rnge[2])

#Graph Title
title(main = "Sales by Year", col.main = "blue", font.main = 4)

#add axis labels
title(xlab = "Year", col.lab = rgb(0, 0, 1))
title(ylab = "Amount", col.lab = rgb(0, 0, 1))

#Flush the output to PDF file