There are at least two things that doesn't make much sense in the logic:
The first condition and the second condition will always be satisfied since they are opposite conditions. One of the two will always be true so none of the conditions after the two will ever be tested at all.
What will be calculated by any of the first two conditions will be exactly the same since the expression after the condition is the same for both.
My guess without knowing what you want to calculate here is that the second line overstriked in red should be removed completely.
as per the condition(except for KEY EMS missing all the conditions are going wrong) written whenever removal flag is there instead of getting 0 i am getting continuous number: Image for your reference:
But when i change the order by i am getting write numbers for all the conditions except for Key ems missing.
order by Equipment,[Part Group], Key_EMS,Calendar_Month_Code; Below image for reference:
I am attaching the file for reference.
Can you please help me with this and change the code accordingly.