top of page

You are learning Power Query in MS Excel

How to fold data (hierarchies) in Power Query?

Creating data hierarchies, or "folding data," in Power Query allows you to group data into nested levels for better organization and analysis. This can be particularly useful for creating drill-down reports or structured data views. Here’s a step-by-step guide on how to create and manage hierarchies in Power Query:

Steps to Create Hierarchies in Power Query

1. Load Your Data into Power Query

1. Open Excel and Load Data:
- Go to the `Data` tab and select `Get Data` > `From File` (e.g., `From Workbook`) to load your data into Power Query.

2. Open Power Query Editor:
- In the `Queries & Connections` pane, right-click on your data query and select `Edit` to open the Power Query Editor.

2. Group Data to Create Hierarchies

1. Identify Hierarchical Levels:
- Determine the levels of your hierarchy. For example, if you have sales data, your hierarchy might be Country > Region > City > Store.

2. Group By:
- To create the first level of your hierarchy, go to the `Home` tab and select `Group By`.
- In the `Group By` window, select the column that represents the highest level of your hierarchy (e.g., Country).
- Choose to create an aggregation (e.g., sum of sales) or to keep rows in a new column.

3. Create Nested Groups:
- To create nested groups, expand the grouped column by clicking on the expand icon next to the new column created by the first grouping.
- Repeat the `Group By` process for each subsequent level of the hierarchy (e.g., Region, City, Store).

3. Expand and Aggregate Data

1. Expand Data:
- After grouping, you might need to expand the grouped data to see detailed records.
- Click on the expand icon next to the grouped column and select the columns you want to include in the expanded view.

2. Aggregate Data:
- When grouping, you can perform various aggregations such as sum, average, count, etc.
- In the `Group By` window, under `New column name`, specify the name and choose the aggregation operation (e.g., sum of sales).

Example: Creating a Sales Hierarchy

Let's assume you have a dataset with the columns: Country, Region, City, Store, and Sales.

Step-by-Step Example

1. Group by Country:
- Go to `Home` > `Group By`.
- In the `Group By` window:
- Group by: Country
- New column name: TotalSales
- Operation: Sum
- Column: Sales

2. Group by Region within each Country:
- Expand the grouped column by clicking the expand icon next to `TotalSales`.
- Go to `Home` > `Group By`.
- In the `Group By` window:
- Group by: Region
- New column name: RegionSales
- Operation: Sum
- Column: Sales

3. Group by City within each Region:
- Expand the grouped column by clicking the expand icon next to `RegionSales`.
- Go to `Home` > `Group By`.
- In the `Group By` window:
- Group by: City
- New column name: CitySales
- Operation: Sum
- Column: Sales

4. Group by Store within each City:
- Expand the grouped column by clicking the expand icon next to `CitySales`.
- Go to `Home` > `Group By`.
- In the `Group By` window:
- Group by: Store
- New column name: StoreSales
- Operation: Sum
- Column: Sales

Result

You will end up with a table that shows the hierarchical structure of your sales data, with total sales aggregated at each level (Country, Region, City, Store).

Tips for Managing Hierarchies

- Consistent Column Naming: Use consistent and descriptive names for your grouped and aggregated columns to make the hierarchy clear.
- Data Validation: Ensure that the data in each hierarchical level is clean and consistent to avoid grouping errors.
- Performance Considerations: Grouping large datasets can be resource-intensive. Optimize your queries and consider filtering data to improve performance.

By following these steps, you can effectively create and manage hierarchies in Power Query, enabling better data organization and analysis.

bottom of page