简体   繁体   English

Dax 和 Power BI,默认和切片器

[英]Dax and Power BI, default and slicer

I am trying to set up a report for users which by default shows today´s data.我正在尝试为默认显示今天数据的用户设置报告。

But I would also like people with whom the report is share to be able to pick data, from the seventies to year 2043.但我也希望与报告共享的人能够选择从 70 年代到 2043 年的数据。

I currently managed to set up a report filter in DAX:我目前设法在 DAX 中设置了一个报告过滤器:

Date_Today_Default = 
var currentrowdate = FORMAT('Date'[DATE], "dd/mm/yyyy")
var DateToday = FORMAT(TODAY(),"dd/mm/yyyy")
return
IF(DateToday = currentrowdate, "YES", "NO")

But I would like to give users the possibility to see default values when the report opens and slice the report as well.但我想让用户在报表打开时查看默认值并切片报表。 In this case, the users cannot change the date if "YES" is selected in my filter.在这种情况下,如果在我的过滤器中选择“是”,则用户无法更改日期。

Is this doable in Power BI?这在 Power BI 中可行吗?

You can leverage the ISFILTERED function to give you behavior like this.您可以利用 ISFILTERED function 为您提供这样的行为。

Start by creating a new table that you will use for the values in your slicer首先创建一个新表,用于切片器中的值

SlicerDate = SELECTCOLUMNS('Date', "Date",[Date])

And then go into your model view and create a relationship between your SlicerDate table and your Date table on the Date field.然后 go 进入您的 model 视图,并在日期字段上创建 SlicerDate 表和 Date 表之间的关系。

Now make the following small changes to your dax.现在对您的 dax 进行以下小的更改。

Date_Today_Default = 
var currentrowdate = FORMAT(Max('Date'[Date]), "dd/mm/yyyy")
var DateToday = FORMAT(TODAY(),"dd/mm/yyyy")
return
IF (isFiltered(SlicerDate[Date]), "YES", IF (DateToday = currentrowdate, "YES", "NO"))

Create a slicer visual and bring the date field from the SlicerDatetable into its values.创建切片器视觉对象并将 SlicerDatetable 中的日期字段带入其值。

When you choose a date in the slicer, you will cause 'Date_Today_Default' to automatically show 'yes' for all records, letting all of them through the filter you have on your visual.当您在切片器中选择日期时,您将导致“Date_Today_Default”自动为所有记录显示“是”,让所有记录通过您在视觉对象上的过滤器。 But then they will be filtered down by the date you just selected in the slicer.但随后它们将被您刚刚在切片器中选择的日期过滤掉。 So that gives your users control over the date selected.这样您的用户就可以控制所选日期。

When no date is chosen in the slicer, the 'Date_Today_Default' logic will fall into the IF DateToday = CurrentRow statement and only mark one row 'YES.'当切片器中没有选择日期时,“Date_Today_Default”逻辑将落入 IF DateToday = CurrentRow 语句中,并且仅将一行标记为“是”。 The filter you have on your visual will omit all the other records.您对视觉对象的过滤器将忽略所有其他记录。

在此处输入图像描述

Hope it helps希望能帮助到你

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

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