You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将多表关联SQL中的Case When语句转换为Power BI DAX

Translating SQL CASE WHEN to DAX for Power BI

Got it, let's convert that SQL conditional logic into clean, readable DAX. DAX's SWITCH function is perfect here—it mirrors the sequential conditional checking of SQL's CASE WHEN far better than nested IF statements.

First, here's the direct translation tailored to your logic:

Status Category = 
SWITCH(
    TRUE(),
    -- First condition: A.status is ACTIVATED and m.caller is not null
    'A'[status] = "ACTIVATED" && NOT(ISBLANK('m'[caller])), "ACTIVE",
    -- Second condition: A.status is ACTIVATED, m.caller is null, n.callingparty is not null
    'A'[status] = "ACTIVATED" && ISBLANK('m'[caller]) && NOT(ISBLANK('n'[callingparty])), "INACTIVE",
    -- Third condition: A.status is ACTIVATED, both m.caller and n.callingparty are null
    'A'[status] = "ACTIVATED" && ISBLANK('m'[caller]) && ISBLANK('n'[callingparty]), "NO_USAGE",
    -- Optional default value if none of the conditions match (like SQL's ELSE)
    BLANK()
)

Key Notes to Match Your SQL Logic:

  • In DAX, we use ISBLANK() to check for null values (equivalent to SQL's IS NULL). To check "not null", wrap it in NOT()—so NOT(ISBLANK('m'[caller])) equals m.caller IS NOT NULL in SQL.
  • The SWITCH(TRUE(), ...) pattern lets us evaluate each condition in order, just like your CASE WHEN—it returns the result of the first condition that evaluates to TRUE.
  • I assumed you had a typo with AINACTIVE in your SQL and used INACTIVE instead; if that's intentional, just swap the string back to "AINACTIVE".
  • Make sure the table names ('A', 'm', 'n') match exactly what's in your Power BI data model—use single quotes around table names if they have spaces or special characters.

This works great as a calculated column if you're applying this logic at the row level (matching how your SQL would run across joined rows). If you need this as a measure instead, you'd wrap the conditions in aggregation functions (like MAX() or SELECTEDVALUE()) depending on your context, but the core conditional logic stays the same.

内容的提问来源于stack exchange,提问作者user14854377

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:06:52