testing for below, between, above in one formula

JamesPW

Board Regular
Joined
Nov 4, 2005
Messages
51
Office Version
  1. 2019
Platform
  1. MacOS
Im trying to write a formula that does one thing if the number is below a low test value, does a second thing if the number is between a low and high test values and a third if the number is above the high test value. It's for an electrical billing formula
$/kWhcost@Usage
40 kWh80 kWh319 kWh
Rate first 50 kWh$0.33cost1 40@.33=13.250@.33=16.550@.33=16.5
Rate 51-200 kWh$0.38cost2 0@.38=080-50=30@.38=11.4200-50@.38=57
Rate above 200 kWh$0.43cost3 0@.43=00@.43=0319-200@.43=51.17
TotalTotal=$@rate1+$@rate2+$@rate3$13.20$27.90$124.67
Needs to be a single formula that gives cost1, cost2, cost3 plus total at all levels of usage.
I can use an IF(AND(>50,<200), TRUE,FALSE) will give me the "between" # but not the correct low and high in the same equation.
Thanks in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Maybe something like the below
Book3
ABCDE
1$/kWhcost@Usage
24080319
3Rate first 50 kWh$0.33cost1 40@.33=13.250@.33=16.550@.33=16.5
4Rate 51-200 kWh$0.38cost2 0@.38=080-50=30@.38=11.4200-50@.38=57
5Rate above 200 kWh$0.43cost3 0@.43=00@.43=0319-200@.43=51.17
6TotalTotal=$@rate1+$@rate2+$@rate3$13.20$27.90$124.67
Sheet2
Cell Formulas
RangeFormula
C6:E6C6=SUMPRODUCT(--(C2>{0;50;200}), (C2-{0;50;200}), {0.33;0.05;0.05})
 
Upvote 0
The first there rows calculate the Cost with hardcode values.
Book2
ABCDE
1kWh Use$/kWh
240$13.20
380$27.90
4319$124.67
5Rate first 50 kWh$0.33cost1 40@.33=13.250@.33=16.550@.33=16.5
6Rate 51-200 kWh$0.38cost2 0@.38=080-50=30@.38=11.4200-50@.38=57
7Rate above 200 kWh$0.43cost3 0@.43=00@.43=0319-200@.43=51.17
8Calculate Cost using hard coded kWH use breakpoints and rates
9TotalTotal=$@rate1+$@rate2+$@rate3$13.20$27.90$124.67
Sheet1
Cell Formulas
RangeFormula
B2:B3B2=IF(A2>=201,(A2-200)*0.43+150*0.34+50*0.33,IF(A2>=51,(A2-50)*0.38+50*0.33,A2*0.33))
B4B4=IF(A4>=201,(A4-200)*0.43+150*0.38+50*0.33,IF(A4>=51,(A4-50)*0.38+50*0.33,A4*0.33))


The second example uses the combination of a lookup table and basically the same formula, but using all indexes into the lookup table.
This way if/when the rates change or the kWh usage breakpoint changes your calculation should not change; all you need to do is update the table.

Lookup Tables (named ranges kWhUse and kWhRate)
Book2
HI
1kWh UsekWh Rate
200.33
3510.38
42010.43
Sheet1


Book2
AB
13Calc Cost Using Index Lookup of Named Ranges
14Test UsageCost Calc
154013.20
165116.88
178027.90
18319124.67
Sheet1
Cell Formulas
RangeFormula
B15:B18B15=IF(A15>=INDEX(KWHUse,3),(A15-INDEX(KWHUse,3)+1)*INDEX(KWHRate,3)+(INDEX(KWHUse,3)-INDEX(KWHUse,2))*INDEX(KWHRate,2)+(INDEX(KWHUse,2)-1)*INDEX(KWHRate,1),IF(A15>=INDEX(KWHUse,2),(A15-INDEX(KWHUse,2)+1)*INDEX(KWHRate,2)+(INDEX(KWHUse,2)-1)*INDEX(KWHRate,1),A15*INDEX(KWHRate,1)))
Named Ranges
NameRefers ToCells
KWHRate=Sheet1!$I$2:$I$4B15:B18
KWHUse=Sheet1!$H$2:$H$4B15:B18


Using the formula provided by MARK858 above with a lookup table
Book2
JK
1KWHUse2kWH Rate Inc
200.33
3500.05
42000.05
Sheet1


Book2
A
14Test Usage
1540
1651
1780
18319
Sheet1

Book2
D
14SumProduct Formula
1513.20
1616.88
1727.90
18124.67
Sheet1
Cell Formulas
RangeFormula
D15:D18D15=SUMPRODUCT(--(A15>=KWHUse2), (A15-KWHUse2), KWHRateInc)
Named Ranges
NameRefers ToCells
KWHRateInc=Sheet1!$K$2:$K$4D15:D18
KWHUse2=Sheet1!$J$2:$J$4D15:D18
 
Upvote 0
If I was using a Lookup rather than hard coding the values then I would use a formula for the differential rate as well, something like the below.
E2:G2 is a number with the custom format of 0" Kwh", C3:C5 and E6:G6 are numbers formatted as currency
Book3
ABCDEFG
1$/kWhcost@Usage
240 Kwh80 Kwh319 Kwh
3Rate first 50 kWh0$0.330.33cost1 40@.33=13.250@.33=16.550@.33=16.5
4Rate 51-200 kWh50$0.380.05cost2 0@.38=080-50=30@.38=11.4200-50@.38=57
5Rate above 200 kWh200$0.430.05cost3 0@.43=00@.43=0319-200@.43=51.17
6TotalTotal=$@rate1+$@rate2+$@rate3$13.20$27.90$124.67
Sheet2
Cell Formulas
RangeFormula
D3:D5D3=C3-C2
E6:G6E6=SUMPRODUCT(--(E2>$B$3:$B$5), (E2-$B$3:$B$5), $D$3:$D$5)


or using a Lookup table

Book3
ABCDEFGHI
1$/kWhcost@Usage
240 Kwh80 Kwh319 KwhGreater thanHourly RateDifferential
3Rate first 50 kWh$0.33cost1 40@.33=13.250@.33=16.550@.33=16.50
4Rate 51-200 kWh$0.38cost2 0@.38=080-50=30@.38=11.4200-50@.38=570$0.33$0.33
5Rate above 200 kWh$0.43cost3 0@.43=00@.43=0319-200@.43=51.1750$0.38$0.05
6TotalTotal=$@rate1+$@rate2+$@rate3$13.20$27.90$124.67200$0.43$0.05
Sheet2
Cell Formulas
RangeFormula
H4:H6H4=B3
I4:I6I4=H4-H3
C6:E6C6=SUMPRODUCT(--(C2>$G$4:$G$6), (C2-$G$4:$G$6), $I$4:$I$6)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top