Useful Code Snippets

Significant processing can be achieved by defining calcs in cPlans using expressions.

Displaying a Value or a Count

Suppose you have an unknown number of data values. If there is only one value, you want to display it; if there are multiple values, you want to display the count. Include something similar to the following calc expression in the cPlan:

calc "Customer Count" `dimcount("Customer")` 
calc "Customer_Name-Number" `if(calc("Customer Count") = 1, any(value("Customer")), string(dimcount("Customer")))` 

This can be used on central and overview pages. If there is one customer, the name is displayed; if more than one, the dimension count is displayed.

Similarly, the following snippet displays the dimension count for invoice keys, or zero if the dimcount is null:

calc "INVOICE_KEY Count" `coalesce(dimcount("INVOICE_KEY"), 0)` 

Defining a Display Label

Often the calc coded in the script is not appropriate to display on the page. Define a label for the calc in the cPlan. For example:

calc "Cases 912" `any(value("Cases"))` label = "Sudan" 

When this calc, Cases 912, is used, the label changes to Sudan for summary Cases. The column is identified as Cases 912.

Detecting Null Values

The following cPlan calc determines if the sales amount is zero before calculations:

calc "GROSS_PROFIT%" `if(add(value("SALES_AMT")) = 0, null, add(value("GROSS_PROFIT")) / add(value("SALES_AMT")) * 100)` 

If the sale amount is zero, null is displayed; otherwise the calculation is performed.

Including an Email

When used in a cPlan, the following calc presents an address to the mail client:

calc "EmailMe" `"[email protected]"` 

See also: