Indeed. In your chart, the max() function aggregates from a field that contains a range of values.
In a LOAD statement, there is only one value in the current record and aggregation functions cannot be used without a GROUP BY clause.
Put the ContractEndDate in a variable, like Michael suggests. Or if you have multiple ContractEndDates, create a mapping table with for each contract a single maximum ContractEndDate. Example code to create this table and that should come before you use applymap():
MAPPING LOAD [Contract Nr], max(ContractEndDate)
RESIDENT / FROM datasource
GROUP BY [Contract Nr];
Code that replaces the max() function in your LOAD statement:
Do you use variables or fieldnames in these IF statements? Then make sure they exists at the moment of evaluation.
Are these IFs placed in a LOAD statement (should work with chart expression IFs as they are the same functions) or on the outer level of your script (doesn't work because those IFs are control statements with a different syntax).