Preserving Context
The problem
Section titled “The problem”Part of what a table means is often stored outside it. A file name might hold the month, a sheet name might hold the region, or a cell above the table might say which product category the rows belong to. When several such tables are combined, that information is lost unless it is brought into the data as a column, and the combined table then mixes rows whose origin can no longer be told apart.
The example here is a workbook, Products.xlsx, with one sheet per parent category. Each sheet names its category in a small table above the product list, and each sheet has a different number of unrelated rows at the top:

Because the number of rows above each table changes from sheet to sheet, the query cannot skip a fixed number of rows. Instead it finds two anchors on each sheet, which are the cell that reads Parent Category and the cell that reads Name, and works out everything else from where they are. The same approach works for a single sheet that holds several tables, each under its own title row.
Step 1: load the sheets
Section titled “Step 1: load the sheets”- On the Data tab, choose Get Data, then From File, then From Workbook, and select
Products.xlsx. - In the Navigator, select the workbook itself rather than one sheet, and choose Transform Data. The editor shows one row per sheet.

- Right-click the
Datacolumn and choose Remove Other Columns, which leaves one table per sheet.

Step 2: number the rows of each sheet
Section titled “Step 2: number the rows of each sheet”Each sheet’s rows need a position number, so that later steps can tell which rows come after the header. Click fx to add a step, and enter:
= Table.AddColumn(#"Removed Other Columns", "Table with Index", each Table.AddIndexColumn([Data], "Index", 0, 1))For each sheet, Table.AddIndexColumn returns a copy of its table with an extra Index column counting from 0. The copy goes in a new column called Table with Index, beside the original.

Step 3: find the context label
Section titled “Step 3: find the context label”Add another step:
= Table.AddColumn(#"Custom1", "Row Index of Parent Category", each List.PositionOf([Data][Column1], "Parent Category"))[Data][Column1] takes the first column of a sheet as a list, and List.PositionOf returns the position of the cell that reads Parent Category. Positions count from 0, so on the first sheet, where the label is on row 5 of the worksheet, the result is 4. The other sheets give different numbers, because each has a different number of rows above the label.

Step 4: read the context value
Section titled “Step 4: read the context value”The category name sits in the cell directly below the label, so add a step that reads it:
= Table.AddColumn(#"Added Custom1", "Parent Category", each [Data]{[Row Index of Parent Category] + 1}[Column1])[Data]{n} picks row n of the sheet’s table, and [Column1] then takes the value in its first column. Adding 1 to the label’s position moves down one row to the value.

The step that this formula refers to must be the previous step in your own query. In the screenshot, that step is called Custom2, so check the name in Applied Steps and change #"Added Custom1" to match it if it differs.
Step 5: find where the product table starts
Section titled “Step 5: find where the product table starts”The product table’s header row is the one whose first cell reads Name. Add a step that finds it in the same way:
= Table.AddColumn(#"Custom3", "Row Index of Name", each List.PositionOf([Data][Column1], "Name"))
Step 6: expand the rows
Section titled “Step 6: expand the rows”Remove every column except Table with Index, Parent Category and Row Index of Name, which are all that the remaining steps need.

Then click the expand button on Table with Index. Every row of every sheet now appears as a row of the query, and each row carries its sheet’s category and the position of its sheet’s header row.

Step 7: drop the rows above each table
Section titled “Step 7: drop the rows above each table”A row belongs to the product table when its Index is at least its sheet’s Row Index of Name. On the Add Column tab, choose Conditional Column and set it up as follows:
- If
Indexis greater than or equal to the columnRow Index of Name, output 1. - Otherwise, output 0.

The comparison has to include equality, because the header row itself must be kept for the next step.

Filter Custom to keep only the 1s, then remove the Index, Row Index of Name and Custom columns. What remains is each sheet’s product table, starting with its header row, followed by the category column.

Step 8: tidy the result
Section titled “Step 8: tidy the result”- On the Home tab, choose Use First Row as Headers. Because the first row is the Bikes sheet’s header row, the context column takes
Bikesas its header too, so rename it back toParent Category. - Rename the other columns as needed, such as
ProductNumbertoProduct Number, and set the data types.

- The header rows of the other sheets are still in the data, as rows whose
NamereadsName. Open the filter on theNamecolumn and clearName.

Step 9: load the result
Section titled “Step 9: load the result”Choose Close & Load. The products from every sheet arrive as one table, and each row keeps the category that was written above its original table.

Why it holds up
Section titled “Why it holds up”The query never counts rows from the top of a sheet. It finds fixed labels and reads relative to them, so it keeps working when someone adds a note above a table, removes a blank row or adds a new sheet in the same layout. The labels are the only thing it depends on, so those are what to check when a refresh gives an odd result.