Basically give every row a unique counter value
Do a group by on entire dataset, grouping by all your grouping columns
For the “groupby” output table, create a new constant value column called “Counter” that has a very high value of say 99999999
Concatenate groupby output with original data set. Sort by the groupings and Counter to assemble rows in desired order, and after that tidy up your data set as you require (e.g. remove the Counter column, or use the “99999999” counter value to identify your “group totals” rows and do something with them, like blanking out specific columns or whatever.