Skip to content

Using a Query as a Pivot Table Data Source

This page shows how a query can serve as the data source for a PivotTable, and why that is worth doing.

A PivotTable fed by a query, rather than by a worksheet range, can make a workbook far smaller, and it can still be used when the source data is out of reach.

The example starts with a source workbook, though the source could be anything Power Query can connect to. One worksheet holds 1 million rows of random sales transactions, each with a category, price, cost and margin:

A worksheet with columns Category, Price, Cost and Margin, with categories such as Sports, SUVs, Saloons and Estate

The workbook has no formulas, only data, as if it had been exported from a sales system. It is about 27 MB:

File Explorer showing the source workbook at 27,331 KB

  • Create a new, blank workbook, and use Power Query to connect it to the source workbook.
  • In the Power Query editor, click Close & Load To, and choose Only Create Connection.

This sets up the connection to the source workbook without bringing any of its data into the new one.

The Import Data dialog with Only Create Connection selected and Add this data to the Data Model unticked

Save the new workbook. It is only 13 KB, which confirms that it holds a connection and no data.

File Explorer showing the new PQ Workbook at 13 KB beside the 27,331 KB source workbook

Next, build a PivotTable on the query’s connection.

  • On the Insert tab, click PivotTable.
  • In the Create PivotTable dialog, select Use an external data source, then click Choose Connection.

The Create PivotTable dialog with Use an external data source selected and the Choose Connection button below it

The Existing Connections dialog lists the query created earlier.

  • Select the query and click Open.

The Existing Connections dialog listing Query - Sheet1, a connection to the Sheet1 query in the workbook

  • Click OK to add the PivotTable, and lay it out as needed. In this example it sums price, cost and margin by category:

A PivotTable with categories Estate, Saloons, Sports and SUVs as rows, and Sum of Price, Sum of Cost and Sum of Margin as values

Saved with the PivotTable, the workbook grows to 536 KB, which is still far smaller than the source.

File Explorer showing PQ Workbook at 536 KB beside the 27,331 KB source workbook

The PivotTable stores its data in the pivot cache, which is compressed, and only the summary appears on the worksheet. That keeps the file small, and here the new workbook is about 51 times smaller than the original.

To prove that the new workbook does not read from the source file, move or delete the source file.

The PivotTable still works fully, without access to the source data, because it works from its pivot cache.

The one thing it cannot do, as you would expect, is refresh.

This example follows Matt Allington’s post Traditional Pivot Tables using Power Query – Can Do on Excelerator BI.