如何在 Power BI 中使用 Dax 显示日期间隔之间的自定义值

How to display custom values between date intervals using Dax in Power BI

我正在尝试在 DAX (power Bi) 中使用以下公式显示不同日期之间的自定义值

= if ([RegistrationDate] >= "6/14/2018" & [RegistrationDate] <= "7/15/2018") then "M4" else if ([RegistrationDate] >= "7/16/2018" & [RegistrationDate] <= "8/26/2018") then "M5" else if [RegistrationDate] >= "8/27/2018" then "M6" else ""

基本上其背后的逻辑是:

使用上面的公式给我一个错误。任何想法为什么?

你想要的看起来像这样:

Custom Column =
IF (
    'table'[RegistrationDate] >= DATE ( 2018, 6, 14 )
        && 'table'[RegistrationDate] < DATE ( 2018, 7, 15 ),
    "M4",
    IF (
        'table'[RegistrationDate] >= DATE ( 2018, 7, 16 )
            && 'table'[RegistrationDate] <= DATE ( 2018, 8, 26 ),
        "M5",
        IF ( 'table'[RegistrationDate] >= DATE ( 2018, 8, 27 ), "M6", "" )
    )
)

您需要在 DAX 中为逻辑 AND 使用双符号 &&,并且不要使用单词 ifthenelse if就像您使用 M 查询编辑器语言一样。

Marco 的方法看起来不错,但如果你有很多 else if 情况,SWITCH 可能比嵌套的 IF 函数更好。

Custom Column =
SWITCH ( TRUE(),
    'table'[RegistrationDate] >= DATE ( 2018, 6, 14 ) &&
    'table'[RegistrationDate] < DATE ( 2018, 7, 15 ),
    "M4",
    'table'[RegistrationDate] >= DATE ( 2018, 7, 16 ) &&
    'table'[RegistrationDate] <= DATE ( 2018, 8, 26 ),
    "M5",
    'table'[RegistrationDate] >= DATE ( 2018, 8, 27 ),
    "M6",
    "" 
)

Here's a reference SWITCH TRUE