How to Calculate Employee Hierarchy Levels in Excel with Power Query
Question details
The user needs a method to calculate multiple levels of management hierarchy for a large employee dataset using Power Query instead of basic worksheet formulas.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Managing large organizational charts or employee lists (e.g., 23,000+ employees) where each record contains an Employee ID and a Supervisor ID.
- Observed behavior
- Repeated worksheet lookups like XLOOKUP or VLOOKUP become slow and difficult to maintain on massive datasets, whereas Power Query handles these merges more efficiently.
Ensure your employee dataset is formatted as an official Excel Table and contains distinct, unique columns for Employee ID and Supervisor ID before importing it into Power Query.
Use Power Query Consecutive Merges
Perform sequential self-joins in Power Query to build the hierarchy level by level efficiently.
Power Query can solve hierarchy calculations by performing consecutive merges. Each step matches the current Supervisor ID with the corresponding Employee ID to retrieve the next supervisor up the chain.
Select your data table in Excel, navigate to the 'Data' tab, and click 'From Table/Range' to open the Power Query Editor.
On the Home tab, click 'Merge Queries'. In the merge dialog, select the exact same query in the second dropdown to perform a self-join.
Select the 'Supervisor ID' column in the top table preview, and match it to the 'Employee ID' column in the bottom table preview, then click OK.
Click the expand icon at the top right of the newly merged column. Uncheck all columns except 'Supervisor ID', uncheck 'Use original column name as prefix', and rename the extracted column to 'Level 2 Supervisor'.
Repeat the merge and expand process up to five times to calculate deeper hierarchy levels. Add a conditional column at the end to verify if the top-level supervisor has been reached (i.e., when the Supervisor ID is null).

Use XLOOKUP Formulas for Smaller Datasets
If your dataset is relatively small, you can use nested XLOOKUP formulas directly in the worksheet.
Edit and Analyze Large Datasets Seamlessly with WPS Office
If you are looking for a fast, lightweight, and completely free alternative to Microsoft Office, WPS Office offers powerful spreadsheet capabilities. It fully supports advanced formulas, large datasets, and seamless compatibility with Microsoft Excel formats.
- 1. Download and Install: Get WPS Office for free from the official website and run the quick installer.
- 2. Open Your Excel File: Launch WPS Spreadsheets and open your existing .xlsx employee dataset.
- 3. Calculate using Built-in Functions: Use WPS Spreadsheets' comprehensive function library to manage and analyze your hierarchy data effortlessly.

Frequently Asked Questions
Why use Power Query over XLOOKUP for hierarchy mapping?
Power Query is optimized for large datasets. It processes data in the background, making your workbook lighter and preventing the lag associated with calculating thousands of live XLOOKUP formulas in real-time.
Can I calculate an unlimited number of hierarchy levels in Power Query?
While you can perform many consecutive merges, each merge requires a manual step in the Power Query editor. For exceptionally deep or variable hierarchies, advanced users often rely on custom M code or recursive logic functions instead of standard UI merges.
How do I update the hierarchy calculation if employee supervisors change?
Once your Power Query steps are saved and loaded to a worksheet, simply right-click the output table and select 'Refresh'. Power Query will automatically pull the updated source data and recalculate all hierarchy levels.




