以编程方式将参数传递给 Power BI 筛选器 [英] Passing parameters to Power BI filter programmatically
问题描述
在我的应用程序中,我正在显示 Power BI 报告.它已经可以工作了,因此通过其 ID (guid) 显示任何报告都没有问题.
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?
更具体地说,我将报告嵌入 HTML 元素中.我将 iframe URL 设置为从报告定义的
embedUrl
(从 REST API 接收)接收的 URL.我通过调用 postMessage()
的 JavaScript 代码来控制它.
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()
.
报告定义:
{
"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 代码:
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.
有两种可能的方法可以将参数(从而设置过滤器)从外部源传递到 Power BI 报表.
There are two possible ways to pass parameters (thus set filter) to the Power BI report from external source.
a) 在 Power BI 应用程序中
您可以通过在报告 URL(在浏览器地址栏中)设置 filter
参数来指定过滤器.参数采用自定义过滤器查询:
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'
其中12345678-6418-4b47-ac7c-f8ac7791a0a7"是报表id,Store"是数据集,PostalCode是要过滤的参数.eq"是一个相等运算符.
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 应该被编码,所以最终 url 看起来像这样:
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 参数
JavaScript(浏览器客户端)通过带有参数的 postMessage()
调用控制加载的 BI 报告(就像上面的问题一样).有一个额外的选项 oDataFilter
可以用来过滤报告.
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.
像这样设置:oDataFilter: "Store/PostalCode eq '15012'"
完整代码如下所示:
function onFrameLoaded() {
var m = {
action: "loadReport",
reportId: reportId,
accessToken: accessToken,
oDataFilter: "Store/PostalCode eq '15012'"
};
iframe.contentWindow.postMessage(JSON.stringify(m), "*");
}
备注
- 过滤器参数(数据源或参数名称)中不得有任何点,因为 Power BI 代码将其作为无效名称默默地拒绝;
这篇关于以编程方式将参数传递给 Power BI 筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!