When I apply the code at my database I think it is almost correct. When I use a filter on the column with 1 I get the first but sometimes I see a different number (2 till 6). When I filter on the reference that is the only result.
I think that I get the result I want when I filter out the blank results but why generates the higher numbers?
I'll validate a bit more to see what exactly is happening.
Edit: I get it now. On the exact date and time there a 3 results in the database. It generates a 3 because it's number 3 of the reference numbers. But that is okay and no problem.
I'll do the validating but it seems alright. I'll mark your answer as the solution!