Calculate a tiered pricing discount

Coding level: Beginner
Duration: 10 minutes
Project type: Custom function

About this solution

If you offer a tiered pricing system for your customers, this custom function makes it easier to calculate the discount amounts for your prices.

Though you could use the built-in function SUMPRODUCT to make a tiered pricing calculation, using SUMPRODUCT is more complex and less flexible than this solution's custom function.

Screenshot of the tier pricing sample

How it works

A tiered pricing model means that the cost of goods or services goes down based on the quantity purchased.

For example, imagine you have two tiers, one that ranges from $0-$500 and is discounted by 10% and one that ranges from $501-$1,000 and is discounted by 20%. If the total price you need to calculate a discount for is $700, the script multiplies the first $500 by 10% and the remaining $200 by 20%, for a total discount of $90.

For a given total price, the script loops through the specified tiers in the tier pricing table. For each portion of the total price that falls within a tier, that portion is multiplied by the tier's associated percent value. The result is the sum of each tier's calculation.

Before you begin

To use this sample, you need the following prerequisites:

  • A Google Account (Google Workspace accounts might require administrator approval)
  • A web browser with access to the internet

Try it

  1. Click the button below to make a copy of the Tier pricing custom function spreadsheet.
    Make a copy

  2. The table at row 16 shows a sample price calculation for a Software as a Service (SaaS) product.

  3. To calculate the discount amount, in cell C20, enter =tierPrice(C19,$B$3:$D$6). The final price updates in cell C21.

Open the Apps Script project

The sample script's code includes in-line comments to help you understand how it works. To access the code, in the spreadsheet, click Extensions > Apps Script.

Modifications

You can edit the custom function as much as you'd like to fit your needs. Below is an optional addition to manually refresh custom function results.

Refresh cached results

Unlike built-in functions, Google caches custom functions to optimize performance. Therefore, if you change something within your custom function, such as a value that's being calculated, it might not immediately force an update. To refresh the function result manually, take the following steps:

  1. Add a checkbox to an empty cell by clicking Insert > Checkbox.
  2. Add the cell that has the checkbox as an extra parameter of the custom function. For example, if you add a checkbox to cell D20, update the tierPrice() function in cell C20 to =tierPrice(C19,$B$3:$D$6,D20).
  3. Check or uncheck the checkbox to refresh the custom function results.

Contributors

This sample is maintained by Google with the help of Google Developer Experts.