Calculation field

From Memento Database Wiki
Revision as of 16:43, 6 September 2016 by UnConnoisseur (talk | contribs)
Jump to navigation Jump to search
« Page as of 2016-08-30, editions Mobile 3.9.7, Desktop 1.0.3 »

The values in Calculation fields are calculated from the expressions specified by you in Memento Script, defined herein.

The following types of expressions are supported:

Numeric
Expression involving numbers.
String
Strings can also be added together, compared, etc...
Boolean
Expressions that evaluate to true (1.0) and false (0.0).

Numeric and string types can not be mixed in a left / right operand pair.

Variables

Variables other fields in the same entry.

Variables must be enclosed by a hash mark and open brace #{ and a closed brace }. e.g. expression:

#{field name 1} + #{field name 2}

String values/variables must be enclosed by single-quote (apostrophe) characters:

'Result: ' + '#{string field 1}' + '#{string field 2}'

Operators

The following operators are supported:

  • ( open parentheses
  • ) closed parentheses
  • + addition (for numbers and strings)
  • - subtraction
  • * multiplication
  • / division
  • % modulus
  • == equal (for numbers and strings)
  • != not equal (for numbers and strings)
  • < less than (for numbers and strings)
  • <= less than or equal (for numbers and strings)
  • > greater than (for numbers and strings)
  • >= greater than or equal (for numbers and strings)
  • && boolean and
  • || boolean or
  • ! boolean not

Result type

For each calculation, there must be a correct result type set:

  • Real result and Integer result are used for expressions with a numerical result.
  • Date/Time result is used to represent the result in the form of date and time. The calculation result is interpreted as the number of seconds that has passed since January 1, 1970 (UNIX time).
  • Date result is the same as the previous type, but displays only the date.
  • String result is used for expressions that contain string manipulations or functions that return a string.

Link to Entry

Use the following syntax to get access to the values of Link to Entry and other linking fields:

#{field name.child field name}

where field name is the name of a field like Link to the entry, child field name – name of one field of a related entry.

If a link field contains several links, then use an index (starting from zero) to get access to them:

#{field name@0.child field name}, #{field name@1.child field name}, 

You can specify one of the supported aggregation functions instead of the index:

#{field name@sum.child field name}

This expression returns the sum of values of field child field name for all entries included into field name. In addition, the following functions are supported: avg, min, max.

Use the following syntax to get the number of entries in field field name:

#{field name@size}

Examples

Example: Sum

Entries contain fields: Price, Count

We add a Calculation field Sum by using the following expression:

#{Price}*#{Count}

Example: Percentage

Entries contain fields: Count, Total

We add a Calculation field Percentage by using the following expression:

(#{Count } / #{ Total }) * 100

Example: Duration

Entries contain fields: StartDateTime, EndDateTime
We add a Calculation field Duration by using the following expression (result is number of seconds):

#{EndDateTime} - #{StartDateTime}

Example: Days

Entries contain fields: StartDate, EndDate

We add a Calculation field Days by using the following expression:

datediff(#{EndDate}, #{StartDate})

Example: Days left

Entries contain fields: Time

We add a Calculation field Days Left by using the following expression:

if( #{Time} > now() , rint((#{Time} - now())/(60*60*24)) , 'in the past' )

Example: References

There’s library Order containing orders and library Item containing items. Library Order contains a link to library Item and a numeric field Count. Library Item contains field Price.

To calculate the cost of the order, let’s add Calculation field Sum using the following formula:

#{count} * #{item.price}

If an order can make a reference to several different items, their total cost can be calculated:

#{item@sum.price}

The most expensive item:

#{item@max.price}

The cheapest item:

#{item@min.price}

The average price for the item:

#{item@avg.price}
Name
Textual, case-insensitive field identifier that can include spaces; it's best to avoid characters that can be used as operators, though (standard across all field types)
Hint
Textual, case-insensitive field identifier that can include spaces

Advanced Parameters

Thousands separators
If the chosen result type is Integer or Real, use spaces as separators to break the value into thousands. By default, it will not.

Display Options

Display in the list as
Select among the following roles for the current field in the entries list:
  • As a Regular field
  • As an Entry Name
  • As an Entry Description
  • As an Entry Status
Use in Calendar as
None / Duration, mins / Duration, hours
The field name
Display the name of the field in the card entry (By default, On). By this, it is meant that the field name will or will not be displayed on either the Entry View card or the Entry Edit card.
Font
Choices are:
  • Family: Regular, Light, Condensed, Thin, Medium. By default, Regular.
  • Style: Normal, Bold, Italic, Bold Italic. By default, Normal.
  • Size: By default, 18. Direct entry to change.
  • Color: By default, White. Palette of 20 colors, backed up by a continuously variable color chooser.
Dependencies
Ability to add one or more visibility dependencies upon fields with qualifying types.