Introduction
This article describes how to extract statistics from a table via a Rule that can then be used for further calculations.
Requirements
- A table with a Rule applied.
- This article assumes you are already in the Automate > Custom Rule or Edit Rule > Edit JavaScript Rule view specified in How to Use Rules in Q and How to Customize a Rule.
- You have read How to Create a Custom Rule.
Method
Referencing statistics
Q differentiates between statistics in cells, to the right and below a table. You can see these by right-clicking the table and going to Statistics - Cells/Right/Below. In Table JavaScript these are referenced separately as follows:
table.statistics
right_table.statistics
below_table.statistics
In each case, only the statistics that are currently being displayed in a table will be included.
If you wish to return a list of all statistics that can be computed from this table, you can instead append the availableStatistics
property to the appropriate table statistic location:
table.availableStatistics
right_table.availableStatistics
below_table.availableStatistics
Note, not all tables will have each of these types of statistics:
- A Pick One, Pick Any, Pick Any - Compact or Number - Multi summary table will only have cell and below statistics.
- A Pick One - Multi summary table will only have cell and right statistics.
- A crosstab, Pick Any - Grid or Number - Grid will include cell, right and below statistics.
- When you switch rows and columns, the right and below statistics will be reversed.
You can check whether right or below statistics exist by using the below functions which will return true or false:
belowTableExists()
rightTableExists()
Note, you must include includeWeb('Table JavaScript Utility Functions');
at the top of the Rule in this case as the underlying code for these functions only exists in this online library.
Referencing values from a statistic
You can return the values of a statistic by using the get
function:
var cell_vals = table.get("%");
var right_vals = right_table.get("Sum");
var below_vals = below_table.get("Average");
In this example, cell_vals
will, for example, "get" the statistic values for every cell in the table across all rows and columns. One thing to note here is that this function uses the original statistic name in Q.
If you want to have a more flexible Rule that uses the primary statistic, you can combine the get
function with the statistics
property:
table.get(table.statistics[0])
The format of the get
function is a two-dimensional array, where the rows are in the first index and the columns in the second index. So an array of cell statistics in a table might look like this:
[[col1-row1, col2-row1, ...],[col1-row2, col2-row2, ...]]
Using thearray[row index][column index]
reference format, vals[0][0]
in this case would be the first table cell, that is col1-row1 for the array.
Note that for one-dimensional SUMMARY tables, a two-dimensional array will still be returned, with only one column in the second dimension. The value type will be based on whether the returning statistic is text or numeric.
Next
How to Create a Custom Statistic or Calculation via a Rule
Later
How to Modify Table Rows and Columns Using a Rule
How to Modify Table Cells Using a Rule
How to Create User Input Fields in a Rule
How to Add Table Validations to Rules
How to Reference and Calculate Tables in a Rule
How to Add Table Spans Using a Rule
How to Add a Custom Table Footnote
See Also