Skip to content

Self-Referencing Tables

This page shows what a self-referencing table is, and when it is worth building one into a workbook.

Data imported from a live source often needs comments of your own added to it. Adding them in the source file is not always practical, for example:

  • when each refresh brings in a fresh copy of the source file, such as an export from the company’s ERP system, so comments typed into the old copy would be lost
  • when several files are combined, and it is easier to annotate one cleaned, summarised table than every source file

A table loaded from Power Query can have an extra column typed beside it. The trouble is that the comments in that column are not tied to the rows of data.

In the example below, a query loads the source debtors table, on the left, into the Power Query table on the right.

Two tables side by side, a source debtors table with ID, Client, Invoice, Balance Outstanding and Due Date columns, and an identical table loaded by Power Query

A Comments column is then typed into the Power Query table by hand:

The same two tables, with a Comments column added by hand to the Power Query table, holding notes such as Called twice and Paying next week

If the source table is then put in a different order and the query is refreshed, the comments no longer line up with the data.

Below, the source table has been sorted alphabetically by client. After the refresh, the rows of the Power Query table follow the new order, but the typed comments stay where they were, so Called twice now sits against a different client from the one it was written for.

Both tables after sorting the source by client and refreshing, with the comments in their original positions and now beside the wrong clients

This happens because the typed column only sits beside the table, and is not part of the rows that the query produces.

The fix is a self-referencing table. The Power Query table, comments and all, is read back into Power Query, and the comments are merged into the original query by ID. The comments then become part of each row, and move with it.

1. Read the commented table back into Power Query

Section titled “1. Read the commented table back into Power Query”
  • Click any cell in the Power Query table, the one with the comments, and on Excel’s Data tab click From Table/Range.

The table read back into the Power Query editor, with ID, Client, Invoice, Balance Outstanding, Due Date and Comments columns

  • Name the query PQ Table with Comments, and Close & Load it as a connection only. The workbook now has two queries: Source Table, which loads the source data, and PQ Table with Comments, which reads the commented table.

The Queries & Connections pane listing Source Table with 5 rows loaded and PQ Table with Comments as connection only

2. Merge the comments into the original query

Section titled “2. Merge the comments into the original query”
  • Edit the Source Table query.
  • Click Merge Queries, and choose PQ Table with Comments as the table to merge with.
  • Select the ID column in both tables, and keep the Left Outer join.

The Merge dialog joining Source Table to PQ Table with Comments on ID with a Left Outer join, matching 5 of 5 rows

A new column appears, with a Table object in each row:

The Source Table query with a new PQ Table with Comments column holding a Table in each row

  • Click the expand icon on the new column, tick only Comments, and leave the prefix empty.

The Expand dialog with only Comments ticked and the column name prefix left blank

The query now carries the comments in its last column:

The Source Table query with a Comments column at the end, each comment matched to its row by ID

3. Delete the comments column typed earlier

Section titled “3. Delete the comments column typed earlier”
  • Close & Load the query.
  • The worksheet table now has two comment columns, one loaded by the query and the other typed by hand earlier.

The Power Query table with two comment columns, Comments loaded by the query and Comments2 typed by hand, holding the same notes

  • Delete the hand-typed column, which is Comments2 in the picture. The table looks the same as before, but the Comments column is now part of each row of the query, not just placed beside the table.

The Power Query table with a single Comments column

Sort the source table into a different order, such as reverse alphabetical order, and refresh the query. Each comment stays with the row it belongs to, because the merge matches them on the ID column, which holds a unique value for each row.

The source table sorted by client in reverse order, and the refreshed Power Query table with each comment still beside the right client

New rows added to the source table, or changes to existing ones, come through on the next refresh, and every comment typed so far stays with its row. A new row arrives with a blank comment, ready to be filled in.

The source table with three new rows, IDs 6 to 8, and the refreshed Power Query table showing them with empty comments while the earlier comments stay in place

The technique on this page was described by Matt Allington’s post Self Referencing Tables in Power Query on Excelerator BI.