以编程方式将参数传递给Power BI过滤器 [英] Passing parameters to Power BI filter programmatically

查看:300
本文介绍了以编程方式将参数传递给Power BI过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在显示Power BI报告。它已经工作,所以没有任何问题,显示任何报告的ID(guid)。

但是有一些报告需要参数化,例如,当前年或查看报告的人。 这是我的问题:如何做到这一点?

更具体地说,我将报告嵌入到HTML < iframe> 元素。我将iframe URL设置为从报表定义的 embedUrl (从REST API接收)接收的URL。我正在通过调用 postMessage()的JavaScript代码来控制它。

报告定义:

  {
id:12345678-6418-4b47-ac7c-f8ac7791a0aa,
name:Retail分析样本,
webUrl:https://app.powerbi.com/reports/12345678-6418-4b47-ac7c-f8ac7791a0aa,
embedUrl:https:// app .powerbi.com / reportEmbed?reportId = 12345678-6418-4b47-ac7c-f8ac7791a0aa



加载报表的JavaScript代码:

 函数onFrameLoaded(){
var m = {
action:loadReport,
reportId:reportId,
accessToken:accessToken
};

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





$ b现在我提供一个来自我的自定义应用程序的参数来过滤报表。有没有办法发送或传递一个值来过滤报表中的数据集?

解决方案

首先,过滤器必须是定义在报告中,所以用户可以手动设置。



有两种可能的方式将参数(因此设置过滤器)从外部源传递到Power BI报告。 / b>

在Power BI应用程序中

您可以通过设置 filter 参数(在浏览器地址栏中)。参数需要自定义过滤器查询:

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



其中12345678-6418-4b47-ac7c-f8ac7791a0a7是一个报告ID ,Store是一个数据集,PostalCode是一个要过滤掉的参数。 eq是一个等号运算符。



URL应该被编码,所以最终的url看起来像这样:

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

JavaScript(浏览器客户端)通过带有参数的 postMessage()调用来控制加载的BI报告(就像上面的问题一样)。有一个额外的选项 oDataFilter ,可以设置过滤报表。



像这样设置:<$完整的代码如下所示:

完整的代码如下所示:

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

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

备注


  • 过滤器参数(数据源或参数名称)中不得有任何点,因为Power BI代码会以无效名称拒绝 / li>

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?

解决方案

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;

这篇关于以编程方式将参数传递给Power BI过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆