14 Replies Latest reply on Jan 26, 2015 9:05 AM by Lucas Chiu

    How to get valid filter values from JS API?

    Jason Hihn

      function filterSingleValue() {

        activeSheet.applyFilterAsync(

        "Region",

        "The Americas",

        tableauSoftware.FilterUpdateType.REPLACE);

      }

       

      The red text above is a value that is in the blue filter. How can I get the list of valid values? All the examples use hard coded values and lists. i.e. ['The Americas', 'Europe']

        • 1. Re: How to get valid filter values from JS API?
          Malcolm McRoberts

          I have the same question with regard to the

          selectMarksAsync(

          fieldValuesMap: object,

          updateType: SelectionUpdateType)

           

          API call.

           

          I've got the country ranks demo dashboard up.http://<server>/views/WorldIndicators/Countryranks

          and I'm trying to mark the cell that's got the population of Italy.  My call looks like this:

           

          selectMarksAsync(

                      {

                          "Country / Region": "Italy",

                          "Measure Names":"Population"

                      },

                      tableauSoftware.SelectionUpdateType.REPLACE)

           

          This does not generate an error, but it does not select any marks.

           

          I'm able to select the whole row for Italy just fine using this call

          selectMarksAsync(

               "Country / Region",

                "Italy",

                 tableauSoftware.SelectionUpdateType.REPLACE)

           

          The tutorial only show's this syntax.  A realistic example of the other syntax would be a big help.  And I think it would answer the original question as well.

          • 2. Re: How to get valid filter values from JS API?
            Mark Roeser

            Malcolm,

             

            Have you been able to get the "Measure Names" field name reference to work in isolation? Using listeners, I have found that Measures does indeed identify as "Measure Names", but the measure names it reports are not the friendly display names. Rather, they are fully qualified names that include datasource, agg type, etc.

             

            For example:

            • [Peformance Topline TDE (local copy) (copy)].[sum:revisit_total_cnt:qk]
            • [Peformance Topline TDE (local copy) (copy)].[usr:Calculation_3910109103413778:qk]

             

            I was able to discover these via the API with this trio of functions:

             

            function onFilterChange(filterEvent) {

              filterEvent.getFilterAsync().then(reportSelectedFilter);

            }

             

            function reportSelectedFilter(field) {

              var fname = field.getFieldName();

              var ftype = field.getFilterType();

              if (fname == "Measure Names") {

                   var measureList = '';

                   values = field.getAppliedValues();

                   for (i = 0; i < values.length; i++) {

                        measureList += "<li>" + values[i].value + "\n";

                   }

                   document.getElementById('measure-names').innerHTML="<ul>\n" + measureList + "\n</ul>\n";

              }

            }

             

            function listenToFilterSelection(viz) {

              viz.addEventListener('filterchange', onFilterChange);

            }

             

            At any rate, when I try to set a filter specifying the "Measure Names" field type, it errors, with the error message simply echoing "Measure Names", which is the same behavior when using any invalid/random value for field type.

             

            function filterIt(viz, objName, theThing) {

              var sheet = viz.getWorkbook().getActiveSheet();

             

              if(sheet.getSheetType() == 'worksheet') {

                   sheet.applyFilterAsync(objName, theThing, 'REPLACE');

              } else {

                   worksheetArray = sheet.getWorksheets();

                   for(var i = 0; i < worksheetArray.length; i++) {

                        worksheetArray[i].applyFilterAsync(objName, theThing, 'REPLACE')

                             .otherwise(function (err) {

                                  alert("Problem!:\n\n " + err);

                             });

                   }

              }

            }

             

            It makes me wonder if manipulating measures via the API isn't fully supported?

            • 3. Re: How to get valid filter values from JS API?
              Malcolm McRoberts

              I also tried

              "[World Indicators new].[sum:F: GDP (curr $):qk]"

              But that didn't select anything either.  Not sure I'm getting the syntax correct.  I'm using selenium from python, but keeping the various quote styles and escapes straight between python and JS is a challenge.

               

              Do you think I'd have better luck marking a different dimension?  I thought this one would be easy.

              • 4. Re: How to get valid filter values from JS API?
                Mark Roeser

                As a sanity check, it might help to try making calls directly on the page to eliminate any possible complications from python & selenium.

                 

                From what I'm seeing with my work, I can access other dimensions just fine with the API calls. It's the measures that are behaving differently.

                 

                I'm hoping that there is some way to manipulating measures (selections and filters). Anyone from Tableau around to provide any guidance?

                • 5. Re: How to get valid filter values from JS API?
                  Russell Christopher

                  This blog entry might help you - it sort walks you through discovering what the "ugly" name of the measure is under the covers so you don't have to guess:

                   

                  Filtering on Measure Names on the URL in Tableau | Tableau Love

                  • 6. Re: How to get valid filter values from JS API?
                    Mark Roeser

                    Thanks Russell. I love your blog.

                     

                    Via the js API, it's possible to use a listener to get the currently selected measure names, in all of their ugly glory. Do you know if it's possible at all to filter measures via the API?

                     

                    The measure field presents itself as "Measure Names" via the listener, but the filter doesn't seem to recognize that name as a valid field. Is this a quirk of the measure field not being a true dimension? Any API-based workarounds you know of?

                     

                    Thanks!

                    • 7. Re: How to get valid filter values from JS API?
                      Malcolm McRoberts

                      Thanks, but I'm not able to translate that directly to my problem.  I'm actually trying to select marks, which shoudl be syntactically similar to filtering.  I'm running this against the country ranks sample dashboard.  Do you see anything obviously wrong here?  If I just use the first selector, then it selects the Italy row just fine, but if I provide both, then 0 marks get selected.  I got this value by looking a the marks from the whole row that I selected using just Italy.

                       

                      selectMarksAsync(

                                  {

                                      "Country / Region": "Italy",

                                      "Measure Names": "[World Indicators new].[sum:P: Population (count):qk]"

                                  },

                                  tableauSoftware.SelectionUpdateType.REPLACE);

                      • 8. Re: How to get valid filter values from JS API?
                        Mark Roeser

                        I believe the issue is probably with Measure Names. Unless you specifically include handling for it, any errors will be silent. So perhaps you can add .otherwise to your async call?

                         

                        selectMarksAsync(

                                    {

                                        "Country / Region": "Italy",

                                        "Measure Names": "[World Indicators new].[sum:P: Population (count):qk]"

                                    },

                                    tableauSoftware.SelectionUpdateType.REPLACE)

                                         .otherwise(function (err) {

                                              alert("Problem!\n\n " + err);

                                         });

                        • 9. Re: How to get valid filter values from JS API?
                          Russell Christopher

                          Mark, doesn't appear you can do this (see attached)

                           

                          applyFilterAsync doesn't seem to "respect" [:Measure Names] as a filter (look for //DON'T comment)

                           

                          However, you can apply a Measure Name filter when you initially instantiate:

                           

                          var placeholderDiv = document.getElementById("content");
                            var url = "https://tableau.russellchristopher.org/t/SkunkWorks/views/FilterMe/TheSheet";
                            var options = {
                            hideTabs: true,
                            hideToolbar: true,
                              //WORKS 
                              '[:Measure Names]':'[Sample - Superstore Sales (Excel)].[sum:Profit:qk]',
                            onFirstInteractive: function () {
                            mainWorkbook = mainViz.getWorkbook();
                            }
                            };
                            try{
                            mainViz = new tableauSoftware.Viz(placeholderDiv, url, options);
                            }
                            catch (e){
                            alert(e);
                            }
                          
                          • 10. Re: How to get valid filter values from JS API?
                            Malcolm McRoberts

                            I suppose the same holds true for selecting marks via the API?

                            • 11. Re: How to get valid filter values from JS API?
                              Russell Christopher

                              Didn't test it, but I wouldn't think so. Question: Why are you trying to select the Measure Name in the first place? I would think that if you choose marks via a dimension (only), then whatever measure happens to be displayed is going to get selected...You then could read the value off the pairs..

                              • 12. Re: How to get valid filter values from JS API?
                                Malcolm McRoberts


                                I happen to pic the country ranks sample dashboard to try this out on.  That one come up with countries for rows, and each column is a different measure.  I was just trying to mark a single cell.  I can always grab the row, and sift through it to find the column I need.  But this does sound like a bug.  If I wanted to embed that Viz in a web page and write controls for it, then this kid of layout would be hard to control.

                                • 13. Re: How to get valid filter values from JS API?
                                  Mark Roeser

                                  Thanks Russell. I was able to get an example of this approach working in my playground. With the proper constraints and context, this can be useful.

                                  • 14. Re: How to get valid filter values from JS API?
                                    Lucas Chiu

                                    Hi,

                                     

                                    I have the exact same issue as the Original Post (Jason). And I read through all the posts, but I am not sure how the measures actually relate to being able to find all the valid values in a filter. I would just like a list of these values, so that I coudl verify that user input is correct.

                                     

                                    Regards,

                                     

                                    Lucas