Passing parameters to Power BI filter programmatically

柔情痞子 提交于 2019-11-27 17:32:17

First of all, filter has to be defined in the report, so user can set it manually.

There are two possible ways to pass parameters (thus set filter) to the Power BI report from external source.

a) In Power BI Application

You can specify the filter by setting filter parameter in the report URL (in browser address bar). Parameter takes custom filter query:

https://app.powerbi.com/groups/me/reports/12345678-6418-4b47-ac7c-f8ac7791a0a7?filter=Store/PostalCode eq '15012'

where "12345678-6418-4b47-ac7c-f8ac7791a0a7" is a report id, and "Store" is a dataset, and PostalCode is a parameter to be filter out. "eq" is a equality operator.

URL should be encoded, so final url looks like this:

https://app.powerbi.com/groups/me/reports/12345678-6418-4b47-ac7c-f8ac7791a0a7?filter=Store/PostalCode%20eq%20%2715012%27

b) JavaScript sendMessage oDataFilter parameter

JavaScript (browser client side) controls the loaded BI report by postMessage() calls with parameters (just like in the question above). There is an extra option oDataFilter that can be set to filter the report.

Set it like this: oDataFilter: "Store/PostalCode eq '15012'"

Full code would look like this:

function onFrameLoaded() {
    var m = {
        action: "loadReport",
        reportId: reportId,
        accessToken: accessToken,
        oDataFilter: "Store/PostalCode eq '15012'"
    };

    iframe.contentWindow.postMessage(JSON.stringify(m), "*");
}

Remarks

  • There must not be any dots in the filter parameters (datasource or parameter name) as the Power BI code rejects it silently as invalid names;

Microsoft created a powerbi-client with which you can do a lot more than just apply one filter. You can apply as many filters as you want and you can also choose default page, default filters, hide filter pane, hide pages navigation, etc.

You can find the client here: https://microsoft.github.io/PowerBI-JavaScript/

Here is a demo application: https://microsoft.github.io/PowerBI-JavaScript/demo/index.html

Here is the documentation: https://github.com/Microsoft/PowerBI-JavaScript/wiki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!