Creating Recursive Org Hierarchy with SubReports
An organizational hierarchy represents roles, responsibilities, and authority within an organization in a structured way. It represents both the chain of command (who reports to whom) and the mechanism by which decisions flow through the different organizational levels.
This article is a step-by-step tutorial on building a report representing an Org Hierarchy structure similar to the one below:
CEO ├── CTO │ ├── VP of Engineering │ │ ├── Engineering Manager │ │ │ ├── Engineers ├── CFO │ ├── Finance Manager │ │ ├── Accountants
The approach demonstrated here uses a recursive SubReport to construct a hierarchy from flat data. The main report references itself in a SubReport item, and each SubReport filters its data using a report parameter passed from its parent. This process continues recursively, building the hierarchy until no more child items remain:
To create the Organizational Hierarchy, follow the steps:
-
Create a new report (
OrgStructure.trdp), which you will use as a report to show the employees. -
Delete the Header and Footer. Leave only the Detail Section.
-
Create a CSV data source item using the self-referencing flat data for the employees. Every data record contains own EmployeeID and the ID of the manager this person reports to, forming the chain-of-command up to the company CEO:
CSVEmployeeID,Name,JobTitle,ManagerID,Department,Location 1,Alice Johnson,CEO,0,Executive,New York 2,Bob Smith,CTO,1,Technology,New York 3,Carol White,CFO,1,Finance,New York 4,David Brown,VP of Engineering,2,Technology,San Francisco 5,Eva Green,VP of Product,2,Product,San Francisco 6,Frank Black,Engineering Manager,4,Technology,Austin 7,Grace Lee,Engineering Manager,4,Technology,Austin 8,Henry Adams,Product Manager,5,Product,Seattle 9,Ivy Clark,Senior Engineer,6,Technology,Austin 10,Jack Davis,Senior Engineer,7,Technology,Austin 11,Karen Lewis,Engineer,6,Technology,Austin 12,Leo Martin,Engineer,7,Technology,Austin 13,Mia Nelson,Finance Manager,3,Finance,Chicago 14,Noah Perez,Accountant,13,Finance,Chicago 15,Olivia Quinn,Accountant,13,Finance,Chicago 16,Paul Roberts,HR Director,1,Human Resources,New York 17,Quinn Taylor,HR Manager,16,Human Resources,Chicago 18,Rachel Scott,Recruiter,17,Human Resources,Chicago 19,Sam Turner,Legal Counsel,1,Legal,New York 20,Tina Walker,Compliance Officer,19,Legal,New York
-
Toggle the option This CSV has header row.
-
For the
EmployeeIDandManagerIDcolumns, use theIntegertype.
-
Set the Report's data source to the just created CSV data source.
-
Create a report parameter (
reportParameterManagerID) of typeIntegerwith Value0.
-
Add two TextBox items and bind their values to the
NameandJobTitlefields respectively:
-
Now, filter the CSV data based on the
ManagerID. Add a Filter based on theManagerIDfield using the report parameter's value:= Fields.ManagerID = = Parameters.reportParameterManagerID.Value -
Click the Preview button. You will see only the CEO (Alice Johnson) because the
ManagerID=0and only the CEO meets the filter criterion. -
Add a SubReport item and set the Report source Type to
UriReportSourceusing the sameOrgStructure.trdpas Uri:
-
Create a parameter (
reportParameterManagerID) whose value is bound to theEmployeeID. Thus, the currentEmployeeIDwill be passed as a value for theManagerIDfor the SubReport.
-
Preview the report to see the organization hierarchy: