I am trying to write an efficient function to use in a calculated field which has the following characteristics