Dynamic Grouping

I have a table with several columns and one amount.

I have a dropdown with the 3 text columns as choices (area,function, use) and a repeating group that will show the amounts grouped by the column selected in th dropdown

I am trying to find a way to do the grouping dynámically. The real application will have more than 3 columns which makes the workaround ( 3 repeating groups) unfeasible.

I think this can be done with the js toolbox, but I don’t have clear how to access the result of the query ( in a private state) or how to populate the result variable .