The Let FunctionPosted: June 13, 2012
I’ve rather avoided the Let function until now – until you use it, it seems to add a layer of complexity, and, as a novice, I’ve always liked keeping things simple.
But now I’m beginning to realise that simplicity is exactly what the Let statement offers. Not only does it enable you to make calculations more readable, but it greatly increases the efficiency of the calculation.
Here’s a good example.
Say I have a field called BKG::BKG_Cost, which is the cost of a Booking. The cost of the Booking is the total of the costs of the Resource Bookings within the Booking. The cost depends on whether the date of the first Session has passed – if the deadline has passed, we increase the cost by 15%.
So, one way of defining the field would be like this:
But if we use a Let statement to define the total booking cost, we end up with this:
The main gain here is that we reference the field PGM to SSN to RBK::RBK_Cost only once, and do the Sum only once, i.e. when defining the calculation variable “BookingTot”. In the first version, to apply the increase, we do that twice. Using the calculation variable also makes the whole thing more readable. And the more variables you use, the more readable it becomes. Here is is again, with a couple more variables (note the need, when defining more than one variable, to enclose the variable definitions in square brackets):