Documentation Docs
Documentation Docs

How to Use Column Totals in Matrix Questions

About Data Aggregation in Matrix Columns

SurveyJS Form Builder allows you to combine data from different rows of a matrix column by using a set of properties under the Totals category. For example, if you have a matrix question that asks a user to specify dates of visit, you might aggregate the "Number of days" column to find the total number of days spent abroad, the average duration of a trip, or the maximum and minimum number of days spent away.

Dynamic Matrix: How to aggregate data in a column

Supported Question Types

You can aggregate data across rows for a specific column in the following question types:

Data Aggregation Methods

The following data aggregation methods are supported:

  • Sum - Adds up all the values in a column to get the total.
  • Count - Counts the number of entries in a column.
  • Min - Finds the smallest value in a column.
  • Max - Finds the largest value in a column.
  • Avg - Calculates the mean value of the data in a column.
Dynamic Matrix: data aggregation methods

How to Add a Total to a Matrix Column

In this example, we create a Dynamic Matrix and populate one of its columns with choices selected in a preceding Multi-Select Dropdown question. For information on how to pipe choices to a Dynamic Matrix column, refer to the following guide:

How to Pipe Selected Choices to a Dynamic Matrix

To calculate the duration of each trip in days, we use the dateDiff() function, which you can learn more about in the dedicated guide:

How to Calculate Duration Between Dates and Prefill a Form Field with the Duration Value

To combine data from different rows within a Dynamic Matrix column, follow the steps below:

  1. Add a Dynamic Matrix to a form.
  2. Select the matrix column whose data you want to aggregate by clicking the area around the column title.
  3. Under General, locate the Cell input type property and set it to Expression (read-only).
  4. Locate the Expression field and enter the following value: dateDiff({row.from}, {row.to}, 'days'), where from is the column name (ID) of the start date and to is the column name (ID) of the end date of a visit.

    Dynamic Matrix: How to use the duration function to calculate the number of days between two dates
  5. Under Totals, locate the Aggregation method property and select the desired method.
  6. Locate the Formatted string property and enter the text format for the column total, where {0} will be replaced with the actual numerical value.

    Dynamic Matrix: How to add the Total value for a column
  7. (Optional) Modify the format type using the Total value display format property. For example, if you are aggregating monetary values, set the property to Currency and select the desired currency type using the Currency drop-down menu.

    Dynamic Matrix: How to set the currency for the total value of a matrix column

The resulting value will look as follows:

Dynamic Matrix: How to sum up values within a column and add a currency sign to the total

How to Sum Numerical Values Across Matrix Columns

In the previous example, we calculated the duration between two dates. If your matrix columns contain numerical values that you want to sum and display, use the sum() function instead of dateDiff() in step 4: sum({row.columnOneName}, {row.columnTwoName}). The rest of the steps are the same.

Limitations

Multi-Select Matrix columns do not support data formatting properties and only allow selection of an aggregation method.

Send feedback to the SurveyJS team

Need help? Visit our support page

Copyright © 2024 Devsoft Baltic OÜ. All rights reserved.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.