Pivot tables offer a powerful way to analyze data, particularly when you need to understand proportions within hierarchical categories. In this tutorial, I’ll show you how to quickly add sums in a pivot table and display values as a percentage of their parent row, allowing for immediate visualization of how individual items contribute to their category totals.
This technique is especially valuable when analyzing sales data across product categories and individual items.
Watch my step-by-step video tutorial below to see this process in action:
Setting Up Your Pivot Table
To begin creating an informative pivot table with percentage calculations, we need to start with a simple dataset that contains hierarchical information. In my example, I’m using a dataset that includes categories (such as fruits, vegetables, and sweets) along with the specific products within each category and their corresponding revenue figures.
The process of creating the pivot table is straightforward:
Select your data range
Navigate to the Insert tab in the Excel ribbon
Click on “Pivot Table”
Choose to place the pivot table on an existing worksheet (I selected cell F1 in my demonstration)
Click “OK” to create the basic pivot table structure
Once your pivot table framework is established, you’ll need to structure it properly to show both categories and their constituent products. In the PivotTable Fields panel, drag the appropriate fields to build your hierarchical view.
Structuring Your Pivot Table
For proper hierarchical analysis, you’ll want to arrange your fields in a logical order. In the Rows section of the PivotTable Fields panel, add your Category field first, followed by the Product field. This creates a nested structure where products appear under their respective categories.
For the values section, we need to add the Revenue field twice — once to show the raw sum and once to show the percentage of parent. Simply drag the Revenue field to the Values area twice. By default, Excel will sum these values, which is exactly what we want for this analysis.
Changing the Layout
By default, Excel displays pivot tables in compact form, but for better readability, I prefer the tabular layout. To change this:
Go to the Design tab under PivotTable Tools
Click on “Report Layout”
Select “Show in Tabular Form”
This adjustment separates the Category and Product into distinct columns, making your data more readable and easier to analyze at a glance.
Adding Percentage of Parent Row
Now comes the key part — transforming one of our revenue columns to show percentage of parent row. This calculation will show how each product contributes proportionally to its category total, and how each category contributes to the grand total. Follow these steps:
Right-click on any cell within the second Sum of Revenue column
Select “Show Values As” from the context menu
Choose “% of Parent Row Total”
This simple change transforms the raw numbers into percentages, giving you immediate insight into the proportional contribution of each item. For instance, in my example, you can now see that apples represent approximately 35% of all fruit sales, while the fruits category as a whole represents about 41% of total sales across all categories.
Understanding the Results
After applying the percentage of parent row calculation, your pivot table automatically adjusts to show meaningful proportions at every level:
Individual products show their percentage contribution to their immediate category
Category subtotals show their percentage contribution to the grand total
The grand total always equals 100%
In my demonstration, this clearly showed that sweets accounted for approximately 40% of total sales, vegetables for about 18%, and fruits for approximately 41%. Within each category, you can similarly see the proportional contribution of each product.
Finalizing Your Pivot Table
To make your pivot table more understandable, it’s important to rename the column headers to accurately reflect what each column represents. In our case:
Change the heading of the first sum column to simply read “Sum”
Rename the second column to “Percentage of Parent”
These descriptive headers ensure that anyone viewing your pivot table will immediately understand what the numbers represent without needing additional explanation.
With these adjustments complete, you now have a comprehensive pivot table that not only shows the raw revenue figures but also clearly illustrates the proportional relationships between categories and their constituent products. This dual-view approach provides both absolute and relative perspectives on your data, enabling more nuanced analysis and decision-making.
This technique is particularly valuable when analyzing sales performance, budget allocations, or any hierarchical data where understanding proportional relationships is important. By visualizing both raw numbers and percentages simultaneously, you gain deeper insights into your data structure and can more effectively communicate those insights to others.
In this tutorial, I demonstrate a practical application of recursive functions in Power Query to calculate hierarchy levels in organizational structures or MLM systems. Using a custom function with recursion, we can efficiently determine each person’s position in a hierarchical structure based on their referrer relationships, providing valuable insights for organizational analysis and reporting.
Understanding hierarchy levels is essential for visualizing reporting structures, tracking MLM downlines, or mapping any parent-child relationships in your data.
Understanding the Problem: Hierarchy Levels in Organizations
In many organizational structures, particularly in multi-level marketing (MLM) systems or corporate hierarchies, understanding the level depth of each member is crucial. The level represents how many steps a person is from the top of the organization. For instance, in our example, John is at the top (level 0), N is directly below John (level 1), and Thomas is below N (level 2).
Our sample data contains an ID column that uniquely identifies each person and a Referrer column that indicates who brought that person into the organization. The referral relationship establishes the hierarchical structure we need to analyze. Our goal is to calculate each person’s hierarchy level automatically using Power Query’s recursive capabilities.
Setting Up Power Query
To begin working with our data, we need to import it into Power Query where we can create and apply our recursive function:
Select any cell in your data table
Go to the Data tab
Click From Table/Range to import your data into Power Query
This imports your data containing the ID and Referrer columns into the Power Query Editor, where we can start building our solution.
Creating a Recursive Function to Calculate Hierarchy Levels
The core of our solution is a custom function that can call itself (recursion) to track up through the hierarchy until it reaches the top. Here’s how to create it:
In the Power Query Editor, go to the Home tab
Click New Source > Other Sources > Blank Query
Now we need to define our function. Our function will require two parameters: the person’s ID we want to calculate the level for and the complete table of people data to reference. The function will:
Find the row for the current person
Get their referrer’s ID
Check if they have a referrer
If they don’t (they’re at the top), return 0
If they do have a referrer, call the same function for the referrer and add 1 to the result
The M code for our function looks like this:
(personID, personTable) =>
let
personRow = Table.SelectRows(personTable, each [ID] = personID){0},
personAboveID = personRow[Referrer],
result = if personAboveID = null
then 0
else @HierarchyLevel(personAboveID, personTable) + 1
in
result
Make sure to name your function HierarchyLevel so that the recursive reference to itself works properly. The @ symbol in front of the function name emphasizes that we’re calling the same function again.
Applying the Function to Our Data
After creating our function, we need to apply it to every row in our data table:
Go back to your main query with the imported data
Click Add Column > Invoke Custom Function
Select your HierarchyLevel function
For personID, select the ID column
For personTable, we need to reference the current table
When setting the personTable parameter, we initially might try to reference a column name, but this will generate errors. Instead, we need to reference the entire table from the previous step. In Power Query, we can do this by referring to the previous step name.
Once correctly configured, the function will calculate the hierarchy level for each person in our table. Thomas, who is referred by N (ID 2) who in turn is referred by John (ID 1), will show as level 2. John, who has no referrer, will be at level 0.
Testing and Validating the Recursion
To verify our function works correctly, we can examine the calculated levels for each person in our organization:
John (ID 1): Level 0 (top of hierarchy, no referrer)
Anne (ID 2): Level 1 (referred by John)
Thomas (ID 5): Level 2 (referred by Anne)
Paul: Level 3 (referred by Thomas)
We can further test by changing referrer relationships. For example, if we change Paul’s referrer from Thomas (ID 5) to someone who is already at level 3, Paul would then become level 4. After making such changes in the source data, we can simply refresh our Power Query to see the updated hierarchy levels.
Top-level members (those with null referrers) are assigned level 0
The function will work for organizations of any depth, continuing to recurse up the chain until it reaches the top
If the data contains circular references (Person A refers to Person B who refers back to Person A), the recursion could create an infinite loop — consider adding error handling for this scenario in real applications
In a real-world scenario, you might want to enhance this function to handle more complex requirements, such as detecting circular references or processing multiple hierarchies within the same dataset.
Loading the Results Back to Excel
Once you’re satisfied with your hierarchy level calculations:
In the Power Query Editor, go to Home > Close & Load
Your data table with the new hierarchy level column will appear in Excel
Any time your hierarchy changes, simply refresh the query to recalculate all levels
This powerful technique allows you to maintain an up-to-date view of your organizational structure with minimal effort. The recursive approach efficiently handles even large hierarchies without requiring complex formulas or manual tracking.
With this solution in place, you can easily build reports and visualizations that leverage hierarchy level information, enabling better insights into your organizational structure, MLM downlines, or any hierarchical data you’re working with.