This function, `toAnnual`, calculates the equivalent **annual churn rate** based...

June 27, 2025 at 08:44 PM

/** * Converts a monthly churn rate to an annual churn rate. * * @param {number} monthlyChurn The monthly churn rate as a decimal or percentage (e.g., 0.1 or 10%). * @return The equivalent annual churn rate. * @customfunction */ function toAnnual(monthlyChurn) { // Normalize if input is given as a percentage (e.g., 10 becomes 0.10) if (monthlyChurn > 1) { monthlyChurn = monthlyChurn / 100; } // Clamp to valid range if (monthlyChurn < 0 || monthlyChurn >= 1) { return 'Invalid input: must be between 0% and less than 100%'; } // Annual churn = 1 - (1 - monthly churn)^12 var annualChurn = 1 - Math.pow(1 - monthlyChurn, 12); // Return as percentage return annualChurn; }

This function, toAnnual, calculates the equivalent annual churn rate based on a given monthly churn rate. Here's a breakdown of how it works:

  1. Input Normalization:

    • If the input monthlyChurn is greater than 1, the function assumes it is expressed as a percentage (e.g., 10 for 10%) rather than a decimal (e.g., 0.1 for 10%).
    • It divides the input by 100 to convert it into a decimal format.
  2. Input Validation:

    • If the monthlyChurn is outside the valid range (less than 0 or greater than or equal to 1), it returns an error message: "Invalid input: must be between 0% and less than 100%".
    • This ensures the function only deals with valid churn rates.
  3. Churn Rate Conversion:

    • The formula 1 - (1 - monthlyChurn)^12 is used to compute the annual churn rate.
    • It is based on the compounding effect where the annual churn rate is not a simple multiplication of the monthly churn rate but rather reflects cumulative loss over a 12-month period.
  4. Output:

    • The computed annualChurn is returned as a decimal value.
    • Optionally, the caller could interpret it as a percentage by further converting it, though the function itself does not explicitly format it as a percentage.

Example Usage:

toAnnual(0.1);  // Input 10% (or 0.1 as decimal). Output will be approximately 0.713 (71.3% annual churn).
toAnnual(10);   // Input 10% as number, normalized first, output will be the same as above (71.3%).
toAnnual(-0.2); // Invalid input, returns an error message.
toAnnual(1);    // Invalid input, returns an error message.

The function is a custom function designed for scenarios such as analytics dashboards (e.g., in spreadsheets or SaaS applications) to help users understand retention and churn trends.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node