Select Page

### Introduction

In this blog post, I will be featuring a very cool and useful function in Tableau: Dynamically shifting lines using a parameter.

With this feature a user can shift a line chart across the x-axis (for time) to account for time lags and investigate if there was a stronger correlation.

My visualisation can be found here.

I will go through a couple of use cases for this feature and then I will showcase the functions/formulas used to create the moving line AND the changing correlation value pictured above. If you’re a beginner looking to recreate this visualisation, there will be another blog post detailing the steps in the future.

### USE CASE

There are a couple of scenarios in which we may need to factor in time lag when comparing the correlation between two lines. Some examples are provided below

###### Example scenario 1

You have a website with a webpage marketing a tourist destination. You want to see if the number of attendees correlate to the number of sessions recorded on the website correlate.
It is unlikely that individuals will be at the tourist destination and looking at the webpage at the same time and more likely they will be on the webpage to research the tourist destination before heading there. Hence we have time lag to consider.

###### Example scenario 2

A global retail company ships their products to different countries. Shipping products may take more time to get to Country A than to Country B. We can probably expect the sales for Country A to occur earlier than in Country B. By shifting the sales lines for Country A or Country B by the time lag we can account for that time lag and see how strongly correlated the countries are.

### THE MOVING LINE

For the moving line to work in Tableau, you’ll at least need to establish a relationship between the two datasets. The link will most likely be the datetime field in both datasets.

To keep it simple, first we create a parameter using a range of integers. The range can be whatever range you want the user to sift through.

In my example, I used a range of -4 to 4 as my x-axis was in quarters

For the moving line I used the LOOKUP table function and the following syntax:

LOOKUP( [expression], – [parameter] )

In my visualisation it ended up being something like this:

We then replace the old field with the new calculated…

and now we can move it using the parameter!

### CORRELATION VALUE

Tableau has a neat function that calculates the correlation based on what is in the view.

After adding both measures into the view (and dual axis -ing them) we can then use the following window calculation

WINDOW_CORR( [expression 1] , [expression 2])

In my visualisation, the calculated field looked like this

All you need to do is add the newly made calculated field to wherever you need it to be.

For example, you can add it into the Detail box on the Marks pane and add it into your title.

### CONCLUSION

In this blog post we went through a couple of use cases for implementing a line shift function. We outlined the table function that enabled us to do so using parameters. And I also showed how we could use the window correlation function which updates the value based on what changes we make in the parameter.

Thank you for taking the time to read this blog post. If you would like to reach out to me you may certainly do so at my LinkedIn profile.

My other Tableau works can be viewed in my Tableau Public profile.