使用 DAX 或查询编辑器替换或替换 - Power BI

Replace or substitute using DAX or Query Editor - Power BI

我有一个要求,我必须循环遍历文本字段中的每个字符

例如:

Input Expected Output
a#123456; 12341 123456;12341
a123456 12341bd 123456;12341
a2017d 12341ds 12341
a123456/12341bd 123456;12341
n/a null

基本上我在这里清理我的数据:

  1. 我将删除所有非数字字符。
  2. 只有至少有 5 个连续的数字才有效。
  3. 在输入中区分 ID 的 space 或“;”或 ',' 或 '/' 可能已被使用。在某些情况下会出现多个 ID,而不限于两个。
  4. 为了保持不变,我会将所有这些连接器替换为“;”

注意:- 我的步骤可能不正确,但我的预期输出是我希望从数据中获得的结果。

我们如何使用 Power BI 中的 DAX 或查询编辑器执行此操作?

我将分两步进行尝试。

首先,让我们删除所有非数字或分隔符的内容。我们可以创建一个自定义列来使用此公式执行此操作(假设您的起始列名为 Raw):

Text.Remove([Raw], List.Transform({33..43,60..255}, each Character.FromNumber(_)))

这使用了 Windows-1252 character numbering, but you could just write a big list of characters to remove using the Text.Remove 函数。

此自定义列现在如下所示:

Custom
123456; 12341
123456 12341
2017  12341
123456/12341
/

从这里开始,计划是使用每个分隔符拆分它并过滤掉太短的字符串。

Text.SplitAny函数允许我们指定多个分隔符。例如,公式 Text.SplitAny("1;23 4.5-6/ 7:8,9", " ,-./:;") 将 return {"1","23","4","5","6","","7","8","9"}.

一旦我们有了一个子字符串列表,我们就可以用 List.Select and then concatenate them from a list of substrings into a single string using Text.Combine.

过滤我们不想要的那些

将所有这些放在一起,我们有这个公式

Text.Combine(List.Select(Text.SplitAny([Custom], " ,-./:;"), each Text.Length(_) > 4), ";")

如果您愿意,可以将其全部粉碎:

= Table.AddColumn(#"Previous Step", "Clean",
      each Text.Combine(
               List.Select(
                   Text.SplitAny(
                       Text.Remove(
                           [Raw],
                           List.Transform(
                               {33..43,60..255},
                               each Character.FromNumber(_)
                           )
                       ),
                       " ,-./:;"
                   ),
                   each Text.Length(_) > 4),
               ";"
           )
       )