Would you mind posting a small excel spreadsheet which contains the absolute minimum number of "fake" columns and rows to illustrate the issue? It's a little difficult to understand what you're trying to do and what isn't working for you.
A combination of a spreadsheet and an example of "the way you think it should behave" vs. the "way it is behaving" would help folks answer your question.
FYI, it's also not necessary for you to use Custom SQL to do this filtering work. Take a look at the "Context Filter" help topic - it will allow you to do the same thing. You could create a simple expression which duplicates your logic above and then add that as a context filter so that only "true" rows come back to Tableau.
Thank you for your response. I shall try the context filter and see if this works. If I am still having a problem I will post the further detail as your suggested.
The context filter may very well work.