PowerBI 解析分隔数据
Posted
技术标签:
【中文标题】PowerBI 解析分隔数据【英文标题】:PowerBI parse delimited data 【发布时间】:2020-11-02 13:02:12 【问题描述】:我有一个表,其中包含其他表的值。这些其他表格可以格式化为 CSV 或 JSON。
您能告诉我如何将这些数据导入 PowerBI 中的各个表吗?我已经尝试过使用 PowerQuery GUI,但到目前为止没有成功,也许需要使用高级编辑器中的代码。
我不能只在 PowerBI 之外解析这些数据,因为公司指导方针禁止使用脚本,所以一切都必须在 PowerBI 内完成 - 尽管允许使用 PowerQuery 代码。
-
csv:
"id,normdist,poissondist,binomial\r\n1,0.00013383,0.033689735,0.009765625\r\n2,0.004431848,0.084224337,0.043945313\r\n3,0.053990967,0.140373896,0.1171875\r\n4,0.241970725,0.17546737,0.205078125\r\n5,0.39894228,0.17546737,0.24609375\r\n6,0.241970725,0.146222808,0.205078125\r\n7,0.053990967,0.104444863,0.1171875\r\n8,0.004431848,0.065278039,0.043945313\r\n9,0.00013383,0.036265577,0.009765625\r\n10,1.49E-06,0.018132789,0.000976563\r\n"
json(按行)
["id":1,"normdist":0.0001,"poissondist":0.0337,"binomial":0.0098,"id":2,"normdist":0.0044,"poissondist":0.0842,"binomial":0.0439,"id":3,"normdist":0.054,"poissondist":0.1404,"binomial":0.1172,"id":4,"normdist":0.242,"poissondist":0.1755,"binomial":0.2051,"id":5,"normdist":0.3989,"poissondist":0.1755,"binomial":0.2461,"id":6,"normdist":0.242,"poissondist":0.1462,"binomial":0.2051,"id":7,"normdist":0.054,"poissondist":0.1044,"binomial":0.1172,"id":8,"normdist":0.0044,"poissondist":0.0653,"binomial":0.0439,"id":9,"normdist":0.0001,"poissondist":0.0363,"binomial":0.0098,"id":10,"normdist":1.49e-06,"poissondist":0.0181,"binomial":0.001]
【问题讨论】:
【参考方案1】:假设数据是 CSV 版本,但只是数据库中的一个字符串,因此它在查询编辑器中看起来像这样:
为了将其扩展为表格,我们需要将其拆分为行和列。 “主页”选项卡有一个拆分列工具,我们将使用下拉列表中的“按分隔符”选项像这样使用:
也就是说,我们使用“\r\n”将单元格拆分为多行。
现在我们的专栏是这样的:
删除所有空白行并再次使用拆分列工具。这一次,您可以保留默认值,因为它会自动猜测您要以逗号分隔并展开成行。
如果你提升标题并清理列类型,最终结果应该是
此示例的完整 M 查询,您可以将其粘贴到高级编辑器中:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("7ZFJbsMwDEXvkrUacBKHA/QUcRYpshHQ2EXc+6OS4mzkC3Th3Qf5+ThdLqdyT/PyfNzL+pt+lrKuy9z1V5mXR7l9T9NzmmZMcAYAZHZuklk9jHMPh2lWyi8n9ZAIo4s37UIkzNa0cEhm5Je1kzJHQGhLowAbe2jTaOi2MaUGSDAMjFpLtCxqHUmQwRzf3VuWw6P29MEoCsFvoo5EUaol4HukjVPW5URceZzSx801kzlw7DeP8ZxKmrPZ/pwICc8Snx/QRgZ05Ard6rtzQ56u6Xjm8czjmf/wmdc/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Split by \r\n into Rows" = Table.ExpandListColumn(Table.TransformColumns(Source, "Column1", Splitter.SplitTextByDelimiter("\r\n", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type itemType), "Column1"),
#"Filtered Blank Rows" = Table.SelectRows(#"Split by \r\n into Rows", each [Column1] <> null and [Column1] <> ""),
#"Split into Columns" = Table.SplitColumn(#"Filtered Blank Rows", "Column1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), "Column1.1", "Column1.2", "Column1.3", "Column1.4"),
#"Promoted Headers" = Table.PromoteHeaders(#"Split into Columns", [PromoteAllScalars=true]),
#"Filtered Repeated Headers" = Table.SelectRows(#"Promoted Headers", each ([id] <> "id")),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Repeated Headers","id", Int64.Type, "normdist", type number, "poissondist", type number, "binomial", type number)
in
#"Changed Type"
【讨论】:
csv 不在文件中,但在数据库中。此 csv 包含在数据库中的一个单元格中,因此我需要导入。 那么它实际上是一个 CSV 文件还是只是存储在数据库单元格中的文本? 存储在数据库单元格中的文本 在这种情况下,您需要使用拆分列(按分隔符)工具。首先按“\r\n”分成行,按“,”分成列。 (必须运行。我稍后会尝试更新。) 是的,这行得通。谢谢,请更新,我会接受。以上是关于PowerBI 解析分隔数据的主要内容,如果未能解决你的问题,请参考以下文章