Home | About | Log in | Get The Feed
Tableau 101 – Using DATEDIFF for Dynamic Date Filters

February

28

2009

Tableau SoftwareWhen you’re using Tableau and it is back-ended with a very large data source (an enterprise data warehouse for instance) one of the issues is making the source data that you’re working with manageable. You definitely don’t want to be querying the whole thing every time you refresh a report (probably) as this makes the workbook slower than it needs to be and makes exporting a packaged workbook for others to use nearly pointless (if it will export at all).

One of the simplest things to do is limit the time frame for the report. For instance, maybe the report you’re working on only needs the last 30 days of data or the last 6 months or the last year.

But how do you do this in Tableau?

Beginners, myself included, will just drop the date field of choice onto the filter shelf and manually set the date range required. The problem is that every time you open the report you have to manually adjust your date filter so it’s not helping you as much as it could be.

Enter DATEDIFF. This function – as the name implies – calculates the difference between two dates at a specific granularity. That’s right, it will calculate the difference in dates by number of days, weeks, months, etc. How cool is that?

Ok, so how does that help create a filter that returns only the last N days of data?

It’s so simple you might think the above is a trick question some how.

Before I get started on directions, I’m going to assume that you’re already in your workbook and connected to your data source. For the purpose of this exercise, I’ll be using our friend – SuperStore Data.

  1. Start by opening the ‘Create Calculated Field’ dialog boxTableau Context Menu
  2. Let’s call this new field ’30day-Filter’Name The Calculated
  3. Tab down (or click in) the ‘Formula Field
  4. Type in DATEDIFF(
  5. The first argument in DATEDIFF is the granularity to calculate with (day, week, month, etc). For our 30day-Filter we’ll use ‘day’
  6. So far you should have DATEDIFF('day'
  7. Arguments in a function are separated by commas so add a comma after ‘day’
  8. The next argument is the start date (e.g. the oldest date) which will be the date from your data source record. In this case, the field we want is ‘Order Date’
  9. Our new function should look like this so far: DATEDIFF('day',[Order Date],
  10. For the end date, we’re going to use another function – TODAY(). TODAY() takes no arguments and returns the current date from your data source. If you’re connected to a remote data server TODAY() will reflect the date on the server. If you’re working with a local data source (text file, excel, etc) then TODAY() will reflect the time on your local machine
  11. So far we have DATEDIFF('day',[Order Date],TODAY()) This is a valid function all by itself and will calculate the difference between the two dates with ‘day’ granularity, but it’s not a filter yet
  12. In this case we want a filter that evaluates to ‘TRUE’ or ‘FALSE’ and really, it’s very simple. Just add ‘<=30' to the end of the function
  13. DATEDIFF('day',[Order Date],TODAY())<=3030day-filter
  14. Save the calculated metric

notice that in the Dimension shelf your new metric has an icon of T/F. Now we're ready to use the filter! Go ahead and add it to your filter shelf.

A couple of last points on using this filter to make your report dynamic in terms of the date range. Make the filter global - remember the date filter should encompass all of the data you need for the report, and making it global makes it apply to all worksheets automatically. Add the filter to your context - this limits the data Tableau is working with on the server side which should improve the speed of all subsequent queries (less rows to traverse for an answer).

Tags: ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

prednisone buy cod watson brand order prednisone on line buy Crestor cod buy 40 mg accutane online without prescription buy Nizoral Online order generic Crestor cytotec online cheap cytotec from mexico buy Premarin no prescriptions order Valtrex without a prescription overnight shipping Valtrex bestellen purchase 10mg accutane 40 mg overnight delivery buy prednisone without rx needed prednisone without a prescription canadian i need to order prednisone without a prescription prednisone purchase without perscripotion buy synthroid mail buy buy zithromax no scams purchase Crestor overnight purchase Crestor pay pal without rx generic finpecia online finpecia usa order Crestor uk best buy valtrex comprar Crestor generico buy cheap Zithromax order buy Zithromax online where can i purchase Buspar no rx buy Atarax without doctor ordering Atarax without a script purchase Atarax cod overnight delivery purchase Atarax buy Lisinopril in india Lisinopril no doctors prescription purchase Atarax without rx to ship overnight buy Atarax cheap (Metformin espana|purchase Metformin|Metformin ohne rezept|Metformin pills|what is Metformin used for|price of Metformin|best buy Metformin|Metformin preis|medikament Metformin|what does Metformin look like|buy Metformin c o d|Metformin 1000 mg|generic Metformin online|Metformin purchase|best buy Metformin|buy Metformin usa|where to buy Metformin|Metformin rezept|Metformin mexico|buy Metformin with no prescription|buy Metformin canada|where to buy Metformin|buy Metformin cheap without prescription|comprare Metformin generico|discount Metformin|buy Metformin online no rx|Metformin purchase|pharmacy Metformin|comprar Metformin generico|uk Metformin generic|Metformin toronto|pharmacy Metformin|Metformin price|cheap Metformin online|buy Metformin without prescription|buy Metformin where|Metformin online purchase|buy Metformin online|online Metformin|online Metformin purchase|uk order Metformin|how to buy Metformin without a prescription|order Metformin withou buy no perscription Buspar prednisone with out a prescription Metformin fedex shipping Xenical cash on delivery no rx Flomax with fedex purchase Premarin cod overnight delivery generic valtrex new zealand buy Crestor on line online purchase Valtrex no prescription required for prednisone buy Premarin us buy Premarin without a rx buy Maxalt once a day buy prednisone without rx needed buy Proscar offshore no prescription fedex buy Valtrex us discount Orlistat buy Atarax online buy herbal Atarax buy cheap Orlistat line order Orlistat without rx needed accutane without prescriptions Buy xenical online with next day shipping Accutane ordering without a dr purchase Accutane no scams Maxalt on line buy Donepezil and Premarin buy Valtrex on line without a rx buy Valtrex and Valtrex ventolin overnight without prescription buy generic Crestor online purchase online prescription Valacyclovir order overnight Zithromax Amitriptyline by mail Zithromax buy Proscar 5 mg purchase cheap online prednisone uk Prednisone generic Buy xenical online without a prescription Buy xenical online perscription uk order Nizoral Adobe Acrobat X Pro buy cheapest Maxalt and Maxalt buy in Valtrex uk prednisone with no perscription overnight shipping buy Prednisone without a credit card buy Prednisone cheap without prescription Proscar from canada 120 mg xenical xenical online purchase buy prednisone want to buy Maxalt in usa prednisone no prescription with mastercard how to purchase Cytotec online without rx buy Valtrex in mo Valtrex buy prednisone over the counter Xenical best buy i want to buy Atarax without a perscription buy Cytotec no prescription low cost generic Atarax cost Rosuvastatin overnight overnight Atarax c.o.d