[SalesForce] How to write formula field that returns a percentage to show amount at or below 100%

Your input is appreciated, I'm creating a formula field called consumption is a result of dividing two numeric fields (Modified Usage, and Range) that will return a percentage of usage for example. If the Modified Usage is 80 and the Range is 100 then I want it to show 80%.

`IF( Modified_Usage__c / (Value(TEXT(Range__c))+0)> 1,1, (Modified_Usage__c /(Value(text (Range__c))+0)))`

Definitions:
Modified Usage is a numeric field
The range is a picklist hence the Value(Text…
Consumption is the percentage of consumption based on the formula calculation.

I am not getting any Syntax errors before saving the formula field, but I get an #ERROR! once I add the field to the page layout for any and all entries. Suggestions are welcome. I've looked through the documentation and know I'm not dividing by zero for the error to occur.

Best Answer

I realize you say you're not dividing by zero, but just to be sure, let's check for blank values first, then we'll optimize:

IF(
  ISPICKVAL(Range__c,""), null,
  MIN(1,Modified_Usage__c/VALUE(SUBSTITUTE(TEXT(Range__c),",",""))))

Here, we see if range is empty, and if so, return a blank value, otherwise we take the lesser of 1 or Modified Usage divided by Range as a value.

Please note that there's a formula option that says "Treat blanks as zero" or "Treat blanks as null." You must make sure you're using the "Treat blanks as zero" option to avoid errors if Modified Usage is null.

You can change this behavior if you like by returning 0 or another value when Range is empty by replacing null with whatever reasonable value you'd like.

Related Topic