title: PowerQuery how to insert null values for missing fields using Table.FromRecords
body: <p>I am creating a custom connector for PowerBI using the PowerQuery SDK, it's purpose will be to read data from a JSON over HTTP API and transform it to a table view.</p>
  
  <p>I am able to read the JSON response, the response is an array of JSON objects. I am using this PowerQuery function to read this result into a table:</p>
  
  <pre><code>    responseJson = Get30DaysHistory(),
      #"TestTable" = Table.FromRecords(responseJson)
  in
     #"TestTable";
  </code></pre>
  
  <p>This works as long as all objects contain the same fields, but if the fields in a record differ than an error is thrown. I am trying to understand how to apply this function to my above code, I just want missing fields to have null values added:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/powerquery-m/missingfield-usenull" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/powerquery-m/missingfield-usenull</a></p>
  
  <p>here is a sample of the JSON output, for added context the field in question that is throwing an error with this data sample is "pageId" as it is present in the first record but not the second:</p>
  
  <pre><code>[
  {
      "type": "page",
      "uri": "://test/url": {
          "name": "Chrome",
          "version": "69.0.3497",
          "os": "Windows"
      },
      "appId": -323232,
      "pageId": "nVTd9XqSEy1mPhaWGNlW9GWw5gU",
      "ts": 1538768760000,
      "lastTs": 1538768809248,
      "duration": 49248
  },
  {
      "type": "feature",
      "uri": "://test/url",
      "parsedUserAgent": {
          "name": "Chrome",
          "version": "69.0.3497",
          "os": "Windows"
      },
      "appId": -323232,
      "featureId": "diOobYRaY56e191qW2a7_pXGIzk",
      "ts": 1538768802811
  },
  {
      "type": "feature",
      "uri": "://test/url",
      "parsedUserAgent": {
          "name": "Chrome",
          "version": "69.0.3497",
          "os": "Windows"
      },
      "appId": -323232,
      "featureId": "_Net3QEcQ2mmg4z9wV95Gqkvx0k",
      "ts": 1538768803809
  },
  {
      "type": "page",
      "uri": "://test/url",
      "parsedUserAgent": {
          "name": "Chrome",
          "version": "69.0.3497",
          "os": "Windows"
      },
      "appId": -323232,
      "pageId": "cQIxuputqop_FYvfM9ehhd2CUvI",
      "ts": 1538768809248,
      "lastTs": 1538768840412,
      "duration": 31164
  }
  </code></pre>
  
  <p>]</p>
  
  <p>Updated with solution:</p>
  
  <pre><code>shared Testamun.Contents = (optional message as text) =&gt;
  let
      responseJson = Get30DaysHistory("testval"),
      headers =
       let
          allHeaders = List.Combine(List.Transform(responseJson, Record.FieldNames)),
          uniqueHeaders = List.Distinct(allHeaders)
       in
          uniqueHeaders,
          testTable = Table.FromRecords(responseJson, headers, MissingField.UseNull)
   in 
      testTable;
  </code></pre>