Tinkering with Data Refresh & Auto-Refresh

This week I was tinkering with ways of auto-refreshing data on an unattended computer and discovered how different Desktop and Server are in regard to refreshing data. Let’s start with user invoked refreshes, before getting into auto-refreshes.

Desktop Data Refresh

There are several ways to refresh your data in Desktop. You can right-click the data connection and click Refresh, or use the menus: Data\(name of connection)\Refresh. Or the easiest way is a F5 keystroke, which is what most of us probably use.

If you are using a data Extract then the refresh is a bit different, as F5 only refreshes the view, not the Extract. To refresh the Extract right-click the data connection, but instead of clicking refresh, go down and click Extract and then click the Refresh option in the flyout menu. Use the same process if you like using the menus. There is no key-combination (that I know) to invoke an Extract refresh.

Server Data Refresh

In Server things are different. F5 (or ctrl-F5 or shift-F5) does NOT refresh your data. It only refreshes your browser window. This does not issue a new query to your database, or refresh the data after a data extract has been refreshed; at least not by itself.

[Note: if after you first load the view into your browser, you edit the URL by adding  ?:refresh=yes then a F5 keystroke will refresh both your browser view and the data connection at the same time. (Make sure to delete the #1 at the end of your URL before adding the refresh parameter or it won’t work.)]

The most common way to refresh your data in Server is to click the refresh icon at the top of the view:

Refresh Data

Again, remember clicking your browser reload icon will NOT refresh your data (unless you edited your URL). Only clicking the pictured icon actually refreshes your data.

So these are the ways a user can manually refresh data in Desktop and Server. Now let’s move onto auto-refreshing data.

Auto-Refresh Data

Just to make sure there isn’t any confusion, when I say ‘Auto Refresh’ I mean refreshing the data and/or view without a user being involved. Even with a live connection, the data and/or view has to be refreshed, or the view on screen will remain unchanged. This is true for both desktop and in Server. Tableau is not natively a live streaming software.

So since Tableau isn’t setup to auto-refresh we will need to write some code or script to do it ourselves. The good news is below you will find examples of code you can modify, so you don’t have to start from scratch.

First, I want to give a shout out to Hugh Nguyen and Bill Lyons who each wrote some of the code below. Here’s our forum discussion of Auto Refreshing.

Desktop Auto-Refresh

If you are not publishing to Server, and instead you are opening a viz in Desktop presentation mode and simply want to refresh the view/data every five minutes then here’s the easiest/simplest script I found.

#NoEnv ; Recommended for  future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts 
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

#Persistent
SetTimer, PressTheKey, 300000
Return

PressTheKey:
Send, {F5}
Return

It does require you to download and install a free open source utility called AutoHotKeys.

[An added advantage of installing this utility is that Nelson Davis wrote an AutoHotKeys script that will autosave your Tableau work every x minutes! Something Tableau still does not do. Here’s his blog on Autosave. Good explain of using AHK toward the end.]

You also could do this with a batch file script, but that’s a bit more complicated so I’ll leave that for another day.

Server Auto-Refresh

In Server we can use the Tableau JavaScript API to do the refreshing for us. (Note: JavaScript API does not work for Desktop.) Here’s the script Hugh put together to refresh data every 3 seconds (3,000 milliseconds).

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Tableau JavaScript API</title>
<script type=”text/javascript” src=”https://online.tableausoftware.com/javascripts/api/tableau_v8.js“></script>
</head>
<body>
<div id=”tableauViz”></div>

<script type=’text/javascript’>
var placeholderDiv = document.getElementById(“tableauViz”);
var url = “https://online.tableausoftware.com/t/shawnwallworktableauonline/views/AutoRefreshTest/Dashboard1“;
var options = {
hideTabs: true,
width: “100%”,
height: “1000px”
};
var viz = new tableauSoftware.Viz(placeholderDiv, url, options);

setInterval(function () {viz.refreshDataAsync() }, 3000);

</script>
</body>

Replace the parts in bold with your own specifics and you’re good to go. Also if you change that third line from the bottom to 300,000 it will refresh every 5 minutes.

As long as you have a fairly quick data connection this is the best solution. However, if you have a slow data connection like I was dealing with, then Bill came up with a clever idea for dealing with this situation. He reasoned that if we pre-load several tabs with our views and then use an AutoHotKey script to cycle through the tabs, then the slow loading pages would load before they reached the view position. And he was right. I ended up using a modified version of his script to cycle through 16 tabs, and most of the time the pages do load prior to being cycled into view (yes this particular database is a real dog).

The added benefit of this approach is that you can rotate through several different dashboards/views. Here’s Bill’s latest version of his script. Just make sure to add the ?:refresh=yes parameter to the end of your URLs in the Excel file or your data won’t get refreshed, just your views will be refreshed.

EDIT: And for an extra bonus, I found this post by Tableau’s Jordan Bunker Rotating Dashboard Display He uses Javascript to rotate through dashboards listed in a csv file, similar to what Bill did in his AHK script. It doesn’t look like Jordan is refreshing the data, just rotating the views.

7 comments on “Tinkering with Data Refresh & Auto-Refresh

  1. vizpainter says:

    I really like AutoHotkey — lot’s of fun things you can do… Thanks for the scripts, Shawn!

    Like

  2. Aiswarya Sundaram says:

    Exactly what i was looking for Shawn! Going to try it soon.So excited to see your blog! 🙂

    Like

  3. Jeff Lee says:

    I see that you have the src= line as bold. What goes in that line? I have the actual url for the dashboard after the url=…

    Like

  4. The src= line looks to be telling Tableau where to find the the Tableau API javascript source code. Your url looks like it goes in the second bolded statement.

    Like

  5. Andy Bryan says:

    Just on your point on keystrokes for invoking a Data Extract refresh, this can be achieved by Alt + d + x. You’ll need to hit return after this and potentially enter server credentials (if you are connected to a MySQL/MS SQL database) but is at least some form of automation

    Like

  6. jd says:

    Would there be a way to drop this script into a tableau server config file so that when I navigate to the viz from Tableau Server the viz auto-refreshes?

    Like

  7. JB says:

    hmm any documentation to start at the scratch?
    I think I need to upload the script to the tableau webserver and store it as html?
    And how does the worksheet now know when I open it in Tableau Server to use this script for autorefresh? Did I miss something?

    Like

Leave a comment