Example: Transform Field Values using an Expression

This is a follow on topic, continuing onward from the preceding example of Copy one Column into Another Column using Transform and using the same data.   We continue using the Expressions template in the  Transform pane to change the values of fields.   We show an example changing the price of selected products.

 

We continue from the previous example.

 

 

Recall that we had two products selected in the table.   If the Transform pane is not in sight we launch the Transform pane.

 

With the focus on the table window, in the Transform pane we choose the Unit Price field, and then we double-click the Expressions template to launch it in the Transform dialog.

 

 

Check the Transform selection only box, to restrict application of the transform to only selected records.   

 

 

When working with selections, it is a good idea to begin by checking the Transform selection only box before specifying other parameters, in case we forget to do that later.

 

Press the Edit Expression button to launch the expression builder dialog.

 

 

In the lower pane of the expression builder dialog, we double-click on the Unit Price field in the table scheme to automatically add the field name to the expression pane.   In the upper pane we continue to write the expression by manually keyboarding the text * 1.1 to form the expression:

 

[Unit Price] * 1.1

 

That means we are telling Manifold to multiply the value of the Unit Price field by 1.1.  

 

Press OK to return to the Transform pane.

 

 

In the Transform pane, for the Result destination we leave the default Same Field choice in place.  That will write the result of the computation back into the Unit Price field, updating it in place.

 

Since we have checked the Transform selection only box, only selected records will be altered.

 

Press Preview to see a preview.

 

 

A blue preview column appears, with the column head caption giving the name of the template being previewed.  The black dot in the Unit Price column head shows that is where the results will go.  We can drag the blue preview column to a different position in the table, or change the width of the column by dragging the sides of the preview column.  

 

We can see from the preview column that digital floating point arithmetic done to a float64 accuracy is giving us more digits than we might want.   The many zeros in the lower value indicate that somewhere far to the right there is a nonzero digit.   We can eliminate those many zeros by using the RoundDecs function in the expression to round the result to at most two decimal digits.

 

 

In the Transform pane we press the Edit Expression button, and then in the expression dialog that pops open we alter the expression to be:

 

RoundDecs([Unit Price] * 1.1, 2)

 

We press OK in the expression dialog to get back to the Transform pane.    Press Preview again.

 

 

That is better.   Rounding the result to at most two decimal digits results in nothing but zeros after the digits seen above, so those extra zeros are not shown.

 

Press Transform.

 

 

The table immediately updates to multiply the Unit Price values for selected records by 1.1,  increasing the unit price by 10% for the two selected records.

Example: Transform Field Values using Multiply

In the prior example we used the Expression template.  In this example we will use the Arithmetic : multiply transform operation to accomplish the same task, increasing prices for selected records.  

 

Using Ctrl-Click we select two records in the Products table,   We will increase the Unit Price for these two of Chef Anton's products   The Unit Price is 24.2 for the first product and 21.35 for the second product.  

 

We will use the Transform pane to modify the Unit Price field.

 

 

With the focus on the Products table window, in the Transform pane we choose the Unit Price field, and then we double-click the Arithmetic template to launch it in the Transform dialog.

 

 

Check the Transform selection only box, to restrict application of the transform to only selected records.   

 

 

When working with selections, it is a good idea to begin by checking the Transform selection only box before specifying other parameters, in case we forget to do that later.

 

In the Arithmetic template, we choose multiply (*) as the Operation.   We enter 1.15 in the Value box.

 

For the Result destination we leave the default Same Field choice in place.  That will write the result of the computation back into the Unit Price field, updating it in place.

 

Since we have checked the Transform selection only box, only selected records will be altered.

 

Press Preview to see a preview.

 

 

In the illustration above we have dragged the blue preview column next to the Unit Price column, to make it easier to compare the results of the template with the column that it will modify.    The preview column is drawn above all other columns, so it covers the column below.

 

Press Transform.

 

 

The table immediately updates to multiply the Unit Price values for selected records by 1.15,  increasing the unit price by 15% for the two selected records.    Unlike the prior example, where we used an expression, the new values use full floating point arithmetic to as many decimal digits as the computation generates.   If we want to truncate the values to only two decimal digits we can use the Round template.

 

 In the Transform pane we press the Up button to move up one level, so we can pick a different template.  

 

In the Transform pane, we double-click the Round template to launch it.

 

 

In the Round template, we choose to nearest (round) as the Round option, and 0.01 as the Round to option.   Since we want to round all values in the Unit Price field in the table, we do not  check the Transform selection only box.

 

Press Preview to see a preview.

 

 

The preview shows how all Unit Price values in the table will be rounded to only two decimal digits.

 

Press Transform.

 

 

That applies the template, to round Unit Price values to two decimal digits throughout the table.

 

See Also

Selection

 

Select Pane

 

Transform Pane

 

Command Window

 

Queries

 

Example: Two Drawings from the Same Table - Take a table with a geom field that is visualized by a drawing.  Add a second geom field to the table and create an rtree index on that field so it can be visualized by a drawing.   Copy the first drawing, paste it and adjust the pasted copy so it uses the second geom field.

 

Example: Construct JSON String using Select and Transform - Use the Select pane and the Transform pane to manually construct a JSON string using values from other fields in a table. Shows how we can manipulate text to build desired contents in a field.

 

Example: Copy one Column into Another Column with Transform - How to use the Transform pane to copy the contents of one column in a table into another column, but only for selected records.  Uses the Products table from the Nwind example data set.  

 

Example: Use a Transform Expression to Create Buffers in a Drawing - Use the Expression template in the Transform pane to create three different sizes of buffers for different lines in a drawing and then automatically create a query which does the same thing.  Includes examples of saving results to a new component and also the Edit Query button.

 

Example: Clip Areas with a Transform Expression - Use the Expression template in the Transform pane to clip areas in a drawing to fit within horizontal bounds.   Includes examples of saving results to a new component and also the Edit Query button.

 

Example: Simplify Lines with a Transform Expression - Use the Expression template in the Transform pane to make lines smoother in a drawing so that longer lines are smoothed more.  Includes examples of using the Edit Query button to show how different queries are created automatically depending on if we want to update a field or to add a new component.

 

Example: Transform Templates, Expressions and Queries - We learn to use a function by clicking on a template in the Transform pane, seeing what it does in a preview, looking at the query Manifold creates and then trying out the function in the Expression tab.