Programmatically Changing Filters on Tableau Extracts

When you have a lot of Tableau extracts (TDEs), you want them to be as small as possible for both generation and performance in a workbook. If you are deploying extracts to a lot of different sites, you may have built an initial extract that is filtered down for Customer A on Site A, but want to publish it to Site B and have it filtered for Customer B. It’s not too difficult, although is does require working with the XML a bit.

Tableau filters work in this order, relatively:

  1. Filters on extract (applied as a WHERE clause on the query used to pull the data for the extract)
  2. Data Source Filters (applied as WHERE clause at all times)
  3. Context Filters
  4. Filters that are set to “Apply to all Sheets Using This Datasource”
  5. Filters that only apply to a given worksheet

Often I see Data Source Filters being used on an extract, which has the same effect for the end user, but the extract itself will still pull all of the information for all of the customers. The only place an Extract filter can be set is in the dialog that pops up when you create the extract.

extract filter location

In the workbook XML, there is a section of extract tags that give the information about how the extract will be regenerated.

For example, if I’ve put a filter on Region to only show ‘South’, the following appears in the XML.


</connection>
<filter class='categorical' column='[Region]'>
<groupfilter function='member' level='[Region]' member='&quot;South&quot;' user:ui-domain='database' user:ui-enumeration='inclusive' user:ui-marker='enumerate' />
</filter>

Now, it’s pretty obvious that the member attribute is all the needs to change to change the definition of the filter.

Here’s what it looks like if you select more than one option:


<filter class='categorical' column='[Region]'>
<groupfilter function='union' user:ui-domain='database' user:ui-enumeration='inclusive' user:ui-marker='enumerate'>
<groupfilter function='member' level='[Region]' member='&quot;South&quot;' />
<groupfilter function='member' level='[Region]' member='&quot;West&quot;' />
</groupfilter>
</filter>

How do we work with this if we want to publish our extracts to the Server using the REST API (or tabcmd)? In that case, you should be saving the extract as a TDSX (Right-click on the data source, then choose “Add to Saved Data Sources”, then select “Tableau Packaged Data Source…”

tdsx

A TDSX file is encoded using ZIP, and contains both the TDS that tells how to build the extract, and the TDE file itself (See this previous post on how to open up and modify these files).

The TDS inside the TDSX will have the same filter and groupfilter sections, which is you modify and pack the thing back up as a TDSX, will then be used the next time the extract is refreshed (including once it is published to Server).

I don’t have the ability to change the filters built into the tableau_rest_api library, but clearly you can build out a programmatic process to do all of this.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s