How to Transform Excel Data and Add Subtotals with Power Query
Question details
The user needs to clean raw Excel data by removing unnecessary columns, sorting records, grouping rows, and generating calculated invoice subtotals.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Preparing and automating a formatted data report with specific subtotal summaries using Power Query.
- Observed behavior
- The goal is to establish a maintainable process where input data can be refreshed to automatically produce cleaned data with organized details and calculated subtotals.
Ensure your raw data is formatted as an official Excel Table (press Ctrl+T) and contains clear column headers before importing it into Power Query.
Transform Data and Add Subtotals Using Power Query
Power Query is the most reliable and maintainable method for automating data cleanup, sorting, and adding subtotals without using complex VBA code.
Power Query (also known as Get & Transform) offers a robust visual interface to set up data transformation steps. Once these steps are configured, updating your subtotal report requires just a simple table refresh.
Select your Excel table, go to the Data tab on the ribbon, and click 'From Table/Range' to open the Power Query Editor.
Select any unwanted columns, right-click the header, and choose 'Remove'. Next, click the dropdown arrow on your grouping column (e.g., CPO) and select 'Sort Ascending'.
Go to the Transform tab and click 'Group By'. Choose your target column for grouping. Under the new column name (e.g., 'Invoice Total'), select the 'Sum' operation and choose the column containing your financial amounts.
Click 'Close & Load' on the Home tab to output the transformed data with subtotals to a new worksheet.
Whenever new data is added to your source table, simply navigate to your output sheet, right-click anywhere inside the green output table, and select 'Refresh' to update the subtotals.

Use VBA Macros or an SQL-Style ETL Process
If you require highly specific custom worksheet formatting alongside your subtotal calculations, a VBA macro or an SQL approach can be utilized.
Easily Group Data and Add Subtotals in WPS Spreadsheet
WPS Spreadsheet provides powerful, intuitive built-in tools like Pivot Tables and the Data Subtotal feature to organize your data and calculate totals instantly, without the need to learn complex query setups.
- 1. Open your dataset: Launch WPS Spreadsheet and open your data file containing the columns you wish to summarize.
- 2. Sort the grouping column: Select the column you want to group by and sort it in ascending order (A to Z) using the Data tab.
- 3. Apply the Subtotal feature: Navigate to the Data tab and click on 'Subtotal'. Choose your grouping column, select 'Sum' as the function, and check the boxes for the specific values you want to calculate.
- 4. Review the outlined summaries: Click OK. WPS Spreadsheet will automatically insert subtotal rows and provide collapsible outline buttons on the left side of the screen to easily toggle between detail and summary levels.

Frequently Asked Questions
Why is Power Query better than VBA for adding subtotals?
Power Query is generally easier to maintain because it uses an intuitive visual interface to record data transformation steps. You do not need to know how to read or write complex VBA code, making it accessible to more users. Furthermore, updating the report is as simple as clicking 'Refresh'.
How do I refresh my Power Query subtotal table when source data changes?
Once you have loaded your Power Query output into a worksheet, you can update it by right-clicking anywhere inside the loaded output table and selecting 'Refresh' from the context menu. Excel will instantly apply all your previous sorting, cleaning, and grouping rules to the updated source data.
Can I combine detail rows and grouped subtotals into one view using Power Query?
Yes. While grouping usually summarizes data, you can achieve a combined view by duplicating your original query, grouping one version to calculate subtotals, and then using the 'Append Queries' or 'Merge Queries' feature to combine the subtotal rows back with your detailed dataset before loading the final table.
What happens if I don't format my source data as an Excel Table before importing?
If your source data is just a normal range and not formatted as an official Excel Table (Ctrl+T), Power Query may not automatically detect and expand its reference range when new rows or columns are added later. Formatting as a Table ensures your subtotal calculations remain perfectly accurate as your dataset grows.




