29

In my application I'm displaying a Power BI report. It already works, so there's no problems with showing any report by its ID (guid).

But there are some reports that need to be parametrized, for instance, with current year or person who views the report. That's my question: how to do it?

To be more specific, I'm embedding the report inside HTML <iframe> element. I set iframe URL to an URL received from report definition's embedUrl (received from REST API). I'm controlling it by JavaScript code that calls postMessage().

Report definition:

{
  "id":"12345678-6418-4b47-ac7c-f8ac7791a0aa",
  "name":"Retail Analysis Sample",
  "webUrl":"https://app.powerbi.com/reports/12345678-6418-4b47-ac7c-f8ac7791a0aa",
  "embedUrl":"https://app.powerbi.com/reportEmbed?reportId=12345678-6418-4b47-ac7c-f8ac7791a0aa"
}

JavaScript code to loads the report:

function onFrameLoaded() {
    var m = {
        action: "loadReport",
        reportId: reportId,
        accessToken: accessToken
    };

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

Now I feed to filter the report by a parameter from my custom application. Is there a way to send or pass a value to filter dataset in the report?

Leonard
  • 2,558
  • 2
  • 15
  • 29
andrew.fox
  • 7,435
  • 5
  • 52
  • 75
  • Possible duplicate of [Power BI Embed URL-multiple filters](http://stackoverflow.com/questions/37943885/power-bi-embed-url-multiple-filters) – Matt Mazzola Sep 13 '16 at 19:41
  • 4
    How it could be a duplicate of a question asked LATER and with different concern? – andrew.fox Sep 19 '16 at 19:12

2 Answers2

36

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;
andrew.fox
  • 7,435
  • 5
  • 52
  • 75
  • It is possible to use this approach to filter data based on a UserID? For example lets say I have 50 users with different permissions on the data they can see. If I am using DirectConnect to SQL server for example, could I use the parameter approach to only pulldown / show data that the user can see? – Markive Apr 27 '16 at 04:53
  • 2
    @Markive - not in a safe way; you could just pass UserId as a filter, but user can clear it. With this requirement it's possible that Power BI is not a way to go for you. In Power BI you have to publish different report to each user account (that use different queries). – andrew.fox Apr 27 '16 at 06:18
  • 4
    Is there a way to specify multiple parameters with URL method? I've tried ?filter=Store/PostalCode eq '15012'&?filter=Store/State eq 'IL' and also ?filter=Store/PostalCode eq '15012' and Store/State eq 'IL' – Travis Jul 09 '16 at 15:28
  • I didn't try combinations. But these filter queries are used by Power BI itself in URLs. Just try to set these filters manually and generate an URL. – andrew.fox Jul 11 '16 at 19:47
  • 1
    just to let you know that this won't work if you publish the report with the value you will be filtering already set in the filter. you have to publish the pbix with no filters set. – freethinker6 Aug 02 '16 at 20:52
  • I building embed url programatically, url is encoded, but with encoded symbols filter isn't applied. Without encoded it is fine... Why is that? – demo Feb 11 '21 at 14:58
  • @demo - it is a Power BI hidden API, I wouldn't expect it to work ideally. Maybe encoding is not necessary at all – andrew.fox Feb 12 '21 at 08:00
  • @andrew.fox ok, I was using UriBuilder to add `filter` to embedUrl, and it encodes filter values in way that wasn't readable by PowerBi. So you need to do something like `Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString()))` or not encoding it at all. – demo Feb 12 '21 at 09:55
10

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

Silko
  • 584
  • 1
  • 8
  • 26
  • Quick question,Is there a limit for the number of values that can be passed for a report filter to Power BI using the API? – Vishnu Suresh May 06 '20 at 11:46
  • I am not sure about that. Would have to do some research. But I would say that if there is a limit it is because of the length of the data that can be sent to the API or maybe because of some javascript limitation. But the number should be big. – Silko May 07 '20 at 07:14