# The Case function in a calculation

**Posted:**September 11, 2013

**Filed under:**Calculations, Field definition, Technique explanations, Variables |

**Tags:**auto-enter calc, calc field, calculation variable, case function, field definition, Filemaker, techniques, tips, variable 2 Comments

A recent post on the TechNet forum, plus a succession of typically helpful answers, provided a very useful tutorial in how best to use the Case function to calculate a marked up cost, with the markup being based on the value of Cost.

Here’s the original formula. “Why won’t it work?” was the cry for help:

Case (

Cost < “5” ; Cost * “4” ;

Cost > “5.01” and Cost < “15” ; Cost * “3.5” ;

Cost > “14.99” and Cost < “25” ; Cost * “3” ;

Cost > “24.99” and Cost < “40” ; Cost * “2.75” ;

Cost > “39.99” and Cost < “60” ; Cost * “2.5” ;

Cost > “59.99” and Cost < “100” ; Cost * “2.25” ;

Cost > “99.99” and Cost < “200” ; Cost * “2” ;

Cost > “199”; Cost * “1.6”)

Well, with the field “Cost” defined as a number, and the calculation result defined as a number, the first step is to remove the quotes, which makes Filemaker see the numeric values as numbers, rather than as text.

So, V2:

Case (

Cost < 5 ; Cost *4 ;

Cost > 5.01 and Cost < 15 ; Cost * 3.5 ;

Cost > 14.99 and Cost < 25 ; Cost *3 ;

Cost > 24.99 and Cost < 40 ; Cost * 2.75 ;

Cost > 39.99 and Cost < 60 ; Cost * 2.5 ;

Cost > 59.99 and Cost < 100 ; Cost * 2.25 ;

Cost > 99.99 and Cost < 200 ; Cost * 2 ;

Cost > 199; Cost * 1.6)

This will now work, but it’s making much harder work of the calc than it needs to. Filemaker only works through the Case statement until it meets a true condition, then it bails out – there’s therefore no need for all of these > and < evaluations.

So, enter V3:

Case (

Cost < 5 ; Cost * 4 ;

Cost < 15 ; Cost * 3.5 ;

Cost < 25 ; Cost * 3 ;

Cost < 40 ; Cost * 2.75 ;

Cost < 60 ; Cost * 2.5 ;

Cost < 100 ; Cost * 2.25 ;

Cost < 200 ; Cost * 2 ;

Cost * 1.6)

(Note the final default value, 1.6, which applies to any value of Cost equal to or greater than 200 – no need for an evaluation there; if we get that far, that’s the value to use.)

The next efficiency is to use calculation variables, for the value of Cost, and the markup to be applied.

So, finally, V4:

Let ( [

$Cost = Cost ;

$Markup = Case (

$Cost < 5 ; 4 ;

$Cost < 15 ; 3.5 ;

$Cost < 25 ; 3 ;

$Cost < 40 ; 2.75 ;

$Cost < 60 ; 2.5 ;

$Cost < 100 ; 2.25 ;

$Cost < 200 ; 2 ;1.6)];

$Cost * $Markup)

But then came another interesting point, i.e. whether the Cost field itself is the right place for the calculation to happen. In the case of a “net price” such as this, which may need to be overridden, the better way is to have a separate Markup field, defined as an auto-enter calc, with the initial value of the markup amount calculated as above. The (new) Price field is then defined as a simple calc field, i.e. Price = Markup * Cost. On the Markup fields, the option “Do not replace existing value of field (if any)” should be ticked, to prevent historical and “override” data being replaced.

For a more comprehensive look at calc and auto-enter fields, see here. Thanks, as ever, to the Technet contributors who combine to make it such a great resource.

# Keeping Text Fields Tidy With Custom Functions

**Posted:**August 12, 2013

**Filed under:**Calculations, Functions, Interface, Technique explanations |

**Tags:**auto-enter calc, custom function, field definition, Filemaker, techniques, tips Leave a comment

Having said in the previous post that I’m trying to use FileMaker’s own feature set wherever possible, there are some areas where it just ain’t good enough.

One such situation is in the formatting of text data entered by the user, especially name and address details. For instance, if you want all names to held in “proper” case (e.g. “Jane Smythe”, rather than “JANE smythe”, “jane Smythe” or any other permutation), the “Proper” calculation function is available – you just need to make your field an auto-enter calc, with the calculation defined as “*Proper (Self)*“.

But this is very limited, and can actually *create* mistakes in data that has been correctly entered. For example, if your user has carefully entered “John O’Shea”, she’ll be cheesed off when FileMaker changes it to “John O’shea”. Likewise “Mary Macdonald”, and “Bbc”.

For a more thorough treatment, I’ve had to look beyond FileMaker to a couple of Custom Functions, both found on Brian Dunning’s excellent site. The CFs are:

- Title, by Howard Schlossberg, which does a great job of putting capital letters where they should be, leaving them as caps where they should be, and making them lower case where appropriate. It’s also quite adaptable, so you can add exceptions to be treated differently, etc. It looks like Howard created it to format titles (of albums, books, etc.) properly, but it works just as well on names of people, organisations, etc.
- SuperTrim, by Debi Fuchs, which removes leading and trailing spaces, tabs and carriage returns from the entered text. Again, like FM’s native function (in this case, “Trim”), but better.

So, once the CFs are added to the application, it’s a straightforward job to define all appropriate text fields as auto-enter calcs with this calculation behind them:

*SuperTrim (Title ( Self; “”))*

# The Let Function

**Posted:**June 13, 2012

**Filed under:**Calculations, Field definition, Functions, Variables |

**Tags:**calc field, calculation variable, Filemaker, tips, variable 1 Comment

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):