Hello,
Apart from the code, would you also be able to provide a minimal dataset so we can run the code and actually try and fix it?
You can find help in the article below how to do this:
Thanks, PJ