Skip to main content
You are here: Forms

Position-Based Calculations

Overview

Often times, calculations need to be performed based on a specific item in an array/list. Every list item, has a position, which is often referred to as an index. Mirata lets users reference different list item positions to perform different types of calculations.

Understanding "Zero-Based" Positioning

Typically when thinking about a list with multiple items, the first item would be considered to have position = 1. When working with lists in relation to code/software, list positions are "zero-based" which means that the first item in the list has position = 0. Another way to think about this is that each list item has position (n-1), where n = the visible position of the item in the list. See the below screenshot for a simple example of comparing zero-based positioning to non-code based lists.

Understanding "Indexes"

Another important concept to understand is an "Index" - an index is an integer value that captures the position of a list item within a list. This is important because in Mirata, users can create an integer field that 1) is tied to a list and 2) captures the position of a selected list item and can be used for calculations.

Position Options when Using Lists in Mirata

There are 4 different position options when considering calculations in relation to lists in Mirata:

  1. First

  2. Last

  3. Constant

  4. Calculation (usually will reference the Index field or current position)

Using First Position - Example

Assume there is a list, with one text field (Name) inside the list-item. Also assume the list will be initialized with 3 list items. This list, for demonstration purposes, will not be connected to any external data source. (See screenshots below for set-up)

Now assume that user wants to have a field outside of the list that captures the Name value from the first list item via an update calculation. There will need to be a text field that is outside the list that will receive this value. The important thing is that instead of referencing the field from inside the list, the user will need to edit the update calculation to understand that the only value that will be populated, is the value from the Name field in the first position.

In the update calculation, find the list in which the "Name" field is contained within. Next to the list, there will be a button that has 123 and three lines (see screenshot below). This button controls the position of the field(s) within a list. By default, this is unset (then button will be red), but since the first position is needed, the button needs to be clicked. After clicking the button, there will be a dropdown with the four position options. Select the option that says "first" and then immediately after that, select the field (Name) that will be referenced at the first position. The position button will change to blue, indicating that a position has been set.

Make sure to click "OK" to save the update calculation. Once the calculation is saved, navigate to Preview mode to test this out. Enter a name in each of the list items Name field, and watch as the Selected_Name field inherits the value of the first list item.

Using Last Position - Example

To get the value from the last item in the list, simply change the position dropdown from first to last in the position dropdown on the update calculation.

Navigate back to Preview and test this to make sure that the Selected_Name inherits the value of the last list item Name field.

Using Constant Position - Example

The constant position refers to the a specific Index position that will be referenced in the update calculation. If for some reason, the user always wanted to reference the second list item (Index = 1) then the constant value can be set to that specific index position.

Now, when navigating back to Preview, the Selected_Field will always be populated with the value from the Name field in the list item at Index = 1 (second position).

Using Calculation Position - Examples

The first example of using the calculation with a position will be for Current Position. This calculation essentially means that the Selected_Name field will populate with the value of the current position list item. I.e. the list item that the user is currently selecting. This is useful because it is very dynamic and responds to the users interaction with the list.

In order to make this function properly, a new integer field is needed to capture the position of the list. Bring in a new integer field and name/label it something like "List_Integer" and immediately after, connect this field to the list by selecting a value for the Selection Field property for the list.

To test that this is working, navigate to Preview mode and click into each of the list items. The List Index field should update to the (zero-based) position of whichever list item is selected.

Once this field is working properly, we can set our update calculation for the "Selected_Name" field so it dynamically pulls the Name value of whichever list item is selected. This calculation is now going to be slightly different from the past three. The previous calculations simply referenced a field at a specific position. Since there is an index connected to the list, that fields needs to have a value in order for the Selected_Name to properly reference the right value. Therefore, there needs to be an IF statement to check this and it will look something like this:

IF (has value(List Index), Name (at selected List Index), constant())

Here's how it looks in the calculation window:

Once this calculation is complete, navigate to Preview mode, type in a value for each Name field in each list item, and then click into each list item. Watch how the Selected_Name field updates to be whichever list item that is selected. The Index calculation at the top of the screen should correspond to the selected index.