3 Replies Latest reply on Dec 29, 2016 2:08 AM by Nikunj Bhardava

    Set default value to filter using tableau JS API

    Nikunj Bhardava

      Hi All,

       

      Tableau Version : 9.2

       

      I am trying to set default value to date filter (MY(Date)) using tableau JS API. I am trying to set it in Option object while creating tableau viz.

      My dates in filters are looks like as below. Its calculate field. and format like MY(Date) in filter shelf.

       

      See below code for initializing the tableau Viz

       

      function initViz()

      {

         var containerDiv = document.getElementById("vizContainer");

         url = "http://servername/views/NHSN_Batch_Submission_WB_TST_3/Facility_Batch_Status?:embed=y&:showShareOptions=true&:display_count=no&:showVizHome=no",

        options =

        {

               "MY(Date)":"October 2016",

               height:"800px",

               width:"100%",

               embed: 'yes',

               hideTabs: true,

               hideToolbar: false,

               onFirstInteractive: function ()

               {

                    Activesheet = viz.getWorkbook().getActiveSheet();

               }

        };

        viz = new tableauSoftware.Viz(containerDiv, url, options);

       

        }

       

      But it is not working. i.e default is not set to date filter as October 2016.

       

      However I tried simple using like MONTH(DATE) instead of MY(DATE) in options and it is working fine.

       

      Any Idea? why this is not working

       

      Any help will highly appreciated!!

       

      Thanks,

      Nikunj

        • 1. Re: Set default value to filter using tableau JS API
          Dan Huff

          Nikunj--

           

          This is due to how we store these values internally. While the field is represented as a string, it is actually a integer that we then transform at display time. In your case, you should be able to use a value like 201610 to filter for October 2016 of your MY(Date) field.

           

          You can always figure out things like this by following the steps below.

           

          • Right click your date field
          • Hover over create and click custom date
          • Select Month / Year and click OK
          • Right click the created field and click Describe
          • Copy the formula that appears into a new calculation
          • Drag this calculation into the view to see what the real value is

           

          I hope this helps out.


          Dan

          • 2. Re: Set default value to filter using tableau JS API
            Dan Huff

            Also, thanks a million for such a thorough and clear question!

             

            Dan

            • 3. Re: Set default value to filter using tableau JS API
              Nikunj Bhardava

              Hi Dan,

               

              Heartly thanks Dan!! your suggestion and guide worked perfectly. many thanks, since I played too much to get this work but I failed every time.

               

              Can you help me more? I have to apply same filter to another dashboard also but that filter is coming from blended data-source.

               

              Like this: I have column from blended source  : "report period str" and it is calculated field from date column like below. It doing nothing but just populating "year-quarter" or "year-month" in proper format as per parameter selected.

               

              case [p_frequency]

              when 'month' then str(datepart('year',[report period]))+'-'+

                  (if datepart('month',[report period]) < 10 then '0' else '' end)+

                  str(datepart('month',[report period]))

              else str(datepart('year',[report period]))+'-Q'+str(datepart('quarter',[report period]))

              end

               

              I am using below JS option while initializing Viz.

               

              var viz;

              var currentSheet , Activesheet;

               

              function initViz()

              {

                        var containerDiv = document.getElementById("vizContainer");

                        url = "servername/viewsworkbookname/viewname?:embed=y&:showShareOptions=true&:display_count=no&:showVizHome=no",

                        options =

                        {

                             "report period str": "2016-05",

                              height:"800px",

                              width:"100%",

                              embed: 'yes',

                              hideTabs: true,

                              hideToolbar: false,

                             onFirstInteractive: function ()

                             {

                                  Activesheet = viz.getWorkbook().getActiveSheet();

                             }

                        };

                       viz = new tableau.Viz(containerDiv, url, options);

              }

              $(document).ready(function()

              {

                        initViz();

              });

               

              But above script is not setting default filter to "2016-05"

               

              Thanks once again Dan!!

               

              Thanks,

              Nikunj