How to get custom number of rows in a column which is filtered by another column

Hi,

I have the data in the following format.

I need it sorted into this format.

The format is topic should be sorted with the highest values.

Once a topic comes in a row all the sub-topic should appear subsequently.

I want to limit the sub-topic to 3 or 5 of highest values.

Kindly help me how to get this scenario.

Thanks,

Varun