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.
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.
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:
- Add a Dynamic Matrix to a form.
- Select the matrix column whose data you want to aggregate by clicking the area around the column title.
- Under General, locate the Cell input type property and set it to Expression (read-only).
- Locate the Expression field and enter the following value:
dateDiff({row.from}, {row.to}, 'days')
, wherefrom
is the column name (ID) of the start date andto
is the column name (ID) of the end date of a visit.
- Under Totals, locate the Aggregation method property and select the desired method.
- Locate the Formatted string property and enter the text format for the column total, where
{0}
will be replaced with the actual numerical value.
- (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.
The resulting value will look as follows:
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.