Sub topic 4
Connecting to an Excel workbook: sheets, tables, and named ranges
This chapter assumes you know the Power BI Desktop interface, can work with Excel tables, and understand rows, columns, and data types. It covers how the Excel connector exposes a workbook, which kind of object to pick, and the errors that most often appear.
What the Excel connector returns
When you choose Get Data > Excel workbook and pick a file, Power Query calls Excel.Workbook. That function returns a table with one row per object it finds in the file. The Navigator window is a friendly view of that table.
Each row has these columns:
| Column | Meaning |
|---|---|
| Name | The display name of the object |
| Data | A nested table holding the object's contents |
| Item | The sheet, table, or defined name |
| Kind | `Sheet`, `Table`, or `DefinedName` |
| Hidden | `true` if the object is hidden |
Your query selects one row by Item and Kind, then reads its Data column. The Navigator shows three kinds of objects, each with its own icon.
- Sheets are whole worksheets. Power Query reads the used range of the sheet.
- Tables are Excel tables created with Ctrl+T. They have a name and a fixed column header row.
- Named ranges are defined names that point to a cell range. They appear with the
DefinedNamekind.
The basic query
Here is the query Power BI generates for a workbook with an Excel table named Sales. It also sets types for two columns.
let
Source = Excel.Workbook(File.Contents("C:\Data\Sales.xlsx"), true, true),
SalesTable = Source{[Item = "Sales", Kind = "Table"]}[Data],
Typed = Table.TransformColumnTypes(SalesTable, {{"OrderDate", type date}, {"Amount", type number}})
in
TypedRead it step by step:
File.Contentsreads the file as binary data. It does not open Excel.Excel.Workbook(..., true, true)parses that binary. The second argument,useHeaders, promotes the first row to column names. The third,delayTypes, skips automatic type detection so you can set types yourself.Source{[Item = "Sales", Kind = "Table"]}[Data]filters the object list to one row and takes itsDatavalue. The curly braces select a row by field values. The square brackets select a column.Table.TransformColumnTypesassigns the types.
The connector reads the file directly. Excel does not need to be installed, and the workbook does not need to be open. If the workbook is open and saved, Power Query can usually still read it. Unsaved changes are not visible to Power Query.
Choosing between sheets, tables, and named ranges
The right choice depends on how stable the data's boundaries are.
Tables
An Excel table is the best option when you control the workbook. Its name stays the same when you move it, and it grows automatically when users add rows. Column headers are guaranteed to be the first row of the table, so the header promotion is predictable. Power Query picks up new rows on the next refresh with no changes to the query.
Named ranges
A named range is useful when the workbook has no table but the data sits in a defined block. A name such as SalesData points to Sheet1!$A$1:$F$500. The range does not grow on its own. If someone adds row 501 and does not extend the name, that row is missing from your report. You can make the range dynamic with a formula using OFFSET or INDEX, but tables are simpler and less error prone.
Sheets
A sheet gives you everything on the worksheet, including titles, notes, blank rows, and totals that sit above or below the data. This is the most fragile option, because the query depends on where things happen to sit. Use it when you cannot change the source file. Expect to remove top rows, filter out total lines, and promote headers yourself.
Here is a summary of the trade-offs:
| Object | Grows with data | Header row | Best for |
|---|---|---|---|
| Table | Yes | Reliable | Files you or your team own |
| Named range | Only if the name is updated | Usually the first row | Fixed blocks without a table |
| Sheet | Yes, by used range | Often not the first row | Files you cannot change |
Working with a sheet
When you connect to a sheet, the first rows often contain a title or blank cells. The data arrives with generic column names such as Column1. A typical cleanup looks like this:
let
Source = Excel.Workbook(File.Contents("C:\Data\Sales.xlsx"), null, true),
Sheet = Source{[Item = "Report", Kind = "Sheet"]}[Data],
NoTitle = Table.Skip(Sheet, 2),
Promoted = Table.PromoteHeaders(NoTitle, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderDate", type date}, {"Amount", type number}})
in
TypedHere the second argument of Excel.Workbook is null, so headers are not promoted automatically. Table.Skip removes the two title rows. Table.PromoteHeaders then turns the next row into column names. Order matters: promote headers only after the extra rows are gone.
The used range of a sheet can also include cells that look empty but have formatting. These produce trailing rows or columns full of null. Remove them with Remove Blank Rows or by deleting the unneeded columns.
Hidden objects and the Hidden column
Excel.Workbook lists hidden sheets, and it can list tables on hidden sheets. The Navigator shows them like any other object. If you build a query that selects by position instead of by name, a hidden object can change which row you get. Select by Item and Kind, as in the examples above, and the query keeps working when objects are added or reordered.
You can also filter on the Hidden column when you want to loop over all visible sheets in a workbook:
let
Source = Excel.Workbook(File.Contents("C:\Data\Sales.xlsx"), true, true),
Visible = Table.SelectRows(Source, each [Kind] = "Sheet" and [Hidden] = false)
in
VisibleTypical errors and how to read them
Most Excel connection problems come from a few causes.
- The file path is wrong or the file moved. The error says the file cannot be found. Check the path in the
File.Contentscall, or use a parameter, which a later chapter covers. - A table or sheet was renamed. The error reads
The key didn't match any rows in the table. The row selector{[Item = "Sales", Kind = "Table"]}found nothing. Open the query in the Advanced Editor, or the Navigator step, and update the name. - A column was renamed or deleted. The error reads
The column 'X' of the table wasn't found. The step that references the column fails. Fix the column name in the source or edit the step. - A value does not match the type. Cells in the column appear as
Errorafter a type change. A column that mixes text and numbers, such as an ID column withA-100and205, cannot convert to a whole number. Choose text for such columns. - The file is an older
.xlsformat or is protected. Older formats may need the Access Database Engine (ACE) OLE DB provider. Files with a password cannot be read by this connector.
Applying the connector in practice
Follow these steps to connect to a workbook and keep the query stable.
- In Power BI Desktop, select Get Data > Excel workbook and open the file.
- In the Navigator, tick the table, sheet, or named range you need. Prefer a table.
- Select Transform Data to open Power Query Editor, not Load, so you can check types first.
- Review the Applied Steps pane. Confirm that
Navigationpoints to the object you want. - Set each column's data type explicitly, then select Close & Apply.
Keeping type steps explicit means a change in the source shows up as a clear error in a named step, instead of silent wrong values downstream.
Exercise
A colleague's workbook has the sales data in a sheet with a title in row 1 and an empty row 2. Column headers start in row 3. Your query uses Excel.Workbook(File.Contents(path), true, true) and selects the sheet, but the columns are named Column1, Column2, and so on. Why does this happen, and how do you fix it?
Show answer
With useHeaders set to true, Power Query promotes the first row of the sheet, which is the title row or a blank, so the real headers remain as data. Pass null for useHeaders, use Table.Skip(Sheet, 2) to remove the title and blank rows, then call Table.PromoteHeaders with [PromoteAllScalars = true]. Better still, ask the colleague to convert the block into an Excel table so the query can select it by name.
Exercise
Your report reads the named range SalesData, which points to Sheet1!$A$1:$F$500. This month the data grew to 520 rows, but the report shows only 499 data rows. What is the cause, and what are two ways to prevent it?
Show answer
The named range is fixed and does not grow when rows are added, so rows 501 to 520 are outside it. Prevent this by converting the data to an Excel table, which expands automatically, or by redefining the name with a dynamic formula using INDEX or OFFSET. The table is simpler and more reliable.