【问题标题】:Parse JSON response list arrays as columns instead of rows using Power BI / Power Query / M Code使用 Power BI / Power Query / M 代码将 JSON 响应列表数组解析为列而不是行
【发布时间】:2021-10-01 07:23:52
【问题描述】:

我一直在尝试使用 Power BI / Power Query / M 代码转换 JSON 文件。

我想将我的示例 JSON 数据(如下提供)解析为如下表格格式:

到目前为止我的 Power BI / Power Query / M 代码

let
    Source = Json.Document(File.Contents("C:\demo.json")),
    #"Converted to Table" = Record.ToTable(Source),
    #"Expanded Value" = Table.ExpandListColumn(#"Converted to Table", "Value"),
    #"Expanded Value1" = Table.ExpandRecordColumn(#"Expanded Value", "Value", {"ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC", "Rows"}, {"ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC", "Rows"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Value1",{"Name", "ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC"}),
    #"Expanded Rows" = Table.ExpandListColumn(#"Removed Columns", "Rows"),
    #"Expanded Rows1" = Table.ExpandRecordColumn(#"Expanded Rows", "Rows", {"RowType", "Cells", "Title", "Rows"}, {"RowType", "Cells", "Title", "Rows.1"})
in
    #"Expanded Rows1"

我的代码达到了这样一个点:查询结果有一行用于标题内容,一行用于两个部分的内容。

我被困在如何将 JSON 数组列表扩展为列而不是行?具体来说:

  • ListHeaderCells
  • List 在两个SectionRows.1 列中

任何提示、提示、想法、指针?

示例 JSON 文件

{
  "Reports": [
    {
      "ReportID": "ProfitAndLoss",
      "ReportName": "Profit and Loss",
      "ReportType": "ProfitAndLoss",
      "ReportTitles": [
        "Profit & Loss",
        "Demo Company (AU)",
        "1 February 2018 to 28 February 2018"
      ],
      "ReportDate": "25 February 2018",
      "UpdatedDateUTC": "\/Date(1519593468971)\/",
      "Rows": [
        {
          "RowType": "Header",
          "Cells": [
            { "Value": "" },
            { "Value": "28 Feb 18" },
            { "Value": "28 Jan 18" }
          ]
        },
        {
          "RowType": "Section",
          "Title": " Income",
          "Rows": [
            {
              "RowType": "Row",
              "Cells": [
                {
                  "Value": "Sales",
                  "Attributes": [
                    {
                      "Value": "e2bacdc6-2006-43c2-a5da-3c0e5f43b452",
                      "Id": "account"
                    }
                  ]
                },{
                  "Value": "9220.05",
                  "Attributes": [
                    {
                      "Value": "e2bacdc6-2006-43c2-a5da-3c0e5f43b452",
                      "Id": "account"
                    }
                  ]
                },{
                  "Value": "5120.05",
                  "Attributes": [
                    {
                      "Value": "e2bacdc6-2006-43c2-a5da-3c0e5f43b452",
                      "Id": "account"
                    }
                  ]
                }
              ]
            },
            {
              "RowType": "SummaryRow",
              "Cells": [
                { "Value": "Total Income" },
                { "Value": "9220.05" },
                { "Value": "1250.09" }
              ]
            }
          ]
        },{
          "RowType": "Section",
          "Rows": [
            {
              "RowType": "Row",
              "Cells": [
                { "Value": "NET PROFIT" },
                { "Value": "-6250.09" },
                { "Value": "-7250.09" }
              ]
            }
          ]
        }
      ]
    }
  ]
}

【问题讨论】:

    标签: json powerbi powerquery


    【解决方案1】:

    可能有更好的方法,但这是我想出的:

    let
        Source = Json.Document(File.Contents("document.json")),
        #"Converted to Table" = Record.ToTable(Source),
        #"Expanded Value" = Table.ExpandListColumn(#"Converted to Table", "Value"),
        #"Expanded Value1" = Table.ExpandRecordColumn(#"Expanded Value", "Value", {"ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC", "Rows"}, {"ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC", "Rows"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Value1",{"Name", "ReportID", "ReportName", "ReportType", "ReportTitles", "ReportDate", "UpdatedDateUTC"}),
        #"Expanded Rows" = Table.ExpandListColumn(#"Removed Columns", "Rows"),
        #"Expanded Rows1" = Table.ExpandRecordColumn(#"Expanded Rows", "Rows", {"RowType", "Cells", "Title", "Rows"}, {"RowType", "Cells", "Title", "Rows.1"}),
        //Get the headers
        #"HeaderRaw" = #"Expanded Rows1"{0}[Cells],
        #"Converted to Table1" = Table.FromList(HeaderRaw, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"Value"}, {"Column1.Value"}),
        #"Added Index" = Table.AddIndexColumn(#"Expanded Column1", "Index", 1, 1, Int64.Type),
        #"Changed Type" = Table.TransformColumnTypes(#"Added Index",{{"Index", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","","blank",Replacer.ReplaceValue,{"Column1.Value"}),
        oldColumnNames = Table.Column(#"Replaced Value", "Index"),
        newColumnNames = Table.Column(#"Replaced Value", "Column1.Value"),
        #"RenameRecords" = List.Zip({oldColumnNames, newColumnNames}),
        //Get contents
        #"Removed Top Rows" = Table.Skip(#"Expanded Rows1",1),
        #"combine" = Table.SelectColumns(#"Removed Top Rows",{"RowType", "Title", "Rows.1"}),
        #"Expanded Rows.2" = Table.ExpandListColumn(combine, "Rows.1"),
        #"Expanded Rows.1" = Table.ExpandRecordColumn(#"Expanded Rows.2", "Rows.1", {"RowType", "Cells"}, {"Rows.1.RowType", "Rows.1.Cells"}),
        #"Expanded Rows.1.Cells" = Table.ExpandListColumn(#"Expanded Rows.1", "Rows.1.Cells"),
        #"Grouped Rows" = Table.Group(#"Expanded Rows.1.Cells", {"Title", "Rows.1.RowType"}, {{"Count", each _, type table [RowType=text, Title=nullable text, Rows.1.RowType=text, Rows.1.Cells=record]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Index", 1)),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"RowType", "Title", "Rows.1.RowType", "Rows.1.Cells", "Index"}, {"Custom.RowType", "Custom.Title", "Custom.Rows.1.RowType", "Custom.Rows.1.Cells", "Custom.Index"}),
        #"Expanded Custom.Rows.1.Cells" = Table.ExpandRecordColumn(#"Expanded Custom", "Custom.Rows.1.Cells", {"Value", "Attributes"}, {"Custom.Rows.1.Cells.Value", "Custom.Rows.1.Cells.Attributes"}),
        #"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom.Rows.1.Cells",{"Custom.Rows.1.Cells.Attributes"}),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns1", {{"Custom.Index", type text}}, "en-GB"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns1", {{"Custom.Index", type text}}, "en-GB")[Custom.Index]), "Custom.Index", "Custom.Rows.1.Cells.Value"),
        #"Final" = Table.RenameColumns(#"Pivoted Column", #"RenameRecords")
    in
        #"Final"
    
    1. 获取标题记录和值,添加索引并将其转换为“旧列名”和“新列名”(您将获得 1 - 空白,2 月 2 日 - 28 日,1 月 3 日 - 28 日)

    2. 获取剩余的记录并将它们全部展开,然后按类型分组并添加索引,这样您将得到类似:总收入 - 1, 9220.5 - 2, 1250.5 - 3

    3. 然后在索引列上旋转表格,使用单元格的值,这会将值放入列中,列将被称为 1,2 和 3

    4. 最后根据我们在步骤 1 中所做的重命名列

    【讨论】:

    • 这里的demo.json 数据工作正常!但是,当我尝试使用更大的 json 文件(包含 12 个月而不是两个月)以及更多部分时,在数据透视之后,Final 表并未全部显示。但是,它们确实会显示到Removed Columns1 步骤。所以枢轴以某种方式过滤。此外,虽然标题行有 13 个列,但有些行的可变数量最多为 100 个。因此,我在称为Row NumberExpanded Rows.1 步骤之后添加了新的自定义索引,并将其包含在Grouped Rows 中。这个固定的 col 问题,现在只有 13 个。但是,Final 仍然只显示少数实际行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多