Power BI M 查询有条件地连接行
Power BI M Query to conditionally concatenate rows
我正在使用 Power BI Desktop,我遇到了一些 'structured' 数据相当糟糕的场景,我需要一些帮助来清理这些数据。数据如下所示:
Address | 123 Whatever Street <br/>
| Some Suburb| Some State <br/>
| Postcode <br/>
| Country <br/>
Company | Company Name Goes Here <br/>
Details | Freeform text goes here <br/>
| and more freeform text here <br/>
依此类推,直到数据集结束。
期望的结果是连接以“|”开头的行(管道)到没有“|”开头的前一行的末尾符号。
例如,上面的输出如下所示:
Address | 123 Whatever Street | Some Suburb| Some State | Postcode | Country <br/>
Company | Company Name Goes Here <br/>
Details | Freeform text goes here | and more freeform text here <br/>
所有数据都在一个列中。
除了知道“|”的条件之外,我不太确定如何处理这个问题符号表示数据属于前面的行。我考虑过创建一个 'group identifier' 就像一个计数器,只有在没有“|”时才会递增在一行的开头,但似乎无法正常工作。
我需要能够使用 M Query 在查询编辑器中将数据操作作为一个或一组步骤来执行。
右键单击您的数据列 > 替换值 > 将“”[space] 替换为空
添加索引列。添加列 > 索引列 > 从 1 或从 0 都没有关系。
Select 索引列 > 右键单击 > 替换值 > 将 888 替换为 999。这将在公式栏中生成此代码:
= Table.ReplaceValue(#"Added Index",888,999,Replacer.ReplaceValue,{"Index"})
用以下代码替换该代码:
= Table.ReplaceValue(#"Added Index",each [Index],each if Text.StartsWith([data],"|") then null else [Index],Replacer.ReplaceValue,{"Index"})
Select 索引列 > 右击 > 填充 > 向下
Select 索引列 > 右键单击 > 分组依据
New Column name = "Merged", Operation = Sum, Column = Column1 (data column header)
您应该有一个出错的新列。替换此公式:
= Table.Group(#"Filled Down", {"Index"}, {{"Merged", each List.Sum([data]), type nullable text}})
用这个公式:
= Table.Group(#"Filled Down", {"Index"}, {{"Merged", each Text.Combine([data],"|"), type nullable text}})
Select 合并列 > 右键单击 > 替换值 > 替换 ||与 |
Select 合并列 > 右键单击 > 拆分列 > 按分隔符 > 使用 |分隔符并确保选择“每次出现”。
删除索引列
Select 所有列 > 合并列 > 使用 |分隔符。
这不是 M 查询,它是 Powershell 脚本。
我从这里改编了一些代码:https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx
((Get-Content -path C:\SourceFile.txt -Raw) -replace ' </br>`r`n| ','|') | Set-Content -Path C:\OutputFile.txt
这应该会根据需要修复您的文件,然后您可以更轻松地导入 M
我说过那个文件格式很笨吗? :D 我想某种 HTML 导出是源系统唯一的选择。
我正在使用 Power BI Desktop,我遇到了一些 'structured' 数据相当糟糕的场景,我需要一些帮助来清理这些数据。数据如下所示:
Address | 123 Whatever Street <br/>
| Some Suburb| Some State <br/>
| Postcode <br/>
| Country <br/>
Company | Company Name Goes Here <br/>
Details | Freeform text goes here <br/>
| and more freeform text here <br/>
依此类推,直到数据集结束。
期望的结果是连接以“|”开头的行(管道)到没有“|”开头的前一行的末尾符号。
例如,上面的输出如下所示:
Address | 123 Whatever Street | Some Suburb| Some State | Postcode | Country <br/>
Company | Company Name Goes Here <br/>
Details | Freeform text goes here | and more freeform text here <br/>
所有数据都在一个列中。
除了知道“|”的条件之外,我不太确定如何处理这个问题符号表示数据属于前面的行。我考虑过创建一个 'group identifier' 就像一个计数器,只有在没有“|”时才会递增在一行的开头,但似乎无法正常工作。
我需要能够使用 M Query 在查询编辑器中将数据操作作为一个或一组步骤来执行。
右键单击您的数据列 > 替换值 > 将“”[space] 替换为空
添加索引列。添加列 > 索引列 > 从 1 或从 0 都没有关系。
Select 索引列 > 右键单击 > 替换值 > 将 888 替换为 999。这将在公式栏中生成此代码:
= Table.ReplaceValue(#"Added Index",888,999,Replacer.ReplaceValue,{"Index"})
用以下代码替换该代码:
= Table.ReplaceValue(#"Added Index",each [Index],each if Text.StartsWith([data],"|") then null else [Index],Replacer.ReplaceValue,{"Index"})
Select 索引列 > 右击 > 填充 > 向下
Select 索引列 > 右键单击 > 分组依据
New Column name = "Merged", Operation = Sum, Column = Column1 (data column header)
您应该有一个出错的新列。替换此公式:
= Table.Group(#"Filled Down", {"Index"}, {{"Merged", each List.Sum([data]), type nullable text}})
用这个公式:
= Table.Group(#"Filled Down", {"Index"}, {{"Merged", each Text.Combine([data],"|"), type nullable text}})
Select 合并列 > 右键单击 > 替换值 > 替换 ||与 |
Select 合并列 > 右键单击 > 拆分列 > 按分隔符 > 使用 |分隔符并确保选择“每次出现”。
删除索引列
Select 所有列 > 合并列 > 使用 |分隔符。
这不是 M 查询,它是 Powershell 脚本。
我从这里改编了一些代码:https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx
((Get-Content -path C:\SourceFile.txt -Raw) -replace ' </br>`r`n| ','|') | Set-Content -Path C:\OutputFile.txt
这应该会根据需要修复您的文件,然后您可以更轻松地导入 M
我说过那个文件格式很笨吗? :D 我想某种 HTML 导出是源系统唯一的选择。