简体   繁体   English

Power BI中的动态过滤

[英]Dynamic Filtering in Power BI

I have embedded a power bi report into webpage. 我已经将power bi报告嵌入到网页中。 I have a table named 'Query1' in report where I have StudentId column whose datatype is text. 我在报表中有一个名为“ Query1”的表,其中有数据类型为text的StudentId列。 I want to render the report on webpage by passing a specific StudentId into it. 我想通过将特定的StudentId传递给网页来呈现报告。

For this, I have tried the below code:- 为此,我尝试了以下代码:

@model PowerBIEmbedded_AppOwnsData.Models.EmbedConfig

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="~/scripts/powerbi.js"></script>

@if (!string.IsNullOrEmpty(Model.ErrorMessage))
{
    <div id="errorWrapper">
        <h2>
            Error
        </h2>
        @Model.ErrorMessage
    </div>

    return;
}

<div id="reportContainer"></div>

@Html.Partial("LiveDemoLink")

<script>
    var accessToken = "@Model.EmbedToken.Token";

    // Read embed URL from Model
    var embedUrl = "@Html.Raw(Model.EmbedUrl)";

    var embedReportId = "@Model.Id";

    var models = window['powerbi-client'].models;

    const filter = {
        $schema: "http://powerbi.com/product/schema#basic",
        target: {
            table: "Query1",
            column: "StudentId"
        },
        operator: "In",
        values: ["1524"]
    };
    var config = {
        type: 'report',
        tokenType: models.TokenType.Embed,
        accessToken: accessToken,
        embedUrl: embedUrl,
        id: embedReportId,
        permissions: models.Permissions.All,
        settings: {
            filterPaneEnabled: true,
            navContentPaneEnabled: true
        }
    };

    var reportContainer = $('#reportContainer')[0];

    var report = powerbi.embed(reportContainer, config);

    if (report) {
        report.setFilters([filter])
            .then(function (result) {
                console.log(result);
            })
            .catch(function (errors) {
                console.log(errors);
            });
    }
</script>

But, dynamic filtering is not working out, I have set StudentId in Report Filters also. 但是,动态过滤无法解决,我也在“报表过滤器”中设置了StudentId。

I tried changing its datatype to whole number but it is not working. 我尝试将其数据类型更改为整数,但无法正常工作。 What to do in this case? 在这种情况下该怎么办?

  1. You can pass the filter in the load config itself to avoid the setFilters() later. 您可以在加载配置本身中传递过滤器,以避免以后再使用setFilters()。
  2. if you're not sure the filter format is correct, I can advise a quick way to take care of this: 如果您不确定过滤器格式是否正确,我可以建议您采取一种快速的方法来解决此问题:

    • Set a filter you'd like with the filter pane (as in Power BI itself). 在过滤器窗格中设置所需的过滤器(如Power BI本身)。
    • Call report.getFilters() and see how the JSON returned is different from yours. 调用report.getFilters()并查看返回的JSON与您的JSON有何不同。
    • All's left to do is modify the ID as you'd like. 剩下要做的就是根据需要修改ID。

Your code looks good but you missed the step: like report.off("loaded"). 您的代码看起来不错,但是您错过了这一步骤:类似于report.off(“ loaded”)。 Below is typescript code you can change it to js : 以下是打字稿代码,您可以将其更改为js:

report.off("loaded");
report.on("loaded", e =>{
            console.log("loaded");
            this.report.setFilters(filters)
            .then(filters=>{
                console.log("filters:" + filters);
            });
            this.report.off("error");
        } );

Happy Coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM