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
SUMPRODUCT is more complex and less flexible than this
solution's custom function.
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
Click the button below to make a copy of the Tier pricing custom function spreadsheet.
Make a copy
The table at row 16 shows a sample price calculation for a Software as a Service (SaaS) product.
To calculate the discount amount, in cell
=tierPrice(C19,$B$3:$D$6). The final price updates in cell
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.
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:
- Add a checkbox to an empty cell by clicking Insert > Checkbox.
- 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
- Check or uncheck the checkbox to refresh the custom function results.
This sample is maintained by Google with the help of Google Developer Experts.