top of page

You are learning Power Query in MS Excel

How to combine multiple rows into a single row based on specific criteria in Power Query?

There are a couple of ways to combine multiple rows into a single row based on specific criteria in Power Query. Here are two common approaches:

1. Group By and Aggregation:

This method works well when you want to summarize data for groups identified by specific columns.

Steps:

* Group By: Use the "Group By" feature to group rows based on the columns that define your criteria.
* Aggregate Values: For each group, use aggregation functions like SUM, MIN, MAX, or AVERAGE on the remaining columns depending on your needs.
* Expand or Unfold: If you want the grouped values in separate columns, use the "Expand" or "Unfold" options under the "Transform" tab.

Example:

Say you have a table with "Product ID", "Color", and "Quantity" columns. You want to combine rows with the same "Product ID" into a single row, showing the total quantity for each color.

1. Group By "Product ID".
2. Use "Sum" on the "Quantity" column.

This will give you a new table with "Product ID", "Color", and "Total Quantity" columns.

2. Custom Column and Transpose:

This method is more flexible for combining specific values from different rows into a single cell.

Steps:

* Custom Column: Create a custom column using conditional logic to define which value from each row should be included in the final output.
* Text.Combine: Use the `Text.Combine` function to concatenate these values from separate rows into a single cell with a chosen delimiter (e.g., comma or semicolon).
* Transpose: Use the "Transpose" feature to convert rows into columns. This brings the combined values into separate columns.
* Merge Columns: Use the "Merge Columns" feature to combine the newly created columns containing the desired values from each row.

Example:

Say you have a table with "Customer ID", "Name", and "Email" columns. You want to combine rows with the same "Customer ID" into a single row, showing the customer's name and all their email addresses separated by commas.

1. Create a custom column that returns the "Name" for the first row in each group and null for subsequent rows.
2. Create another custom column that uses `Text.Combine` to concatenate all "Email" values separated by commas within each group.
3. Transpose the table.
4. Merge the "Name" and "Combined Emails" columns into a single column with a chosen delimiter.

Remember, the best approach depends on your specific data structure and the desired output format. Feel free to ask if you have a specific scenario you'd like to explore!

bottom of page