If you have worked with Excel’s Data Model and CUBE functions, you probably appreciate being able to keep the source data behind a report and display just the answers you need. Sometimes that is the part of the workflow you want: a few related tables, a useful calculation, and a worksheet someone can interact with.
Python in Excel offers another way to do that. Power Query can leave the source tables as connection-only queries, and Python can read those queries directly. We can combine the tables and send a small summary back to the worksheet, without giving every source table its own tab.
Let’s try it with two Python cells and a Region dropdown. We will use pandas, which is already included in Python in Excel. This example does not read an existing Power Pivot Data Model or reuse DAX measures; it builds a separate reporting workflow from the source tables.
What you need to follow along
Use Excel for Microsoft 365 on Windows with Python in Excel available. These instructions demonstrate the desktop application. Microsoft currently excludes the Power Query-to-Python route from Excel for the web; see its Power Query import instructions for the current platform details.
Browse the reader files on GitHub.
The download contains a starter workbook, three CSV files, and the copy-ready Python blocks. The data is fictional: 240 sales, 12 products, and 24 customers. Sales contains the amounts we want to summarize, while the other tables supply product categories and customer regions.
| Source | What it contributes | Matching field |
|---|---|---|
| Sales | Transaction detail, revenue, and cost | ProductID and CustomerID |
| Products | Product name and category | ProductID |
| Customers | Customer name and region | CustomerID |
Open report-starter.xlsx from the extracted download. Its Report sheet already has a Region dropdown in B3 and space for our results. Leave the Python sheet before Report in the workbook’s tab order: Python cells calculate in worksheet order, then across each row and down the sheet. Our first cell needs to prepare the data before the second uses it. Microsoft explains the calculation order here.
1. Create three connection-only Power Queries
On the Data tab, choose Get Data > From File > From Text/CSV and open sales.csv. Choose Transform Data so you can inspect the columns in Power Query Editor. Name the query Sales, preserving the capitalization used in the code below. Check that Revenue and Cost are numeric.
Choose Home > Close & Load > Close & Load To. In the Import Data dialog, select Only Create Connection and leave Add this data to the Data Model unchecked.

Repeat the import for products.csv and customers.csv, naming the queries Products and Customers. If the preview shows generic column names such as Column1, Column2, and Column3, choose Home > Use First Row as Headers before loading. Verify that Products has ProductID, Product, and Category, and Customers has CustomerID, Customer, and Region. Then choose Only Create Connection for each query. The Queries & Connections pane should now list all three queries without adding source-data worksheets.

That last point is easy to miss. A connection-only query can still be an input to Python. You do not have to land it on the grid first and read it back in.
2. Read and combine the tables in one Python cell
Go to Python!A1. Choose Formulas > Insert Python to enter Python mode, then paste this block:
sales = xl("Sales")
products = xl("Products")
customers = xl("Customers")
data = sales.merge(products, on="ProductID").merge(customers, on="CustomerID")
data["Profit"] = data["Revenue"] - data["Cost"]
data
The first three lines use xl() to read our named queries. Each becomes a pandas DataFrame, which is a table-like Python object with labeled columns. The names sales, products, and customers let us refer to those objects in the code.
The merge() line matches the tables using ProductID and CustomerID, bringing category and region onto each sales record. It performs a job you might otherwise handle with lookup formulas or Power Query merges. Our clean example assumes unique IDs in Products and Customers and a matching product and customer for every sale.
The next line adds Profit by subtracting Cost from Revenue. The final data tells Excel which Python object to return from the cell.
Run the cell with Ctrl+Enter and keep its output as a Python Object. Select the DataFrame card to inspect the combined data without spilling all 240 rows onto the worksheet.
We now have one combined table available to another Python cell. Unlike a relationship in Power Pivot, this merge actually assembles the columns into a new DataFrame. If the object itself is unfamiliar, my guide to creating DataFrames in Python in Excel introduces the different input routes.
3. Return a category report to the worksheet
On Report, leave B3 set to All. Insert a Python cell in B9 and enter:
region = xl("Report!B3")
selected = data
if region != "All":
selected = data[data["Region"] == region]
report = selected.groupby("Category", as_index=False)[["Revenue", "Profit"]].sum()
report.loc[len(report)] = ["Total", report["Revenue"].sum(), report["Profit"].sum()]
report["Margin"] = report["Profit"] / report["Revenue"]
report
This cell reads the Region dropdown and starts with all the data. If the selected value is something other than All, the indented line keeps only rows for that region. The == compares each row’s Region with our selection; != means “not equal to.”
Next, groupby() collects the rows by Category and sum() adds their Revenue and Profit. Think of the category rows and summed values in a PivotTable. The as_index=False setting keeps Category as an ordinary output column.
The report.loc[len(report)] line adds a Total row after the category rows, with their combined revenue and profit. We then calculate Margin for every row. Adding the total before that calculation gives us total profit divided by total revenue, rather than an average of the category percentages. Python returns the complete report, including its totals.
Run the cell with Ctrl+Enter, then change its output type to Excel Values. The report appears in B9:E13, with headers, three category rows, and a Total row. Format Revenue and Profit as currency with two decimal places, and Margin as a percentage with two decimal places.

With All selected, the Total row should show $94,275.25 revenue, $30,346.25 profit, and 32.19% margin. As a category check, Furniture should show $52,721.50 revenue, $17,251.50 profit, and 32.72% margin.
4. Change the region
Choose North in B3. With automatic calculation enabled, the report should update. Its Total row now shows $15,787.50 revenue, $4,998.80 profit, and 31.66% margin.

Someone using this report can change the region without editing Python. The code keeps referring to the same dropdown and applies the same calculation to the selected rows. That is the useful resemblance to a model-based report: source data stays behind the scenes while a small worksheet presents the answers.
Changing the region uses data already imported into the workbook. When the CSV files themselves change, use Data > Refresh All and let the queries finish before checking the Python results.
Can this replace the Data Model and CUBE functions?
It can be a useful alternative when your immediate goal is a worksheet report based on several related tables. The combined DataFrame is also available for a chart, statistical analysis, or another Python calculation.
CUBE functions remain the appropriate route when you want to query measures from an existing Excel Data Model. This pandas workflow creates neither a cube connection nor DAX filter context, and its calculations do not become model measures for PivotTables. We are reading Power Query results and writing ordinary Python code.
There is a fair question here: couldn’t Power Query perform the merges and grouping? Yes. If you only need one fixed summary, you may already have everything you need there. Python earns its place here by connecting the prepared data to a worksheet selection and making that data available for further analysis in the same environment.
I made a related point in this earlier discussion about where Python fits alongside Power Query. The useful choice depends on the work you want to repeat and the people who will maintain it.
My Power Query and Python comparison goes further into that division of work. You could move the merges into Power Query and still use the same basic idea: read a prepared query, analyze it with Python, and return only the results the worksheet needs.
Keeping records off the worksheet does not make the workflow unlimited in size. Python calculations run in Microsoft’s cloud, and Microsoft documents a limit of 100 MB of data per calculation. Current Python in Excel limits
Keep learning Python in Excel
If this is your first pandas example, start by getting comfortable with what each cell contributes: the first combines the source tables, and the second filters and summarizes them. You can change the report’s region while leaving both blocks alone.
This example brings together several skills I teach in Python in Excel for Data Analytics: working with DataFrames, filtering and summarizing data, and connecting Python calculations to an Excel workbook. Chapter 2 develops the pandas foundation, and Chapter 10 covers integration with Power Query and worksheet controls. If you would like a structured way to build on this example, take a look at the book and its downloadable exercises.
