Power Query 将列表中的记录转换为列

Power Query Convert Records in List to Columns

我有一个 JSON 文档的列表,我正在尝试将其转换为 Power Query 中的行。我正在努力,因为我需要的值是记录,在列表中,在记录列中。任何开始接近我需要的东西都会变得非常复杂

单个记录如下所示:

{
    "key_as_string": "2020-02-25T23:00:00.000Z",
    "key": 1582671600000,
    "doc_count": 1086187,
    "attack_types": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "attack-sqli",
                "doc_count": 380989
            },
            {
                "key": "attack-protocol",
                "doc_count": 8195
            },
            {
                "key": "attack-xss",
                "doc_count": 1216
            },
            {
                "key": "attack-rce",
                "doc_count": 258
            },
            {
                "key": "attack-disclosure",
                "doc_count": 157
            },
            {
                "key": "attack-lfi",
                "doc_count": 24
            },
            {
                "key": "attack-generic",
                "doc_count": 17
            },
            {
                "key": "attack-rfi",
                "doc_count": 2
            }
        ]
    }
}

我正试图把它变成这样: 为了清楚起见,此处显示的第二行只是第二条记录的示例。

非常感谢任何帮助!

我从这个 JSON 开始,保存在一个名为 test.json 的文件中,以确保我能看到两个记录是如何工作的:

    [{
    "key_as_string": "2020-02-25T23:00:00.000Z",
    "key": 1582671600000,
    "doc_count": 1086187,
    "attack_types": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "attack-sqli",
                "doc_count": 380989
            },
            {
                "key": "attack-protocol",
                "doc_count": 8195
            },
            {
                "key": "attack-xss",
                "doc_count": 1216
            },
            {
                "key": "attack-rce",
                "doc_count": 258
            },
            {
                "key": "attack-disclosure",
                "doc_count": 157
            },
            {
                "key": "attack-lfi",
                "doc_count": 24
            },
            {
                "key": "attack-generic",
                "doc_count": 17
            },
            {
                "key": "attack-rfi",
                "doc_count": 2
            }
        ]
    }
},
{
    "key_as_string": "2020-02-25T22:00:00.000Z",
    "key": 158267000000,
    "doc_count": 1086186,
    "attack_types": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "attack-sqli",
                "doc_count": 384419
            },
            {
                "key": "attack-protocol",
                "doc_count": 2046
            },
            {
                "key": "attack-xss",
                "doc_count": 1504
            },
            {
                "key": "attack-rce",
                "doc_count": 198
            },
            {
                "key": "attack-disclosure",
                "doc_count": 120
            },
            {
                "key": "attack-lfi",
                "doc_count": 16
            },
            {
                "key": "attack-generic",
                "doc_count": 200
            },
            {
                "key": "attack-rfi",
                "doc_count": 2
            }
        ]
    }
}]

我使用 GUI 导出了这个 M 代码,它似乎有效:

let
    Source = Json.Document(File.Contents("MYFILEPATH\test.json")),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"key_as_string", "key", "doc_count", "attack_types"}, {"key_as_string", "key", "doc_count", "attack_types"}),
    #"Expanded attack_types" = Table.ExpandRecordColumn(#"Expanded Column1", "attack_types", {"doc_count_error_upper_bound", "sum_other_doc_count", "buckets"}, {"doc_count_error_upper_bound", "sum_other_doc_count", "buckets"}),
    #"Expanded buckets" = Table.ExpandListColumn(#"Expanded attack_types", "buckets"),
    #"Expanded buckets1" = Table.ExpandRecordColumn(#"Expanded buckets", "buckets", {"key", "doc_count"}, {"key.1", "doc_count.1"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded buckets1", List.Distinct(#"Expanded buckets1"[key.1]), "key.1", "doc_count.1"),
    #"Removed Other Columns" = Table.SelectColumns(#"Pivoted Column",{"key", "attack-sqli", "attack-protocol", "attack-xss", "attack-rce", "attack-disclosure", "attack-lfi", "attack-generic", "attack-rfi"}),
    #"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"key", Order.Descending}})
in
    #"Sorted Rows"

只需将上述 M 代码剪切并粘贴到您的高级编辑器中即可。 将 MYFILEPATH 替换为您的文件路径。

我得到了这个结果: