Sales data analysis often requires summarizing results by specific categories to gain valuable business insights. In this article, I demonstrate how to efficiently calculate sales summaries using Power Query’s grouping functionality, providing a powerful alternative to traditional Excel formulas for analyzing merchant performance, income totals, and product distribution.
The grouping technique shown allows for multiple aggregations in a single operation, dramatically improving efficiency when working with larger datasets.
Watch my detailed tutorial video below to follow along with each step:
Understanding the Sales Summary Challenge
When analyzing sales data, we often need to create summaries that show performance metrics grouped by specific categories. In this demonstration, I’m working with a dataset containing sales transactions that include merchants, products, dates, and income values. My goal is to summarize this data to show several key metrics for each merchant:
- Count of sales transactions
- Sum of income
- Average income per transaction
- List of unique products sold
While these calculations can be done using Excel formulas (which I’ve prepared in the worksheet for comparison), using Power Query offers significant advantages, especially for larger datasets or repetitive analysis tasks.
Importing Data into Power Query
The first step in our process is to bring the data into Power Query where we can perform the grouping operations:
- Select the data table in Excel
- Navigate to the Data tab in the Excel ribbon
- Click “From Table/Range” to import the data into Power Query
Once the data is loaded into the Power Query Editor, we can see all our sales records and begin the grouping process to create our summary.
Creating Groups in Power Query
To summarize our data by merchant, we need to use the Group By function in Power Query:
- Select the Merchant column in the Power Query Editor
- Go to the Home tab and click “Group By”
The default Group By dialog offers basic functionality with just one column for grouping and a single aggregation. For our needs, we require the Advanced option since we want to create multiple aggregations based on the merchant column.
Setting Up Advanced Grouping
In the advanced Group By dialog, we need to configure multiple aggregations:
- Set the grouping column to Merchant
- Add the following aggregations:
- “Count Rows” for the number of sales (named “Count”)
- “Sum” of the Income column (named “Sum”)
- “Average” of the Income column (named “Average”)
- “All Rows” to capture all related data (named “List”)
After clicking OK, Power Query creates a new table with our merchant groups and the specified aggregations. The Count, Sum, and Average values match the Excel formulas we prepared earlier, confirming that our grouping is working correctly.
Customizing the Product List
The “All Rows” aggregation creates a nested table for each merchant containing all their sales records. However, we only want a list of unique products sold by each merchant, not all the row data. Since the Group By dialog doesn’t offer this specific option, we need to modify the M formula directly.
Modifying the M Formula
To customize our product list, we need to edit the M code that Power Query generated:
- The default “All Rows” aggregation produces tables with all columns for each merchant
- We need to change this to extract only the Product column values
- Furthermore, we need to eliminate duplicates from these lists
The original formula includes {_} which references the entire row. We need to replace this with a reference to just the Product column: [Product]. Additionally, we need to wrap this in the List.Distinct function to remove duplicates:
The modified formula section should look like: List.Distinct([Product])
Transforming the Product List into Text
Now that we have lists of unique products for each merchant, we can transform these lists into comma-separated text values for better readability:
- Click on the expand button in the List column header
- Select “Extract Values…” rather than “Expand to New Rows”
- Choose a custom delimiter (comma followed by space: “, ”)
- Click OK
This transforms our product lists into readable text strings showing all the unique products each merchant has sold, separated by commas.
Loading the Results Back to Excel
Once we’ve created our summary with all the required metrics, we can load the results back to Excel:
- Go to the Home tab and click “Close & Load To…”
- Select “Existing Worksheet” and choose where to place the results
- Click OK
After loading, we can format the Sum and Average columns as currency to improve readability. The final result is a clean summary table showing the count of sales, total income, average income, and unique products for each merchant.
Benefits of Using Power Query for Grouping
While our example used a relatively small dataset, the advantages of using Power Query for this type of analysis become even more apparent with larger data volumes:
- Efficiency — Performs calculations much faster than Excel formulas for large datasets
- Repeatability — The query can be refreshed when data changes without redoing formulas
- Flexibility — Allows for complex aggregations and transformations that would be difficult with Excel formulas
- Data cleaning — Integrates with other Power Query transformations for comprehensive data preparation
By mastering this grouping technique in Power Query, you can create sophisticated sales summaries and other data aggregations with just a few clicks, saving significant time and effort compared to traditional Excel methods.