Power Query M为导入的json中的每个嵌套记录添加行

时间:2019-11-26 18:21:13

标签: excel powerquery m

我有一些json数据,我想使用强力查询在excel中导入,但是没有运气:

json看起来像这样:

{
  "User1@g.com": {
    "2019-03-14T15:23:03": {
      "Message": "A message (1)",
      "Timestamp": "2019-03-14T15:23:03.5042800+01:00"
    },
    "2019-03-15T14:38:34": {
      "Message": "A message (2)",
      "Timestamp": "2019-03-15T14:38:34.3995690+01:00"
    }
  },
  "user2@g.com": {
    "2018-11-02T17:02:19": {
      "Message": "A message (3)",
      "Timestamp": "2018-11-02T17:02:19.8146010+01:00"
    },
    "2019-04-03T09:31:53": {
      "Message": "A message (4)",
      "Timestamp": "2019-04-03T09:31:53.7216810+02:00"
    },
    "2019-04-03T09:43:46": {
      "Message": "A message (5)",
      "Timestamp": "2019-04-03T09:43:46.2697460+02:00"
    }
  },
  "user3@g.com": {
    "2018-11-10T11:03:52": {
      "Message": "A message (6)",
      "Timestamp": "2018-11-10T11:03:52.4458880+01:00"
    },
    "2019-02-27T17:08:02": {
      "Message": "A message (7)",
      "Timestamp": "2019-02-27T17:08:02.4327310+01:00"
    }
  }
}

我希望输出看起来像下表:

User            message         TimeStamp

User1@g.com     A message (1)   2019-03-15T14:38:34.3995690+01:00
User1@g.com     A message (2)   2019-03-15T14:38:34.3995690+01:00
user2@g.com     A message (3)   2018-11-02T17:02:19.8146010+01:00
user2@g.com     A message (4)   2019-04-03T09:31:53.7216810+02:00
user2@g.com     A message (5)   2019-04-03T09:43:46.2697460+02:00
user3@g.com     A message (6)   2018-11-10T11:03:52.4458880+01:00
user3@g.com     A message (7)   2019-02-27T17:08:02.4327310+01:00

导入json时,我的用户电子邮件列+包含记录的列

initial

钻取一条记录显示

enter image description here

深入到该记录中最终会显示消息

enter image description here

我已经尝试了很多事情,似乎可以选择联接,但是需要一个表,而不是记录。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

在展开前只需转换“值”列:

let
    Source = Json.Document("{
  ""User1@g.com"": {
    ""2019-03-14T15:23:03"": {
      ""Message"": ""A message (1)"",
      ""Timestamp"": ""2019-03-14T15:23:03.5042800+01:00""
    },
    ""2019-03-15T14:38:34"": {
      ""Message"": ""A message (2)"",
      ""Timestamp"": ""2019-03-15T14:38:34.3995690+01:00""
    }
  },
  ""user2@g.com"": {
    ""2018-11-02T17:02:19"": {
      ""Message"": ""A message (3)"",
      ""Timestamp"": ""2018-11-02T17:02:19.8146010+01:00""
    },
    ""2019-04-03T09:31:53"": {
      ""Message"": ""A message (4)"",
      ""Timestamp"": ""2019-04-03T09:31:53.7216810+02:00""
    },
    ""2019-04-03T09:43:46"": {
      ""Message"": ""A message (5)"",
      ""Timestamp"": ""2019-04-03T09:43:46.2697460+02:00""
    }
  },
  ""user3@g.com"": {
    ""2018-11-10T11:03:52"": {
      ""Message"": ""A message (6)"",
      ""Timestamp"": ""2018-11-10T11:03:52.4458880+01:00""
    },
    ""2019-02-27T17:08:02"": {
      ""Message"": ""A message (7)"",
      ""Timestamp"": ""2019-02-27T17:08:02.4327310+01:00""
    }
  }
}
"),
    toTable = Record.ToTable(Source),
    transform = Table.TransformColumns(toTable, {"Value", each Table.FromRecords(Record.FieldValues(_))}),
    expand = Table.ExpandTableColumn(transform, "Value", {"Message", "Timestamp"})
in
    expand

enter image description here