DAX中使用CONCATENATEX处理层级筛选结果时如何去重?
解决DAX中使用DISTINCT后CONCATENATEX找不到列的问题
问题原因
当你在CONCATENATEX的表参数中使用DISTINCT时,如果仅传入单列(比如DISTINCT(dim_legal_entity_hierarchy[legal_entity_id])),返回的表只包含该单列,自然无法找到legal_entity_name列,导致公式报错。若对整个表使用DISTINCT仍报错,通常是因为层级表的重复行并非完全重复,需要明确按实体的唯一标识组合分组去重。
解决方案
要实现无重复的实体列表展示,推荐以下两种可行的修正方案:
方案1:使用SUMMARIZE生成唯一实体组合
SUMMARIZE会根据指定的列分组,自动去除重复的实体ID和名称组合,确保每个实体仅出现一次,是通用性更强的方案:
SelectedFilters_Entity = VAR _Entity_ = IF ( ISFILTERED ( dim_legal_entity_hierarchy ), "Legal Entity: " & CONCATENATEX ( SUMMARIZE ( dim_legal_entity_hierarchy, dim_legal_entity_hierarchy[legal_entity_id], dim_legal_entity_hierarchy[legal_entity_name] ), [legal_entity_id] & "-" & [legal_entity_name], ", " ), BLANK () ) RETURN IF ( ISBLANK ( _Entity_ ), "Legal Entity: No Filter Applied", _Entity_ )
方案2:使用DISTINCT对多列去重(适用于表存在完全重复行的场景)
如果层级表中存在完全重复的实体行(即ID和名称完全一致的重复记录),可以直接对包含所需列的整表使用DISTINCT:
SelectedFilters_Entity = VAR _Entity_ = IF ( ISFILTERED ( dim_legal_entity_hierarchy ), "Legal Entity: " & CONCATENATEX ( DISTINCT ( dim_legal_entity_hierarchy ), [legal_entity_id] & "-" & [legal_entity_name], ", " ), BLANK () ) RETURN IF ( ISBLANK ( _Entity_ ), "Legal Entity: No Filter Applied", _Entity_ )
关键说明
- 优先选择
SUMMARIZE方案,无论层级表的重复是完全重复还是同一实体出现在不同层级行中,都能确保实体唯一。 - 禁止仅对单列使用
DISTINCT,否则会丢失拼接所需的其他列,导致CONCATENATEX无法找到对应字段。
内容的提问来源于stack exchange,提问作者Nelson Gomes Matias
相关产品推荐
相关产品推荐

