# List and Math Operations

Beyond the filtering of data that flows into a page, you might also want to perform a list operation or a mathematical calculation on that data. The Expression widget in iRise gives you this capability.

In this tutorial, you will set up three different operations using Expression widgets. First, you will use list operations to sort and provide a count of the results from a data query. Then you will build a converter function that will switch product weights from ounces to grams.

## Watch a Video

Watch this video for a brief demonstration of the topics covered in this tutorial.

## What is an Expression widget?

An Expression widget takes the output of a Record widget and performs a function on that data. Since the output is typically in the form of an array, list operations such as SUM, COUNT, AVG, MIN and MAX can be performed on the data, as well as sorting operations. You can also use the Expression Builder to create free form mathematical calculations.

After you use the Expression widget to perform the list or math operation on the data input, you will typically send the output to the page. This output might be used to alter the display somehow (e.g., with a Sort Ascending list operation), or it might be displayed on the page.

You will gain some experience with both of these output types in the tutorial that follows.

Note: This tutorial picks up where the previous tutorial left off. Although completing that tutorial before starting this one would be ideal, you can adapt the methods used here to any data table or Tile List that you’ve created in iRise.

## Counting the number of records returned by a Record action

We’ll start out with the Product List page we created in the previous tutorial. The product search results on this page present a good opportunity to add a number of enhancements involving Expression widgets. First, we’ll display the total number of records returned after the product filters are applied.

Add an Expression widget to the canvas on the left side of the page. In the Expression widget’s Properties panel, choose ‘Count’ in the List Operation field. Now drag the Get PRODUCTS widget and release it on the Expression widget, choosing Send Data in the pop-up menu. In the Select a Field window, choose ‘Price’ and click OK.

Now double-click the Expression widget. When the Expression Builder opens, click the Price button on the right side of the window to add the field to the calculation (this is all we need, since we are performing a list operation rather than a calculation). Change the Precision to 0 so no decimal places appear in the output. Then click OK.

To display the Expression’s output, we’ll add a Text widget and a Text Input widget above the Tile List. The Text widget should contain the text “Matches found:” The Text Input widget should be positioned just to the right of the Text widget, and should have a width of 2 characters. Now drag the Expression widget and release it on the Text Input widget. Type Matches in the Select a Field window and click OK.

In this example, you could choose any field from the PRODUCTS datasheet to use as input for the Expression, since all the Expression is doing is counting the number of records flowing into the Tile List.

Test the Expression by launching the page in your browser. After you make a selection from the Type (Category) filter, the integer displayed in the Text Input widget should match the number of records displayed in your Tile List. As you apply different filters, the output of the Expression widget should continue to match the number of records displayed in the Tile List.

## Converting product weights from ounces to grams

The product weights displayed in our Tile List are currently in ounces. Let’s give our reviewers the ability to convert those values from ounces to grams (for each record) with a single mouse click. To do this, we’ll combine an Expression widget with alternate views and a Switch Views action.

To the right of the [weight] placeholder in the Tile, add another Text widget containing the text “Convert to grams”. Wrap both Text widgets in a Section, and then create an alternate view by right-clicking the Section and choosing Add View > Duplicate View.

In the Views panel, name the dynamic display “Ounces/grams conversion” and name the views “Ounces” and “Grams”. Make sure the Ounces view is set as the default view. With the Grams view displayed on the page, delete the “Convert to grams” Text widget.

To switch from the Ounces view to the Grams view, we’ll connect a Switch Views action to the ‘Convert to grams’ Text widget.

Before we can map data to the alternate view, we need to perform the conversion. Add a third Expression widget below the other two. Drag the Get PRODUCTS widget and release it on the new Expression widget, choosing Send Data when prompted. In the Select a Field window, select ‘Weight’ and click OK.

The conversion factor for calculating grams from ounces is 28.3495 (i.e., there are 28.3495 grams in one ounce). Double-click the Expression widget. Add the Weight field to the calculation and then multiply that by the conversion factor. The final calculation will be 28.3495*Weight. Change the Precision to 1 and click OK.

### TIP

You can test the conversion by typing a number in the field to the right of the Weight button and then clicking the TEST button.

Finally, drag the Expression widget and release it on the [weight] placeholder in the Tile, making sure the Grams view is displayed. In the Select a Field window, select ‘Weight’ and click OK.

Test the conversion by launching the page in your browser. Make a selection in the Type (Category) filter, and then click the ‘Convert to grams’ link in any of the displayed records. The weight in ounces should be replaced by the weight in grams.

## Conclusion

Expression widgets are extremely versatile tools for adding data fidelity to your simulations. While communicating your vision to your stakeholders will not always require this level of fidelity, what you’ve accomplished in this tutorial should help prepare you for those situations when high fidelity is a must.