3 Replies Latest reply on Dec 1, 2014 11:51 AM by Guilherme Toschi

    Javascript API - how do I filter a date range?

    Rob Graham

      Hi,

       

      I have a report that has a filter on a datetime column.  The filter is set to filter a range of dates.  I have embedded the report via the viz API and in code I am calling...

       

      viz.filter({report_time: ["2013-01-01", "2013-01-31"]});

       

      To attempt to filter the whole of Jan.  It does not appear, however, to be working.  The set of data that comes back is way to small.

       

      I have also tried many other date formats such as

       

      31/01/2013

      01/31/2013

      1st Jan 2013

       

      All these fail - the server comes back with an error.

       

      Thanks in advance,

       

      Rob.

        • 1. Re: Javascript API - how do I filter a date range?
          Russell Christopher

          Hey Rob -

           

          In the version 7 of the API there is no way to do this. You CAN in the soon-to-be-released / radically improved / awesome API which comes with Tableau Server 8.

           

          In 7, most people approach this problem by building a calculated field with two parameters as inputs. You populate the parameters on the URL or via JavaScript instead of the filters and use the calculated field as a filter

           

          Param: BeginDate

          Param: EndDate

           

          MyField: [Date] >= BeginDate AND [Date] <= EndDate

           

          MyField=True // your filter

           

          As I recall, all viz.filter() does is place the string &SomeFilterName=SomeValue at the end of the URL that we fire, so I think it'll work equally well for a parameter.

           

          Good luck!

          • 2. Re: Javascript API - how do I filter a date range?
            Rob Graham

            Hi Russell,

             

            Thanks for the reply, I look forward to playing with Tableau 8!

             

            I've managed to setup the custom filter with the device parameters but unfortunately I still cannot figure out the format of the dates I am supposed to inject.

             

            The two parameters I have setup (called startDate and endDate) have a format of Date & time which in the current value field shows a format of:

             

            dd/mm/yyyy HH:MM:ss

             

            I have tried injecting this as a filter with...

             

            viz.filter({startDate: "01/01/2013 00:00:00"});

            viz.filter({endDate: "29/01/2013 23:59:59"});

             

            But I still get "Invalid value in '29/01/2013%2023:59:59' for filter 'endDate'"

             

            What format should the passed dates be in?

             

            Thanks,

             

            Rob

            • 3. Re: Javascript API - how do I filter a date range?
              Guilherme Toschi

              Hello,

               

              I found on the Tableau API tutorial:

               

              return activeSheet.applyRangeFilterAsync(

                "Date (year)",

                {

                min: new Date(Date.UTC(2002, 1, 1)),

                max: new Date(Date.UTC(2008, 12, 31))

                },

                tableauSoftware.FilterUpdateType.REPLACE);

               

              It may help you, helped me!

               

              Thanks