将json二维数组转换为表格

时间:2019-03-14 09:02:32

标签: json powerquery

我很难找到如何将2D数组(通过JSON格式获取)转换为表的方法...

我的JSON:

[
  ["index", 1, 2, 3, 4, 5, 6, 7, 8, 9],
  ["type", "years", "years", "years", "quarters", "quarters", "quarters", "months", "months", "months"],
  ["period", "2016", "2017", "2018", "Q2", "Q3", "Q4", "Dec", "Jan", "Feb"],
  ["nb_month", 12, 12, 12, 3, 3, 3, 1, 1, 1],
  ["MY_scrap", 0, 54, 1529, 325, 445, 532, 120, 193, 131],
  ["MY_completed", 6, 89, 2895, 394, 722, 1622, 437, 805, 542],
  ["SV_total", 6, 1806, 36520, 6143, 11772, 15318, 3579, 6407, 5216],
  ["SV_scrap", 0, 54, 1529, 325, 445, 532, 120, 193, 131],
  ["SV_other", 6, 1752, 34809, 5808, 11198, 14773, 3449, 6214, 5081],
  ["WIP", 0, 971, 3214, 1493, 2395, 3214, 3214, 3832, 4445],
  ["WLS", 0, 178, 650, 390, 475, 650, 650, 709, 902]
]

JSON的每一行是表中的一列,JSON中的第一个“行”是表头。

我开始研究电源查询,最后获得了代码的开头:

let
    Source = Json.Document(Web.Contents("http://localhost/api/test")),
    trans1 = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Transposed Table" = Table.Transpose(trans1)
in
    #"Transposed Table"

结果在这里: enter image description here

但是从这里我不了解如何管理适当的列表扩展...

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我相信,有一个M函数对您非常有用。 Table.FromColumns(以列表形式列出)

let
    Source = Json.Document(Web.Contents("http://localhost/api/test")),
    TableResult = Table.FromColumns(Source),
    #"Promoted Headers" = Table.PromoteHeaders(TableResult, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"index", Int64.Type}, {"type", type text}, {"period", type text}, {"nb_month", Int64.Type}, {"MY_scrap", Int64.Type}, {"MY_completed", Int64.Type}, {"SV_total", Int64.Type}, {"SV_scrap", Int64.Type}, {"SV_other", Int64.Type}, {"WIP", Int64.Type}, {"WLS", Int64.Type}})
in
    #"Changed Type"

我认为这正是您想要的。

enter image description here