当列不包含在列表中时,创建 SQL Case 等效的 PowerBI Dax 自定义列

问题描述 投票:0回答:1

在 PowerBI 中创建自定义列时,如何重构下面的 SQL case-when 语句,类似于多子句 if-then-else 语句或 switch 语句。这还包含 AND、OR 并检查字符串是否存在于某个列表中(在本例中为货币名称)

Case
    When Account = 2 and Currency in ('EUR','GBP') then 'Europe'
    When Account = 3 and Currency NOT in ('USD','JPY') then 'Emerging Market'
    When Account = 4 then 'Account 4'
    When code like '123%' then 1
    When code like '%222' then 2
    Else Country_Name
    End as my_custom_column
powerbi dax
1个回答
0
投票

创建计算列:

my_custom_column = 
SWITCH(
    TRUE(),
    Account = 2 && (Currency = "EUR" || Currency = "GBP"), "Europe",
    Account = 3 && NOT(Currency = "USD" || Currency = "JPY"), "Emerging Market",
    Account = 4, "Account 4",
    LEFT(code, 3) = "123", "1",  //  'like '123%'' can be traslated to starts with '123'
    RIGHT(code, 3) = "222", "2", // Assuming 'like '%222'' can be traslated to starts with ends with '222'
    Country_Name
)

了解有关 DAX 中SWITCH的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.