Add Aggregate Formula

Add a aggregate formula in the specified table.

REQUEST URI

https://<ZohoAnalytics_Server_URI>/restapi/v2/workspaces/<workspace-id>/views/<view-id>/aggregateformulas

Post

oauthscope: ZohoAnalytics.modeling.create

QUERY PARAMETERS

Parameter NameDescription
CONFIG*JSONObject

Config parameter specifications are available in the below section.

FIELDS FOR CONFIG JSON

KeyDescription
expression*String

Formula expression.
formulaName*String

Name of the formula column to be created.
descriptionString

Description of the formula column.

POSSIBLE ERROR CODES

7103 , 7301 , 7115 , 8083 , 7168 , 8507 , 8078

Sample Request:

Copiedcurl https://analyticsapi.zoho.com/restapi/v2/workspaces/<workspace-id>/views/<view-id>/aggregateformulas --data-urlencode 'CONFIG={"formulaName":"<formula-name>","expression":"<formula-expression>"}' 
-X 'POST' 
-H 'ZANALYTICS-ORGID: <org-id>' 
-H 'Authorization: Zoho-oauthtoken <access_token>'
Copiedusing System;
using System.Collections.Generic;
using ZohoAnalytics;

namespace ZohoAnalyticsTest
{
    class Program
    {
        long orgId = 55522777;
        long workspaceId = 35130000001055707;
        long viewId = 35730000007354002;

        public void AddAggregateFormula(IAnalyticsClient ac)
        {
            string formulaName = "TotalCost";
            string expression = "sum(\"Cost\")";
            IViewAPI view = ac.GetViewInstance(orgId, workspaceId, viewId);
            long formulaId = view.AddAggregateFormula(formulaName, expression, null);
            Console.WriteLine(formulaId);
        }

        static void Main(string[] args)
        {
            string clientId = "1000.xxxxxxx";
            string clientSecret = "xxxxxxx";
            string refreshToken = "1000.xxxxxxx.xxxxxxx";

            try
            {
                IAnalyticsClient ac = new AnalyticsClient(clientId, clientSecret, refreshToken);
                Program obj = new Program();
                obj.AddAggregateFormula(ac);
            }
            catch (ServerException ex)
            {
                Console.WriteLine("Server exception - " + ex.GetErrorMessage());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Other exception - " + ex.Message);
            }
        }
    }
}
Copiedpackage main

import (
    "fmt"
    ZAnalytics "zoho/pkg/analyticsclient"
)

var(
    clientId = "1000.xxxxxxx"
    clientSecret = "xxxxxxx"
    refreshToken = "1000.xxxxxxx.xxxxxxx"

    orgId = "55522777"
    workspaceId = "35130000001055707"
    viewId = "35730000007354002"
)

func AddAggregateFormula(ac ZAnalytics.Client) {
    config := map[string]interface{}{}
    formulaname := "NewFormula"
    expression := "sum(\"Cost\")"
    view := ZAnalytics.GetViewInstance(&ac, orgId, workspaceId, viewId)
    result, exception := view.AddAggregateFormula(formulaname, expression, config)
    if(exception != nil){
        fmt.Println(exception.ErrorMessage)
    }else{
        fmt.Println(result)
    }
}

func main() {

    ac := ZAnalytics.GetAnalyticsClient(clientId, clientSecret, refreshToken)
    AddAggregateFormula(ac)

}
Copiedimport com.zoho.analytics.client.*;
import org.json.*;

public class Test {

    private long orgId = 55522777l;
    private long workspaceId = 35130000001055707l;
    private long viewId = 35730000007354002l;

    public static void main(String args[]){

        String clientId = "1000.xxxxxxx";
        String clientSecret = "xxxxxxx";
        String refreshToken = "1000.xxxxxxx.xxxxxxx";

        Test tObj = new Test();
        AnalyticsClient ac = new AnalyticsClient(clientId, clientSecret, refreshToken);

        try {
            tObj.addAggregateFormula(ac);
        }
        catch (ServerException ex) {
            System.out.println("Server exception - ErrorCode : " + ex.getErrorCode() + ", ErrorMessage : "  + ex.getErrorMessage());
        }
        catch (ParseException ex) {
            System.out.println("Parser exception - ErrorMessage : "  + ex.getResponseMessage());
        }
        catch (Exception ex) {
            System.out.println("Other exception - ");
            ex.printStackTrace();
        }
    }

    public void addAggregateFormula(AnalyticsClient ac) throws Exception {
        String formulaName = "Sum_Cost";
        String expression = "sum(\"Sales\".\"Cost\")";
        ViewAPI view = ac.getViewInstance(orgId, workspaceId, viewId);
        long result = view.addAggregateFormula(formulaName, expression, null);
        System.out.println(result);
    }
}
Copied<?php

    require 'AnalyticsClient.php';

    class Test
    {
        public $ac = NULL;
        public $client_id = "1000.xxxxxxx";
        public $client_secret = "xxxxxxx";
        public $refresh_token = "1000.xxxxxxx.xxxxxxx";

        public $org_id = "55522777";
        public $workspace_id = "35130000001055707";
        public $view_id = "35730000007354002";

        function __construct() {
            $this->ac =  new AnalyticsClient($this->client_id, $this->client_secret, $this->refresh_token);
        }

        function addAggregateFormula() {
            $formula_name = "Formula 1";
            $expression = "sum(\"Cost\")";
            $view = $this->ac->getViewInstance($this->org_id, $this->workspace_id, $this->view_id);
            $response = $view->addAggregateFormula($formula_name, $expression);
            print_r($response);
        }
    }

    $test_obj = new Test();

    try {
        $test_obj->addAggregateFormula();
    }
    catch(ServerException $se) {
        echo "Server exception : " . $se->getErrorMessage() . "\n";
    }
    catch(IOException $ioe) {
        echo "IO exception : " . $ioe->getErrorMessage() . "\n";
    }
    catch(ParseException $pe) {
        echo "Parser exception : " . $pe->getErrorMessage() . "\n";
    }
    catch(Exception $e) {
        echo "Exception : " . $e->getErrorMessage() . "\n";
    }
?>
Copiedfrom __future__ import with_statement
from AnalyticsClient import AnalyticsClient
import sys
import json

class Config:

    CLIENTID = "1000.xxxxxxx";
    CLIENTSECRET = "xxxxxxx";
    REFRESHTOKEN = "1000.xxxxxxx.xxxxxxx";

    ORGID = "55522777";
    WORKSPACEID = "35130000001055707";
    VIEWID = "35730000007354002";

class sample:

    ac = AnalyticsClient(Config.CLIENTID, Config.CLIENTSECRET, Config.REFRESHTOKEN)

    def add_aggregate_formula(self, ac):
        formula_name = "Formula_1"
        expression = "sum(\"Cost\")"
        view = ac.get_view_instance(Config.ORGID, Config.WORKSPACEID, Config.VIEWID)
        result = view.add_aggregate_formula(formula_name, expression)
        print(result)

try:
    obj = sample()
    obj.add_aggregate_formula(obj.ac);

except Exception as e:
    print(str(e))
Copiedvar analyticsClient = require('./AnalyticsClient');

var clientId = '1000.xxxxxxx';
var clientSecret = 'xxxxxxx';
var refreshtoken = '1000.xxxxxxx.xxxxxxx';
var orgId = '55522777';
var workspaceId = '35130000001055707';
var viewId = '35730000007354002';

var ac = new analyticsClient(clientId, clientSecret, refreshtoken);

var formulaName = '';
var expression = '';

var config = {};
var view = ac.getViewInstance(orgId, workspaceId, viewId);
view.addAggregateFormula(formulaName, expression, config).then((response) => {
    console.log(response);

}).catch((error) => {
    console.log('errorCode : '+error.errorCode);
    console.log('errorMessage : '+error.errorMessage);
});
CopiedorgId = "55522777";
workspaceId = "35130000001055707";
viewId = "35130000004356002";

headersMap = Map();
headersMap.put("ZANALYTICS-ORGID",orgId);

config = Map();
config.put("formulaName", "Formula_1");
config.put("expression", "sum(\"Cost\")");
paramsMap = Map();
paramsMap.put("CONFIG",config.toString());

response = invokeurl
[
  url :"https://analyticsapi.zoho.com/restapi/v2/workspaces/" + workspaceId + "/views/" + viewId + "/aggregateformulas"
  type :POST
  parameters:paramsMap
  headers:headersMap
  connection:"analytics_oauth_connection"
];
info response;

Download SDK : C# | GO | JAVA | PHP | PYTHON | NodeJS

Sample value for CONFIG parameter:

Copied{
    "formulaName": "<formula-name>",
    "expression": "<formula-expression>"
}

Sample Response:

CopiedHTTP/1.1 200 OK
Content-Type:application/json;charset=UTF-8

{
    "status": "success",
    "summary": "Create aggregate formula",
    "data": {
        "formulaId": 1767024000009137000,
        "formulaName": "F2"
    }
}