Calculate the average purchase cycle

Calculate the average purchase cycle of your customer accounts by dividing the days between their first and last closed-won deal by the total number of intervals minus one.

Understanding the average purchase cycle of your customers is important for devising your sales, marketing, and retention strategies. This cycle represents the time customers take to make two consecutive purchases, providing valuable insights into their buying behavior. By knowing your customers' buying behavior, teams can better time follow-ups, nurturing campaigns, and promotional offers.

However, calculating this metric in your CRM requires exporting your data to Excel or Zoho Analytics, which can be time-consuming and difficult. Is there a way to automatically compute and update this value in a custom field whenever a closed-won deal is created for an account? Fortunately, our low-code tools makes it possible.

Using a combination of a custom field, Deluge script, and a workflow rule, you can automatically calculate and update the average purchase cycle in an account record. The deluge script computes the average by dividing the days between the first and last closed-won deal by the total number of intervals minus one, then rounds the result. Continue reading to learn more.

Permissions and availability

  • Users with the Modules Customization permission can add custom fields.
  • Users with the Manage Automation permission can create and update workflow rules.
  • Users with the Manage Extensibility permission can create custom functions.
  • Users with the Manage Sandbox permission can manage the sandbox.

Requirements

  • Add a Number field in the Accounts module. This field will capture the average purchase cycle of an account in days. Learn more
  • Create a workflow rule for the Deals module that triggers upon marking a deal as closed won. Learn more
  • Write a custom Deluge function and link it up with the workflow rule. Learn more
  • Test the workflow rule in a sandbox before deploying it to your production environment. Learn more

Add a custom field

The first step is adding a custom number field to the Accounts module.

  1. Navigate to Setup > Customization > Modules and Fields.
  2. Click the Accounts module to open the layout editor.
  3. Drag and drop the Number field from the New Fields tray to the desired section of the layout.
  4. Name the number field (e.g., "Average Purchase Cycle") and define its properties as required.
  5. Once you have finished, click Save and Close.

Create a workflow rule

The next step is creating a workflow rule in your CRM account.

  1. Navigate to Setup > Workflow Rules > +Create Rule.
  2. Select Deals from the module dropdown list.
  3. Provide a name for the rule. For example: "Calculate the average purchase cycle".
  4. Add a description (optional) and then click Next.
  5. In the Execute this workflow rule based on section, do the following:
    1. Choose Record Action, then select Create or Edit from the dropdown.
    2. Check the Repeat this workflow whenever a deal is edited option.
    3. Click Next.
  6. In the Which deals would you like to apply the rule to? section, select <Stage> is <Closed Won>, then click Done.
  7. Under Instant Actions, select Function and Write your own.
  8. Provide a Name for the function, as well as a description, if necessary.
  9. In the Deluge Script Editor, do the following:
    1. Copy and paste the code given below.
    2. Click Edit Arguments.
    3. Enter the name as id, select the value as Accounts - Account Id, then click Save.
    4. Click Save & Execute script.
  10. Click Save on the workflow rule page.

The code

Code Copied
rsp = zoho.crm.getRelatedRecords("Deals","Accounts",id);
sz = rsp.size();
if ( sz > 0 )
{
count = 0;
mp = Map();
dlist = List();
for each ele in rsp
{
if ( ele.get("Stage") == "Closed Won" )
{
	dlist.add(ele.get("Closing_Date"));
 count = count+1;
}
}
ldate = dlist.sort(false).get(0);
info ldate;
fdate = dlist.sort(true).get(0);
info fdate;
numberOfDays =abs(daysBetween(fdate,ldate));
 info numberOfDays;
 val = numberOfDays/(count-1);
 mp.put("Average_Purchase_Cycle",val.round(0));
up = zoho.crm.updateRecord("Accounts",id,mp);
info up;
info mp;
}

Notes

  • Make sure to use the accurate API name of the custom number field (e.g., "Average Purchase Cycle") in the code snippet. Learn more
  • The code above is an example of how to calculate and update the average purchase cycle for a customer in their account record in your CRM. You can use this code to update the average purchase cycle in other modules, such as Contacts, by modifying the module name and parameters.

Tip

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.

Test the solution

  1. Navigate to the Accounts module.
  2. Open an account record with one or more deals in the closed-won stage.
  3. On the Account Details page, click Deals under the Related List, and then click the + icon to add a new deal.
  4. Enter the required deal information, set its stage to Closed Won, and click Save.
    The workflow rule will be triggered.
  5. Check whether the Average Purchase Cycle field for the corresponding account was updated with the correct number of days.

Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.

More info

  • ModuleAccounts
  • Trigger PointWorkflow Rule
  • EditionEnterprise and above
  • ComplexityMedium
  • Implementation Time30 minutes

Features used in the solution

Custom FieldWorkflow RuleDeluge ScriptSandbox
Story Image

Looking for a custom solution?

Contact us, we will help enhance your productivity at lightning speed.

SUBMIT REQUEST

Developers: Share your solution with our community!