Hi Akshat,
If you're using Cube as the source, then I assume (I may be wrong) that you wouldn't have to create the logic you did, to show the data in Hierarchy (Tree Structure), it would be like that by default.
Check the below official tutorials:
Web Intelligence 4.0: Create a query based on an OLAP universe
Web Intelligence 4.0: Connect to a BEx query with hierarchical data
Thanks,
Mahboob Mohammed