Fix bug on distance marker grouping

## Summary
Previous fix for distance marker ordering did not take the `where` key on distances into account; now we do.

## Change
- Split calculation in two steps, grouping first on `where` and next on `tg`.