Skip to content

Preserving Context

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:

A worksheet with three rows of irrelevant notes at the top, a small table below them giving the Parent Category as Bikes with a report date, and then the product table with Name, ProductNumber, Color, StandardCost, ListPrice and Size columns

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.

  1. On the Data tab, choose Get Data, then From File, then From Workbook, and select Products.xlsx.
  2. In the Navigator, select the workbook itself rather than one sheet, and choose Transform Data. The editor shows one row per sheet.

The workbook in the editor as a table of four rows, one for each of Sheet1 to Sheet4, with Name, Data, Item, Kind and Hidden columns and each Data cell holding a table

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

The query reduced to a single Data column with four rows, each holding a table

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.

The query with a second column, Table with Index, alongside Data, both holding a table in each of the four rows

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.

The new Row Index of Parent Category column, holding 4, 2, 1 and 0 for the four sheets, with Custom2 selected in Applied Steps

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 new Parent Category column, reading Bikes, Accessories, Components and Clothing for the four sheets

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"))

The new Row Index of Name column, holding 7, 5, 4 and 3 for the four sheets

Remove every column except Table with Index, Parent Category and Row Index of Name, which are all that the remaining steps need.

The query reduced to the Table with Index, Parent Category and Row Index of Name columns

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.

The expanded rows of the Bikes sheet, where Index runs from 0 upwards, rows 0 to 6 are empty, row 7 holds the headers StandardCost, ListPrice and Size, and the Parent Category and Row Index of Name columns read Bikes and 7 on every row

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 Index is greater than or equal to the column Row Index of Name, output 1.
  • Otherwise, output 0.

The Add Conditional Column dialog, comparing Index with the column Row Index of Name using is greater than or equal to, with an output of 1 and an Else value of 0

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

The new Custom column, which is 0 for rows 0 to 6 and 1 from the header row at index 7 onwards

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.

The filtered rows in generic columns Column1 to Column6, starting with the header row Name, ProductNumber, Color, StandardCost, ListPrice and Size, followed by the bike products, with Bikes in the Parent Category column

  1. 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 Bikes as its header too, so rename it back to Parent Category.
  2. Rename the other columns as needed, such as ProductNumber to Product Number, and set the data types.

The promoted table with Name, Product Number, Color, Standard Cost, List Price and Size headers, where the context column is headed Bikes because it took the first row’s value

  1. The header rows of the other sheets are still in the data, as rows whose Name reads Name. Open the filter on the Name column and clear Name.

The filter list on the Name column with the Name entry cleared and the product names left ticked

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.

The loaded worksheet table of bike products with their numbers, colours, costs, prices and sizes, and the category in the last column, which in this capture is still headed Bikes

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.