通过在 URL 中添加查询字符串参数来筛选报表Filter a report using query string parameters in the URL

在 Power BI 服务中打开报表时,报表的每一页都有自己的专属 URL。When you open a report in Power BI service, each page of the report has its own unique URL. 若要筛选报表页,可以使用报表画布上的“筛选器”窗格。To filter that report page, you could use the Filters pane on the report canvas. 也可以向 URL 添加查询字符串参数来筛选报表。Or you could add query string parameters to the URL to filter the report. 你可能有一个要向同事展示的报表,你希望为同事预筛选报表。Perhaps you have a report you'd like to show colleagues and you want to pre-filter it for them. 方法之一是,从报表的默认 URL 入手,向 URL 添加筛选参数,然后通过电子邮件向同事发送完整的 URL。One way to do this is to start with the default URL for the report, add the filter parameters to the URL, and then email them the entire URL.

用于筛选的查询字符串参数语法Query string parameter syntax for filtering

语法相当简单;从报表 URL 入手,然后依次添加问号和筛选语法。The syntax is fairly straightforward; start with the report URL, add a question mark, and then add your filter syntax.

URL?filter=/字段 eq ''URL?filter=Table/Field eq 'value'

  • 字段名称区分大小写,不区分大小写。Table and Field names are case sensitive, value is not.
  • 报表视图中隐藏的字段仍可供筛选。Fields that are hidden from report view can still be filtered.
  • 必须用单引号将括起来。Value has to be enclosed with single quotes.
  • 字段类型必须为数字或字符串Field type has to be a number or string
  • 表和字段名称中不能有任何空格。Table and field names cannot have any spaces.

如果仍感到困惑,请继续阅读,我们将分部分讲解。If it's still confusing, continue reading and we'll break it down.

筛选一个字段Filter on a field

假设我们的报表 URL 如下。Let’s assume that the URL to our report is the following.

从上文中的地图可视化效果可以看出,我们在北卡罗来纳州有商店。And we see in our map visualization (above) that we have stores in North Carolina.

备注

本示例以“零售分析”示例为依据“This example is based on the Retail Analysis sample.

若要从报表中筛选出“NC”(北卡罗来纳州)商店的数据,请在 URL 后面追加以下内容:To filter the report to show data only for stores in "NC" (North Carolina), append the URL with the following;

?filter=Store/Territory eq 'NC'?filter=Store/Territory eq 'NC'

备注

NC 是“Store”表的“Territory”字段中存储的值。NC is a value stored in the Territory field of the Store table.

我们的报表针对北卡罗来纳州进行了筛选;报表页上的所有可视化效果都只显示北卡罗来纳州的数据。Our report is filtered for North Carolina; all the visualizations on the report page show data for only North Carolina.

筛选多个字段Filter on multiple fields

还可以通过将其他参数添加到 URL 来筛选多个字段。You can also filter on multiple fields by adding additional parameters to your URL. 让我们回到最初的筛选器参数。Let's go back to our original filter parameter.

?filter=Store/Territory eq 'NC'

要对其他字段进行筛选,请添加 and 以及与上述格式相同的另一个字段。To filter on additional fields, add an and and another field in the same format as above. 示例如下。Here is an example.

?filter=Store/Territory eq 'NC' and Store/Chain eq 'Fashions Direct'

使用 DAX 来对多个值进行筛选Using DAX to filter on multiple values

对多个字段进行筛选的另一方法是创建将两个字段合并成一个值的计算列。Another way to filter on multiple fields is by creating a calculated column that concatenates two fields to a single value. 然后,便可以筛选此值。Then you can filter on that value.

例如,我们有以下两个字段:“Territory”和“Chain”。For example, we have two fields: Territory and Chain. 在 Power BI Desktop 中,新建一个计算列(字段),并将其命名为“TerritoryChain”。In Power BI Desktop, create a new Calculated column (Field) called TerritoryChain. 请注意,字段名称中不能有任何空格。Remember that the Field name cannot have any spaces. 下面是此计算列的 DAX 公式。Here is the DAX formula for that column.

TerritoryChain = [Territory] & " - " & [Chain]TerritoryChain = [Territory] & " - " & [Chain]

将报表发布到 Power BI 服务,然后使用 URL 查询字符串筛选出 NC 中 Lindseys 商店的数据。Publish the report to Power BI service and then use the URL query string to filter to display data for only Lindseys stores in NC.

https://app.powerbi.com/groups/me/reports/8d6e300b-696f-498e-b611-41ae03366851/ReportSection3?filter=Store/TerritoryChain eq 'NC–Lindseys'https://app.powerbi.com/groups/me/reports/8d6e300b-696f-498e-b611-41ae03366851/ReportSection3?filter=Store/TerritoryChain eq 'NC–Lindseys'

将筛选后的报表中的可视化效果固定到磁贴中Pin a tile from a filtered report

使用查询字符串参数筛选报表后,便可以将此报表中的可视化效果固定到仪表板中。Once you've filtered the report using query string parameters, you can pin visualizations from that report to your dashboard. 仪表板上的磁贴会显示筛选出的数据,选择相应的仪表板磁贴会打开用于创建磁贴的报表。The tile on the dashboard will display the filtered data and selecting that dashboard tile will open the report that was used to create it. 不过,使用 URL 执行的筛选结果未与报表一起保存,选择仪表板磁贴后打开的报表处于未经筛选的状态。However, the filtering you did using the URL is not saved with the report and when the dashboard tile is selected, the report opens in its unfiltered state. 也就是说,仪表板磁贴中显示的数据与报表可视化效果中显示的数据不一致。This means that the data displayed in the dashboard tile will not match the data displayed in the report visualization.

在某些情况下,可能希望查看不同的结果(仪表板上显示筛选后的数据,报表中显示未经筛选的数据),那么这就会非常有帮助。There may be some cases where this will be helpful when you'd like to see different results; filtered on the dashboard and unfiltered in the report.

限制和疑难解答Limitations and troubleshooting

使用查询字符串参数时,需要注意两点。There are a couple of things to be aware of when using the query string parameters.

  • 查询字符串筛选不适用于发布到 Web 或 Power BI Embedded。Query string filtering does not work with Publish to web or Power BI Embedded.
  • 字段类型必须为数字或字符串。Field type has to be number or string.
  • 表和字段名称中不能有任何空格。Table and field names cannot have any spaces.

后续步骤Next steps

将可视化效果固定到仪表板Pin a visualization to a dashboard
试用一下 - 完全免费!Try it out -- it's free!

更多问题?More questions? 尝试咨询 Power BI 社区Try asking the Power BI Community