In your "catchall" else condition "ERROR" is a string and Tableau has already established that you're going to return an integer all the time. Change "ERROR" to NULL or something else that shows an error and it will work