DAX using SWITCH and SELECTEDVALUE for an output based on a slicer selection

浪子不回头ぞ 提交于 2019-12-11 13:15:22

问题


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.

For example:

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

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
    )

回答1:


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

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.

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].



来源:https://stackoverflow.com/questions/53268765/dax-using-switch-and-selectedvalue-for-an-output-based-on-a-slicer-selection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!