PowerBI:移除文本字段中的特殊字符及后续数字
Hey there! Let's figure out how to remove that underscore _ and any trailing numbers after it from your text values in Power BI. I've got two easy methods for you—pick whichever fits your workflow better:
If you want to handle this directly in the report view with a calculated column, use a combination of LEFT(), FIND(), and IFERROR() functions to cover both cases (values with _ and those without).
Here's the formula you can paste into the calculated column editor:
Output Col = IFERROR( LEFT('Table1'[Input Col], FIND("_", 'Table1'[Input Col]) - 1), 'Table1'[Input Col] )
简单拆解下逻辑:
FIND("_", 'Table1'[Input Col])定位文本中第一个下划线的位置LEFT(..., position - 1)提取下划线之前的所有字符IFERROR(..., original value)做容错处理:如果文本里没有下划线(比如"asd"),直接返回原内容,避免报错
如果你更习惯在数据源层面清洗数据,Power Query有个超直观的函数可以搞定:Text.BeforeDelimiter()。
跟着步骤来:
- 打开Power Query编辑器(点击主页选项卡的转换数据)
- 选中你的
Table1和Input Col列 - 点击添加列 > 自定义列
- 在公式栏粘贴这段代码:
Text.BeforeDelimiter([Input Col], "_")
- 把新列重命名为"Output Col",关闭编辑器即可
这个函数会自动提取第一个下划线之前的内容,如果没有下划线就直接返回原文本——完全适配你的示例场景!
用你的测试数据验证下:
输入值:Abc_1、asd、fgh-abd_5、g_7
输出结果:Abc、asd、fgh-abd、g
两种方法都能得到你想要的结果,选适合你项目的方式就好!
内容的提问来源于stack exchange,提问作者Student of the Digital World

