Power BI中使用CASE/IF语句统计白人与有色人种占比
用IF/CASE编写DAX实现种族分类统计
核心需求实现(两类分类)
按照你单独统计White or European占比、其余种族归为People of color的需求,分别用IF和CASE语句实现如下:
1. IF语句版本
Race_Ethnicity_Category = IF( 'Contact'[race_and_ethnicity] = "White or European", "White or European", "People of color" )
2. CASE语句版本(DAX 2023及以上版本支持)
Race_Ethnicity_Category = CASE WHEN 'Contact'[race_and_ethnicity] = "White or European" THEN "White or European" ELSE "People of color" END
原SWITCH代码的错误说明
你之前的SWITCH代码报错,问题出在最后一行的正则匹配逻辑:DAX中不能直接用=匹配正则表达式".*(;).*"。如果需要识别含分号的混合种族并单独归类,可改用CONTAINSSTRING函数判断字符串是否包含分号,修正后的SWITCH示例如下(若需保留部分细分类别):
Race_Ethnicity = SWITCH(TRUE(), 'Contact'[race_and_ethnicity] = "White or European", "White or European", CONTAINSSTRING('Contact'[race_and_ethnicity], ";"), "Mixed Race or Ethnicity", "People of color" )
内容的提问来源于stack exchange,提问作者NgLyn
相关产品推荐
相关产品推荐

