With these informations it's quite difficult to say what didn't worked like expected. Maybe it's easier to extract this within the script:
Num(Mid(Code, 3, 4)) as CodeYear
Are the other code-parts not numeric you could also use: keepchar(Code, '0123456789'). And then should be a min() or a firstsortedvalue() expression within the charts simpler.