使用SWITCH和SELECTEDVALUE进行DAX,用于基于切片器选择的输出 [英] DAX using SWITCH and SELECTEDVALUE for an output based on a slicer selection

查看:755
本文介绍了使用SWITCH和SELECTEDVALUE进行DAX,用于基于切片器选择的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列显示订单的老化小时数( AgingHours)。我想创建另一个名为 Aged的列,如果 AgingHours值大于切片器中的选定值,则该列为1,否则为0。用户可以选择 1 Hr, 2 Hrs,或来自切片器的 3小时。

I have a column that shows number of hours aging ("AgingHours") for "Orders". I want to create another column called "Aged" that will = 1 if the "AgingHours" value is greater than the selected value in the slicer, otherwise 0. The user is able to select either "1 Hr", "2 Hrs", or "3 Hrs" from the slicer.

例如:

创建了称为AgingDate [HourDay]的切片器。用户从切片器中选择 2小时(这意味着 AgingHours列中大于等于2的任何内容都会在 Aged列中输出1。下面的aged列中的输出为:

Created slicer called AgingDate[HourDay]. User selects "2 Hrs" from the slicer (which would mean, anything in the "AgingHours" column that is >= 2 would output a 1 in the "Aged" column. The output in the aged column below would be:

Order AgingHours Aged
A          1       0
B          2       1

我尝试根据选择使用IF语句创建计算列,但每个值都显示0。

I tried making a calculated column utilizing an IF statement based on the selection, but every value is showing 0.

Aged = 
SWITCH (
    SELECTEDVALUE( AgingDate[HourDay] ),
    "1 Hr", IF ( [AgingHours] >= 1, 1, 0 ),
    "2 Hrs", IF ( [AgingHours] >= 2, 1, 0 ),
    "3 Hrs", IF ( [AgingHours] >= 3, 1, 0 ),
     0
    )


推荐答案

计算的列仅在加载数据时才评估一次,因此,它们不能响应报表器上的任何切片器或过滤器。

Calculated columns are only evaluated once when the data is loaded. Therefore, they cannot respond to any slicers or filtering on your report page.

特别是 SELECTEDVALUE(AgingDate [HourDay])返回一个空白值(因为它不知道您选择了哪个单一值),这意味着您的 SWITCH 正在使用 else 0

In particular, SELECTEDVALUE( AgingDate[HourDay] ) is returning a blank value (since it doesn't know what single value you have selected), which means your SWITCH is taking the else value of 0.

如果您使用相同的代码作为度量,则看起来应该像只要在引用 [AgingHours] 时也使用表名,就可以正常工作。

If you use the same code as a measure, it looks like it should work OK as long as you use the table name as well when you refer to [AgingHours].

这篇关于使用SWITCH和SELECTEDVALUE进行DAX,用于基于切片器选择的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆