<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Construct Filter with Javascript function in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/101371#M3533</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently working on bringing Power BI Embedded to a place that our company can think of using this more often. One limitation I can currently see is potentially needing different filters/filter styles from page to page. Rather than constantly type out the code for the filter, I thought I would build a function to make them based off of inputted parameters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EX: I have a filter:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            var useFilter = {
                $schema: "http://powerbi.com/product/schema#advanced",
                target: {
                    table: table,
                    column: column
                },
                logicalOperator: 'And',
                conditions: [
                    {
                        operator: "Is",
                        value: value
                    }
                ]
            };&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where table, column, and value were some hard coded variables I had in the script section. Now, this is fine for a one off, but isn't dynamic enough for what we might need. So I created a function to try and return this object, based off of inputted parameters. Here is the function:&lt;/P&gt;&lt;PRE&gt;function advancedFilter(table, column, logicalOperator, operator, value){
    
    if (logicalOperator === undefined) {
        logicalOperator = "And";
    }
    if (operator === undefined) {
        operator = "Is";
    }
    if (value === undefined) {
        value = 0;
    }

    var filter = {
        $schema: "http://powerbi.com/product/schema#advanced",
        target: {
            table: table,
            column: column
        },
        logicalOperator: logicalOperator,
        conditions: [
            {
                operator: operator,
                value: value
            }
        ]
    };
}&lt;/PRE&gt;&lt;P&gt;I would then set a variable like so:&lt;/P&gt;&lt;PRE&gt;var useFilter = advancedFilter(table, column, "And", "Is", arr)&lt;/PRE&gt;&lt;P&gt;Where arr is a basic array variable with a value (104 for example) in it. I have also tried just passing the raw value instead of an array. What am I doing wrong here? Every time I try to use this, I get an error:&amp;nbsp;Uncaught (in promise) [Array[4]]. If I use the example filter at the top, without the function, everything works fine. Anyone have answers?&lt;/P&gt;</description>
    <pubDate>Mon, 12 Dec 2016 20:16:40 GMT</pubDate>
    <dc:creator>Coltsbro</dc:creator>
    <dc:date>2016-12-12T20:16:40Z</dc:date>
    <item>
      <title>Construct Filter with Javascript function</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/101371#M3533</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently working on bringing Power BI Embedded to a place that our company can think of using this more often. One limitation I can currently see is potentially needing different filters/filter styles from page to page. Rather than constantly type out the code for the filter, I thought I would build a function to make them based off of inputted parameters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EX: I have a filter:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            var useFilter = {
                $schema: "http://powerbi.com/product/schema#advanced",
                target: {
                    table: table,
                    column: column
                },
                logicalOperator: 'And',
                conditions: [
                    {
                        operator: "Is",
                        value: value
                    }
                ]
            };&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where table, column, and value were some hard coded variables I had in the script section. Now, this is fine for a one off, but isn't dynamic enough for what we might need. So I created a function to try and return this object, based off of inputted parameters. Here is the function:&lt;/P&gt;&lt;PRE&gt;function advancedFilter(table, column, logicalOperator, operator, value){
    
    if (logicalOperator === undefined) {
        logicalOperator = "And";
    }
    if (operator === undefined) {
        operator = "Is";
    }
    if (value === undefined) {
        value = 0;
    }

    var filter = {
        $schema: "http://powerbi.com/product/schema#advanced",
        target: {
            table: table,
            column: column
        },
        logicalOperator: logicalOperator,
        conditions: [
            {
                operator: operator,
                value: value
            }
        ]
    };
}&lt;/PRE&gt;&lt;P&gt;I would then set a variable like so:&lt;/P&gt;&lt;PRE&gt;var useFilter = advancedFilter(table, column, "And", "Is", arr)&lt;/PRE&gt;&lt;P&gt;Where arr is a basic array variable with a value (104 for example) in it. I have also tried just passing the raw value instead of an array. What am I doing wrong here? Every time I try to use this, I get an error:&amp;nbsp;Uncaught (in promise) [Array[4]]. If I use the example filter at the top, without the function, everything works fine. Anyone have answers?&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 20:16:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/101371#M3533</guid>
      <dc:creator>Coltsbro</dc:creator>
      <dc:date>2016-12-12T20:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: Construct Filter with Javascript function</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102181#M3552</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17541"&gt;@Coltsbro﻿&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;Below code works in my test. You can debug your filter by using the alert statement before applying it.&amp;nbsp;Check more details on &lt;A href="https://github.com/Microsoft/PowerBI-JavaScript/wiki/Filters" target="_self"&gt;Filters&lt;/A&gt;. By the way, do note that when using "In", it is value&lt;STRONG&gt;s&lt;/STRONG&gt; and when other operators it is a scalar value. You may have to put more logics in your function.&lt;/P&gt;
&lt;PRE&gt;var arr = [1,2,3,4,6,8]

function advancedFilter(table, column, logicalOperator, operator, value){
    
    if (logicalOperator === undefined) {
        logicalOperator = "And";
    }
    if (operator === undefined) {
        operator = "Is";
    }
    if (value === undefined) {
        value = 0;
    }

    &lt;STRONG&gt;return&lt;/STRONG&gt; filter = {
        $schema: "http://powerbi.com/product/schema#advanced",
        target: {
            table: table,
            column: column
        },
        //logicalOperator: logicalOperator,
        //conditions: [
        //    {
                operator: operator,&lt;BR /&gt;                //It is "values" when using a operator "In"
                &lt;STRONG&gt;values&lt;/STRONG&gt;: value
        //    }
        //]
    };
}


alert(JSON.stringify(advancedFilter("table","column","","In",arr)));&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 09:21:45 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102181#M3552</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2016-12-14T09:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Construct Filter with Javascript function</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102307#M3557</link>
      <description>&lt;P&gt;This is now working for operators besides "In".&amp;nbsp;Modified code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;function advancedFilter(table, column, logicalOperator, operator, value){
    
 if (logicalOperator == undefined || logicalOperator == "" || logicalOperator == null) {&lt;BR /&gt; logicalOperator = "And";&lt;BR /&gt; }&lt;BR /&gt; if (operator == undefined || operator == "" || operator == null) {&lt;BR /&gt; operator = "Is";&lt;BR /&gt; }&lt;BR /&gt; if (value == undefined || value == "" || value == null) {&lt;BR /&gt; value = 0;&lt;BR /&gt; }

    if (operator == "In") {
        return filter = {
            $schema: "http://powerbi.com/product/schema#advanced",
            target: {
                table: table,
                column: column
            },
            logicalOperator: logicalOperator,
            conditions: [
                {
                    operator: operator,
                    values: value
                }
            ]
        };
    }
    else {
        return filter = {
            $schema: "http://powerbi.com/product/schema#advanced",
            target: {
                table: table,
                column: column
            },
            logicalOperator: logicalOperator,
            conditions: [
                {
                    operator: operator,
                    value: value
                }
            ]
        };
    }
    
}&lt;/PRE&gt;&lt;P&gt;Called in the view:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;var arr = [104, 102, 117];&lt;BR /&gt;var useFilter = advancedFilter(table, column, "", "In", arr);&lt;/PRE&gt;&lt;P&gt;Pushed to the report filters:&lt;/P&gt;&lt;PRE&gt;                report.on('loaded', event =&amp;gt; {
                    report.getFilters()
                    .then(filters =&amp;gt; {
                        filters.push(useFilter);
                        return report.setFilters(filters);
                    })
                })&lt;/PRE&gt;&lt;P&gt;It may also be worth noting that I have the function in a separate javascript file that I am referencing in the view. Here is what returns in the alert text box:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{  
   "$schema":"http://powerbi.com/product/schema#advanced",
   "target":{  
      "table":"SiteInfo",
      "column":"SiteId"
   },
   "logicalOperator":"And",
   "conditions":[  
      {  
         "operator":"In",
         "values":[  
            104,
            102,
            117
         ]
      }
   ]
}&lt;/PRE&gt;&lt;P&gt;I still get the error message in the developer console on the web page: Uncaught (in promise): Array[4]. &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Array[4]
0
:
Object
message
:
"operator is invalid. Not meeting required constraint"
__proto__
:
Object
1
:
Object
message
:
"values is invalid. Not meeting required constraint"
__proto__
:
Object
2
:
Object
message
:
"conditions.0.value is invalid. Not meeting required constraint"
__proto__
:
Object
3
:
Object
message
:
"filter is invalid"&lt;/PRE&gt;&lt;P&gt;What do you suggest?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 14:02:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102307#M3557</guid>
      <dc:creator>Coltsbro</dc:creator>
      <dc:date>2016-12-14T14:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: Construct Filter with Javascript function</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102318#M3558</link>
      <description>&lt;P&gt;Disregard. It appears that "In" is specific to a basic filter, which is why I was throwing an error. All seems to be working perfectly now, than you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 14:16:01 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102318#M3558</guid>
      <dc:creator>Coltsbro</dc:creator>
      <dc:date>2016-12-14T14:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Construct Filter with Javascript function</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102551#M3565</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/17541"&gt;@Coltsbro﻿&lt;/a&gt;&lt;/P&gt;&lt;P&gt;You're always welcome. &lt;img id="smileylol" class="emoticon emoticon-smileylol" src="https://community.fabric.microsoft.com/i/smilies/16x16_smiley-lol.png" alt="Smiley LOL" title="Smiley LOL" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 00:42:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Construct-Filter-with-Javascript-function/m-p/102551#M3565</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2016-12-15T00:42:25Z</dc:date>
    </item>
  </channel>
</rss>

