My logic is as simple as this. If it is not working for you, either your data is not what you think it is or the processing is erroneous. From my example, I am able to establish the processing seems to be correct. Let us then examine your data and what you are doing. If you can attach a sample data and your script, the forum may be able to help.