Self-Referencing Tables
This page shows what a self-referencing table is, and when it is worth building one into a workbook.
Adding comments to a debtors list
Section titled “Adding comments to a debtors list”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.

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

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.

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.

- 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.

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.

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

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

The query now carries the comments in its last column:

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.

- 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.

4. Test the self-referencing query
Section titled “4. Test the self-referencing query”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.

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 technique on this page was described by Matt Allington’s post Self Referencing Tables in Power Query on Excelerator BI.