Power Query Excel.ShapeTable: How to Use It
In this article
Power Query's Excel.ShapeTable function turns a table in the current worksheet into a structured table that Power Query can read, and it is most useful when you are building a query that must adapt to a table whose name or position is not fixed in advance. You do not call it from the Power Query Editor's formula bar in a normal workbook query; it is designed for use inside a worksheet formula, typically with Excel.CurrentWorkbook or when you are generating a query from VBA or Office Scripts, and it is also what Power Query records when you load a table and then edit the source step. The practical way to use it is to let Power Query generate the step for you, then edit the generated Excel.ShapeTable call if you need to change the table name, the column count, or whether headers are promoted.
What You'll Need
- Excel for Microsoft 365, Excel 2021, or Excel 2019 on Windows or Mac with Power Query built in (on Windows it is on the Data tab; on Mac it is on the Data tab as well, though labels differ slightly by version and region).
- A worksheet table created with Insert > Table (or Ctrl+T), not just a formatted range.
Excel.ShapeTableworks on a table object, not on arbitrary cells. - The table must have a name. Click any cell in the table, then use Table Design > Table Name to set one.
- Basic familiarity with the Power Query Editor and the Advanced Editor, because you will usually edit the generated M code rather than type the function from scratch.
Step-by-Step
Create and name the source table. Select your data range and press Ctrl+T (Cmd+T on Mac), confirm the range and that the table has headers, then on the Table Design tab set a clear name such as
tblSales. Avoid spaces and special characters in the name; Power Query references it as a text string and spaces make the M code harder to read.Load the table into Power Query. Click any cell in the table, then go to Data > From Table/Range. The Power Query Editor opens with a query that already reads the table. If you are prompted to create a table first, cancel and repeat step 1.
Open the Advanced Editor to see the generated code. On the Home tab, click Advanced Editor. The first step will look something like . That is the normal path for a table in the same workbook, and it is usually what you want.
Recognise where Excel.ShapeTable appears. If you instead connect to a table through a workbook-level query, or if you record a query with the macro recorder, the source step may be written as . The first argument is the table content, and the optional record argument controls how many columns the shaped table has and whether the first row is treated as headers. The exact record fields can vary by version, so treat the generated code as the reference.
Edit the table name if the query must point at a different table. Change the string inside to the new table name. The name is case-sensitive in practice, so match the Table Design name exactly. If the name does not exist, the step fails with an error rather than returning an empty table.
Adjust the column count only when the generated code includes it. If your generated
Excel.ShapeTablecall has aColumnsfield, set it to the number of columns in the table. If you set it too low, extra columns are dropped; too high, you get null columns. If your generated code does not include that field, do not add it by hand unless you have confirmed the syntax from the function's own documentation in your Excel version.Promote headers if the shaped table did not do it. On the Home tab, click Use First Row as Headers if the first row appears as data. This adds a
Table.PromoteHeadersstep, which is more reliable than relying on the shaping options.Check the data types. Power Query often leaves columns as Any or Text. Click the type icon in each column header and set Whole Number, Decimal Number, Date, or Text as appropriate. Dates and numbers that stay as text will break later merges and calculations.
Close and load. On the Home tab, click Close & Load (or Close & Load To to choose a worksheet location or the Data Model). The query refreshes from the table each time the workbook recalculates or you click Refresh All.
Test the refresh path. Add a row to the source table, then click Data > Refresh All. If the new row appears in the output, the query is wired correctly. If it does not, the query is probably pointing at a different table name or a cached copy.
Common Mistakes
- Using a formatted range instead of a real table.
Excel.ShapeTableandExcel.CurrentWorkbookboth expect a table object. A range with fill colours is not a table and will not appear in the name list. - Renaming the table after the query is built. The query stores the old name as text. Rename the table and the query breaks until you update the string in the source step.
- Hard-coding a column count that no longer matches. If you add a column to the source table and the shaped table was fixed at the old count, the new column silently disappears from the output.
- Leaving headers unpromoted. The first data row then becomes the column names, and every later step references the wrong fields.
- Editing the generated M code without a backup. Copy the query before changing the source step; a broken source step is harder to recover from than a broken later step.
Tips
- Prefer for ordinary same-workbook tables. It is simpler, and it is what the Power Query Editor generates by default.
- Keep table names short, unique and free of spaces.
tblSalesis easier to spot in M code thanSales Table 2024. - If you need the query to follow a table whose name changes, store the name in a cell and reference that cell through a parameter query, then build the name string from it. This avoids editing M code every time.
- Use the Advanced Editor's step list to see exactly which step failed; the error message names the step, which is faster than guessing.
- When sharing the workbook, keep the source table on a visible sheet. A table on a very hidden sheet is harder to audit and easier to break accidentally.
Troubleshooting
The query errors with "We couldn't find an Excel table named...". The name in the source step does not match any table in the workbook. Check Table Design > Table Name, correct the string, and refresh.
The output has the wrong number of columns. The shaped table's column count is fixed in the generated code. Either remove that field and let Power Query read the full table, or update the count to match the current table.
New rows do not appear after refresh. Confirm you are refreshing the right query, that the source table name is correct, and that the table range actually expanded to include the new rows. Tables usually expand automatically, but a pasted block below the table may sit outside it.
Dates or numbers come through as text. Set the column type explicitly in the query rather than relying on the source formatting. If the source cells are text, change them to real dates or numbers first, because changing the query type will not convert text that only looks like a date.
The Advanced Editor shows code you did not write. That is normal when a query was recorded or generated. Read each step from top to bottom before editing, and change one step at a time so you can tell which edit caused a problem.
If that did not work, delete the query and rebuild it from Data > From Table/Range. Rebuilding is usually faster than repairing a heavily edited source step, and it guarantees the generated code matches your current Excel version.
Frequently Asked Questions
Is Excel.ShapeTable available in every Excel version?
It is part of the Excel connector that ships with Power Query in modern Excel for Microsoft 365, Excel 2021 and Excel 2019. Older versions and some Mac builds may not expose the same generated syntax, so rely on what your own Advanced Editor produces.
Can I type Excel.ShapeTable manually in a normal query?
You can, but the arguments must match your version's function signature. The safer route is to let Power Query generate the step and then edit the generated values, because a wrong argument produces a cryptic error.
Does it work on a table in a different workbook?
Not directly. Excel.ShapeTable and Excel.CurrentWorkbook read tables in the workbook that contains the query. For another workbook, use Data > Get Data > From File > From Workbook and point at the file.
Why does the query still show old data after I edit the table?
Power Query caches results until you refresh. Click Data > Refresh All, or right-click the output table and choose Refresh. Closing and reopening the workbook also forces a refresh on load if the query is set to refresh on open.
Can I use it with Office Scripts or VBA?
Yes, in the sense that recorded or generated queries may include the function. Office Scripts and VBA can create or refresh queries, but the M code itself is still edited in the Power Query Editor, not in the script.
What happens if the table has merged cells or subtotals?
Both break the clean tabular shape Power Query expects. Unmerge cells and remove subtotal rows before loading, or the shaped table will contain nulls and misaligned columns.