Built-in Functions → sum
sum() is an aggregation function that returns the sum of the values for each group.
Signature
sum(double[] exp, groupBy)
The following table illustrates the function parameters:
Parameter | Description |
---|---|
double[] exp | expression evaluating to double |
groupBy | Grouping function that specifies which group of data to operate over |
Returns
double
representing the sum of values for each group.
Example
Consider the following table of example data:
Year Quarter Month Amount Sold2019 Q1 M1 1002019 Q1 M2 2002019 Q1 M3 1002019 Q2 M4 1502019 Q2 M5 2502019 Q2 M6 1502019 Q3 M7 2002019 Q3 M8 2502019 Q3 M9 3002019 Q4 M10 1002019 Q4 M11 502019 Q4 M12 502020 Q1 M1 1502020 Q1 M2 1002020 Q1 M3 2002020 Q2 M4 1502020 Q2 M5 1002020 Q2 M6 1502020 Q3 M7 502020 Q3 M8 1002020 Q3 M9 2002020 Q4 M10 1502020 Q4 M11 1002020 Q4 M12 50
Find the total amount sold for each quarter for each year.
SUM(Amount Sold, group by (Year, Quarter))
Use the following steps for detailed instructions on how to use the groupBy parameter:
Note
The data has been uploaded, then added and loaded into a Schema named Sales_Acme. For information on creating a data source and a schema, refer to the Guides→Start document.
- In the Navigation bar, select the Content tab, and then select + New → Add Dashboard.
- In the Add Dashboard dialog, for Name, enter Sales Dashboard, and then select Add.
- In the Action bar, select + (add icon).
- In the Insight panel, select Listing Table or V.
- In Tables, select Aggregated Table.
- In the Data panel, select Manage Dataset (+).
- In the Manage Data Sets panel, select Sales_Acme.
- From the Data panel, drag and drop the following fields to the Grouping Dimension tray:
- Year
- Quarter
- Month
- Drag and drop the Amount Sold column to the Measure tray.
- From the Data panel, drag and drop Add Formula to the Measure tray.
- The Formula Builder automatically opens:
- In Search Functions, select the down arrow, and then select Aggregation Functions.
- Double-click
sum(double[] exp, groupBy)
to add the formula to the editor. - In the Formula Editor, replace double[] exp with Amount_Sold and dimension, ... with Year,Quarter from the Data panel.
sum(Sales_Acme.Sales.Amount_Sold,groupBy(Sales_Acme.Sales.Year,Sales_Acme.Sales.Quarter))- Select Validate & Save.
- In the Measure tray, double-click the New Formula pill and rename it to Amount Sold Per Quarter.
- In the Action bar, select Save.
For more information, refer to the Level Based Measure document.