在 Power BI 中创建相对日期切片器和筛选器Creating a relative date slicer and filter in Power BI
09/09/2020
本文内容
适用对象:
面向使用者的 Power BI 服务
面向设计者和开发者的 Power BI 服务
Power BI Desktop
需要 Pro 或 Premium 许可证APPLIES TO:
Power BI service for consumers
Power BI service for designers & developers
Power BI Desktop
Requires Pro or Premium license
借助相对日期切片器 或筛选器 ,可以向数据模型中的任意日期列应用时间筛选器。With the relative date slicer or relative date filter, you can apply time-based filters to any date column in your data model. 例如,可使用相对日期切片器,仅显示过去 30 天(或月、日历月等)的销售数据 。For example, you can use the relative date slicer to show only sales data that's happened within the last 30 days (or month, calendar months, and so on). 刷新数据时,相对时间段会自动应用相应的相对日期约束。When you refresh the data, the relative time period automatically applies the appropriate relative date constraint.
要与 Power BI 同事共享报表,你和这位同事都应具有独立的 Power BI Pro 许可证,并且应将报表保存在 Premium 容量中。To share your report with a Power BI colleague requires that you both have individual Power BI Pro licenses or that the report is saved in Premium capacity.
创建相对日期范围切片器Create the relative date range slicer
相对日期切片器的使用方法与其他任何切片器的使用方法一样。You can use the relative date slicer just like any other slicer. 为报表创建“切片器”视觉对象,再选择日期值作为“字段”值 。Create a slicer visual for your report and then select a date value for the Field value. 在下图中,选择的是“订购日期”字段 。In the following image, we selected the OrderDate field.
选择画布上的切片器,然后选择切片器视觉对象右上角的脱字号。Select the slicer on your canvas and then the carat in the upper-right corner of the slicer visual. 如果视觉对象具有日期数据,则菜单将显示“相对”的选项 。If the visual has date data, the menu displays the option for Relative.
若要使用相对日期切片器,请选择“相对” 。For the relative date slicer, select Relative.
然后,可以选择设置。You can then select the settings.
对于相对日期切片器中的第一个设置,可进行如下选择 :For the first setting in the relative date slicer, you have the following choices:
最后一项Last
下一步Next
本This
通过相对日期切片器中的第二个(中间)设置,可输入一个数字来定义相对日期范围 。The second (middle) setting in the relative date slicer lets you enter a number to define the relative date range.
可以通过第三个设置选择日期度量值。The third setting lets you pick the date measurement. 可以选择下列选项:You have the following choices:
日Days
周Weeks
周(日历)Weeks (Calendar)
月Months
月(日历)Months (Calendar)
年Years
年(日历)Years (Calendar)
如果在此该列表中选择“月”,并在中间设置中输入 2,则结果如下所示 :If you select Months from that list, and enter 2 in the middle setting, here's what happens:
如果今天是 7 月 20 日:If today is July 20:
由切片器约束的视觉对象中包含的数据将显示前两个月的数据,The data included in visuals constrained by the slicer will show data for the previous two months,
即 5 月 21 日到 7 月 20 日(今天的日期)。Starting on May 21 and going through July 20 (today's date).
相比之下,如果选择“月(日历)” ,约束的视觉对象会显示 5 月 1 日到 6 月 30 日(过去两个整日历月)的数据。In comparison, if you selected Months (Calendar), the visuals constrained would show data from May 1 through June 30 (the last two complete calendar months).
创建相对日期范围筛选器Create the relative date range filter
还可以为报表页或整个报表创建相对日期范围筛选器。You can also create a relative date range filter for your report page or your entire report. 为此,请将日期字段拖到“字段”窗格中的“页面级筛选器”或“报表级筛选器”框 :To do so, drag a date field into the Page level filters well or the Report level filters well in the Field pane:
可在此处更改相对日期范围。Once there, you can change the relative date range. 这与自定义相对日期切片器的方式类似 。It's similar to how you can customize the relative date slicer. 在“筛选器类型” 下拉列表中选择“相对日期筛选” 。Select Relative date filtering from the Filter Type drop-down.
选择“相对日期筛选”后,可修改 3 个部分(包括中间的数字框),就像切片器一样 。Once you've selected Relative date filtering, you see three sections to change, including a middle numeric box, just like the slicer.
限制和注意事项Limitations and considerations
目前,使用相对日期范围切片器和筛选器 时,需要遵循以下限制和注意事项。The following limitations and considerations currently apply to the relative date range slicer and filter.
切片器中字段的数据类型必须是日期,而不是默认的文本。The data type for the field in the slicer must be a date, and not the default of text. 否则,切片器中不会显示相对选项。Otherwise, the relative options don't show up in the slicer.
Power BI 中的数据模型不包含时区信息 。Data models in Power BI don't include time zone info. 模型可以存储时间,但并不指明所在时区。The models can store times, but there's no indication of the time zone they're in.
切片器和筛选器始终基于 UTC 的时间。The slicer and filter are always based on the time in UTC. 如果在报表中设置筛选器并将其发送给位于其他时区的同事,你们将看到相同的数据。If you set up a filter in a report and send it to a colleague in a different time zone, you both see the same data. 除非你处于 UTC 时区,否则你和你的同事都必须考虑将会遇到的时间偏移量。Unless you are in the UTC time zone, you and your colleague must account for the time offset you experience.
可使用查询编辑器将在本地时区捕获的数据转换为 UTC 。You can convert data captured in a local time zone to UTC using the Query Editor.
后续步骤Next steps