I have had a look at your attachment just now and I'm afraid I don't see any sample data showing your expected outcomes, and I don't actually understand the requirement, so without sample results, no, I can't help I'm afraid.