The IF statement is a T-SQL control flow statement, and is not meant to be embedded within SQL statements. It looks like you're trying to build a different derived table, depending on the IF evaluation, which is not possible or legal T-SQL syntax.
Declare a table type variable.
Use the IF to determine what to populate the table type variable with.
Join the table type variable to your main query instead of the conditonal derived table.