Initially we need to sort the pivot data before use it when  grouping