Thanks for your answer - is working fine but when you I type in k30 code first time and the the level will be null, previus() function will return previus value of level but I will expect null because there was no last value for k30.
Sure you can workaround this problem. In the if-statement, you can check whether the previous value of the field Code equals the current value of Code. If not, then return NULL, else return the value you want.
The updated code will be like this:
//here the additional check...
//return NULL if previous code does NOT have the same value as the current CODE