I did it!
Moodle 3.6 introduced the IF expression, I was using Moodle 3.4 and saw the 3.6 documentation which had the IF expression.
This is the formula I used:
=if(4>1,((average(1,2,3)+4)/2),(average(1,2,3)))
Where 1, 2 and 3 are the normal grade items for all students and 4 is the final exam.
This way, those students who received a grade in the final exam will have a specific formula for their final grade: (average(1,2,3)+4)/2.
And normal students will have their own formula as well: average(1,2,3)