PowerBI中基于关键词筛选文本列并生成新表的DAX技术求助
解决PowerBI中基于多关键词筛选文本列生成新表的问题
嘿,作为实习生刚接触PowerBI和DAX确实会遇到这类筛选的问题,我来帮你搞定这个需求!你之前尝试的条件列更适合在原表做标记,但要生成独立的新表,我们需要用DAX的表函数来直接筛选符合条件的行。
方法一:直接多条件筛选(关键词较少时)
这种写法直观,适合当前3个关键词的场景:
Filtered_PPT_Table = CALCULATETABLE( Main_File, -- 检查ShortDescription列是否含任一关键词 OR( SEARCH("PowerPoint", Main_File[ShortDescription],,0) > 0, SEARCH("power point", Main_File[ShortDescription],,0) > 0, SEARCH("Power-Point", Main_File[ShortDescription],,0) > 0 ) -- 逻辑或:检查LongDescription列 || OR( SEARCH("PowerPoint", Main_File[LongDescription],,0) > 0, SEARCH("power point", Main_File[LongDescription],,0) > 0, SEARCH("Power-Point", Main_File[LongDescription],,0) > 0 ) -- 逻辑或:检查ProblemSolution列 || OR( SEARCH("PowerPoint", Main_File[ProblemSolution],,0) > 0, SEARCH("power point", Main_File[ProblemSolution],,0) > 0, SEARCH("Power-Point", Main_File[ProblemSolution],,0) > 0 ) )
方法二:变量存储关键词(灵活易扩展)
如果以后需要新增关键词,这种写法只需修改变量即可,更高效:
Filtered_PPT_Table = -- 定义关键词数组 VAR KeywordList = {"PowerPoint", "power point", "Power-Point"} RETURN FILTER( Main_File, -- 遍历每个关键词,检查是否在三列中任意一列出现 MAXX( KeywordList, IF( SEARCH([Value], Main_File[ShortDescription],,0) > 0 || SEARCH([Value], Main_File[LongDescription],,0) > 0 || SEARCH([Value], Main_File[ProblemSolution],,0) > 0, 1, -- 匹配到返回1 0 -- 未匹配返回0 ) ) > 0 -- 只要有一个关键词匹配就保留该行 )
关键细节说明:
SEARCH函数:不区分大小写,第四个参数设置为0表示找不到关键词时返回0,所以>0就代表找到了匹配内容。如果需要严格区分大小写,可以换成FIND函数。- 操作步骤:打开PowerBI的建模选项卡,点击「新建表」,粘贴上面的DAX公式,命名新表(比如
Filtered_PPT_Table)即可。
内容的提问来源于stack exchange,提问作者Ethel
相关产品推荐
相关产品推荐

