Forum Discussion
Construct Filter with Javascript function
- 9 years ago
Coltsbro
Below code works in my test. You can debug your filter by using the alert statement before applying it. Check more details on Filters. By the way, do note that when using "In", it is values and when other operators it is a scalar value. You may have to put more logics in your function.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; } return filter = { $schema: "http://powerbi.com/product/schema#advanced", target: { table: table, column: column }, //logicalOperator: logicalOperator, //conditions: [ // { operator: operator,
//It is "values" when using a operator "In" values: value // } //] }; } alert(JSON.stringify(advancedFilter("table","column","","In",arr)));
Coltsbro
Below code works in my test. You can debug your filter by using the alert statement before applying it. Check more details on Filters. By the way, do note that when using "In", it is values and when other operators it is a scalar value. You may have to put more logics in your function.
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;
}
return filter = {
$schema: "http://powerbi.com/product/schema#advanced",
target: {
table: table,
column: column
},
//logicalOperator: logicalOperator,
//conditions: [
// {
operator: operator,
//It is "values" when using a operator "In"
values: value
// }
//]
};
}
alert(JSON.stringify(advancedFilter("table","column","","In",arr)));
This is now working for operators besides "In". Modified code:
function advancedFilter(table, column, logicalOperator, operator, value){
if (logicalOperator == undefined || logicalOperator == "" || logicalOperator == null) {
logicalOperator = "And";
}
if (operator == undefined || operator == "" || operator == null) {
operator = "Is";
}
if (value == undefined || value == "" || value == null) {
value = 0;
}
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
}
]
};
}
}Called in the view:
var arr = [104, 102, 117];
var useFilter = advancedFilter(table, column, "", "In", arr);
Pushed to the report filters:
report.on('loaded', event => {
report.getFilters()
.then(filters => {
filters.push(useFilter);
return report.setFilters(filters);
})
})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:
{
"$schema":"http://powerbi.com/product/schema#advanced",
"target":{
"table":"SiteInfo",
"column":"SiteId"
},
"logicalOperator":"And",
"conditions":[
{
"operator":"In",
"values":[
104,
102,
117
]
}
]
}I still get the error message in the developer console on the web page: Uncaught (in promise): Array[4].
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"
What do you suggest?
- Coltsbro9 years agoFrequent Visitor
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!
- Eric_Zhang9 years ago
Microsoft Employee
You're always welcome. :smileylol: