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 and how to pick the right object.
What the connector shows you
Choose Get Data > Excel workbook and select a file. The Navigator window lists the objects Power Query found. Each has an icon and a kind:
| Object | Kind in Power Query | Notes |
|---|---|---|
| Excel table (Ctrl+T) | `Table` | Has a stable name and fixed column headers. |
| Worksheet | `Sheet` | Includes the whole used range, including stray cells. |
| Named range | `DefinedName` | Returns only the cells the name covers. |
Tables are usually the best choice. A table grows as rows are added, so a refresh picks up new data. It also keeps its name when someone moves it on the sheet. A sheet returns everything on it, including titles, notes, and blank rows, so you must clean those out. A named range is fixed to its cells unless the name is defined with a dynamic formula.
The query Power BI generates
Select the Sales table in the Navigator and choose Transform Data. Power Query writes this M code:
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
TypedEach line is an applied step:
Excel.Workbookopens the file and returns a table listing every sheet, table, and named range. The second argument,true, uses the first row as headers. The third,true, makes the connector infer column types.Source{[Item = "Sales", Kind = "Table"]}[Data]picks one row from that list by name and kind, then reads itsDatacolumn.Table.TransformColumnTypessets the type of each column you name.
The lookup uses Item and Kind, so a sheet and a table both called Sales do not collide. If someone renames the table, the lookup fails and refresh reports an error that the key matched no rows.
Working with sheets
If you must read a sheet, expect extra cleanup. Typical steps are:
- Remove Top Rows to drop titles above the header.
- Use First Row as Headers to promote the real header row.
- Remove Blank Rows and delete empty columns.
Because these steps depend on row positions, a change to the layout of the sheet can break them. Ask the workbook owner to turn the range into a table if you can.
Common errors
- "The key didn't match any rows in the table." The table, sheet, or name in
Itemwas renamed or deleted. Fix the name in the step, or restore it in the workbook. - Columns typed as text or errors in a column. The connector inferred types from the first rows only. Set types explicitly with
Table.TransformColumnTypes, and use a locale-aware conversion if dates come from another region. - The file is locked or cannot be read. Close the workbook if another program holds it open, and check that your account can read the file.
- Old
.xlsfiles. These need the Access Database Engine on the machine, and Power BI Desktop and the engine must have the same bitness (32-bit or 64-bit).
Exercise
A workbook has a sheet named Budget that holds a title in row 1, a blank row, and then data. It also has an Excel table named tblBudget over the same data. Which object should you select, and why?
Show answer
Select tblBudget. A table returns only its own rows with proper headers, grows as rows are added, and does not carry the title and blank row. Selecting the sheet would return those extra rows and force you to add position-dependent cleanup steps.
Exercise
What do the Item and Kind fields do in Source{[Item = "Sales", Kind = "Table"]}[Data]?
Show answer
They select one row from the list of workbook objects. Item matches the object's name and Kind matches its type (Table, Sheet, or DefinedName). [Data] then reads the contents of that row as a table.