Quantcast
Channel: All Developer posts
Viewing all articles
Browse latest Browse all 48084

Construct Filter with Javascript function

$
0
0

Hi all,

 

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.

 

EX: I have a filter: 

            var useFilter = {
                $schema: "http://powerbi.com/product/schema#advanced",
                target: {
                    table: table,
                    column: column
                },
                logicalOperator: 'And',
                conditions: [
                    {
                        operator: "Is",
                        value: value
                    }
                ]
            };

 

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:

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
            }
        ]
    };
}

I would then set a variable like so:

var useFilter = advancedFilter(table, column, "And", "Is", arr)

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: Uncaught (in promise) [Array[4]]. If I use the example filter at the top, without the function, everything works fine. Anyone have answers?


Viewing all articles
Browse latest Browse all 48084

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>