Calc IF problem

Hi,

I have a spreadsheet with four pricing ranges for a certain number of units.

G4 = 601
H4 = calculated by below formula = 751.25 but should be 751.52
.
F20 = 1.25
G20 = 150 i.e. 0->150 units
h20 = F*G

I20 = 1.25
J20 = 200 i.e. 150.0001 -> 350 units
K20 = I*J

L20 = 1.25
M20 = 250 i.e. 350.0001 -> 600 units
N20 = L*M

O20 = 1.52
P20 >= 600 i.e. 600.0001 -> infinity units
Q20 = O*P

The below formula calculates H4
=IF(0<G4<=$G$20,G4*$F$20,IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),IF($M$20<G4<$P$20,(G4-$G$20-$J$20-$M$20)*$O$20+(($G$20*$F$20)+($J$20*$I$20)+($M$20*$L$20),IF(G4>=$P$20,((G4-$G$20-$J$20-$M$20-$P$20)*$O$20+(($G$20*$F$20)+($J$20*$I$20)+($M$20*$L$20)+($O$20*$P$20))))))))))

Is there a simpler way to achieve the right answer i.e. 751.52 as I have checked all the cell values and they all read as indicated here.

Where am I making a mistake?

Regards
Hylton

Hi,

Where am I making a mistake?

Don´t use multiple nested IF-functions. Use the VLOOKUP-function!

Get yourself Calc User Guide, Chapter 13:

https://wiki.documentfoundation.org/images/5/53/CG4013-CalcAsASimpleDatabase.pdf

and look for VLOOKUP.

Cheers,
Stefan

Hi.
Brian Barker gave a very succint method to do this in a previous discussion.
You can search this list in Nabble
http://nabble.documentfoundation.org/Users-f1639498.html

Search VLOOKUP
Steve

Electricity.ods
<http://nabble.documentfoundation.org/file/n4077843/Electricity.ods>

Thank you both for the input.

I have searched on Nabble and not found Brian's succint method and despite
looking at the manual and the vlookup command was unable to apply it to my
situation.
I would be happy to change the many IF formulae I have to use vlookup, but I
need some assistance in one cell on using the formula with the layout of the
data I have.

If you feel challenged towards producing better formulae usage or even feel
mathematically challenged, please have a look on Nabble for the uploaded
file and using the TAB Data2 try and get the currency value of H4 from the
value in G4 using the sliding scale on F20:O20

What is strange is that on entering the Function Wizard on H4 to edit the
formula, placing my cursor on the 2nd last line of the formulae I get the
CORRECT answer displaying in the the 'Function Result' window but not in
the 'Result' window of the Function Wizrd. Of course pressing OK then gives
the incorrect answer ie 751.25 as opposed to the correct one of 751.52.

Appreciated
Hylton

Hi.
I think it is this.
http://nabble.documentfoundation.org/Fwd-Re-libreoffice-users-VLOOKUP-for-Numbers-td4046030.html#a4046335

Steve

Sorry if this appears critical, but there are a number of things to say here.

o To get the best help from others, you really do need to explain your problem in detail - and that means words, not formulae or values. Once you have defined your problem, it is very likely someone can help you. Indeed, explaining your problem concisely and completely may well help you to see how to solve it yourself!

o You have laid your values out as a row (in row 20) - as if it's a vector, a twelve by one array. But surely it is a four by three array? Setting this out as four rows of three columns would help you see what the likely solution was. And you have not used those products (H20, K20, etc.) in your formula: instead, you have effectively recalculated them all, so they are just distracting.

o I always hope it's obvious (but it's clearly not) that you cannot define what you are trying to calculate by quoting a formula that you know doesn't work. This must indicate what you *don't* want! Please, please omit all the detail and say what you do want. (Forcing yourself to do this is a good first step in solving any problem.)

o Any formula as complicated and repetitive as this cannot be the right way to do things. Apart from anything else, you might not notice any slip you might have made in the middle of the formula, and it would be quite easy to miss it during testing. Anything repetitive should be reduced to a simpler formula that nevertheless deals with all situations.

o You have apparent logical expressions such as 0<G4<=$G$20. Whilst A<B<C is meaningful in mathematics, where in the documentation did you find any suggestion that it makes sense in spreadsheet lingo?! Since it's undefined, I'm having to guess what this will mean to Calc. First, 0<G4 will be evaluated, giving the result TRUE or FALSE. Now the rest of the expression reduces to TRUE<=$G$20 or FALSE<=$G$20 - which make no sense, of course. In practice, it seems that the logical values are interpreted as numbers - with FALSE being zero and TRUE being one - and this numerical value is then compared with $G$20. But these values are 150, 200, and so on, which will always be greater than either zero or one, so all the conditions will evaluate to TRUE!

o Your last IF function has a argument for truth but not one for falsehood, so the logical value FALSE will be substituted. You have given no alternative because your final condition is bound to be true if the previous ones have all been false, so you don't need it. Remove that IF!

o Remember that multiplication precedes addition and subtraction in evaluation, so many of your parenthesis pairs are unnecessary. You may think they clarify things, but in my experience they obfuscate the meaning of a formula and mask errors.

There is a lot here that anyone helping is still having to guess, but the problem is almost certainly best solved using VLOOKUP(). I haven't had time to look at it yet ...

I trust this helps (so far).

Brian Barker

While I agree with Brian's other points, I just wanted to comment that
normally, I find it much better to include parentheses around sums,
explicitly stating the order, rather than relying on (sometimes
differing) operator precedence rules. This also helps to group things,
and in my opinion usually makes things more clear, rather than
obfuscating anything. However, in this extreme example, I doubt it would
make much difference either way.

Paul

Remember that multiplication precedes addition and subtraction in evaluation, so many of your parenthesis pairs are unnecessary. You may think they clarify things, but in my experience they obfuscate the meaning of a formula and mask errors.

While I agree with Brian's other points, I just wanted to comment that normally, I find it much better to include parentheses around sums, ...

Sums? But sums (along with differences) often do need parentheses, of course: it's products and quotients that often don't.

... explicitly stating the order, rather than relying on (sometimes differing) operator precedence rules.

"Sometimes differing"?! Is this the new mathematics? ;^)

This also helps to group things, and in my opinion usually makes things more clear, rather than obfuscating anything.

Chacun à son goût, of course.

Brian Barker

Sums? But sums (along with differences) often do
need parentheses, of course: it's products and quotients that often
don't.

You're right, my bad; I meant around parts of formulas, calculations,
expressions or whatever you want to call them, depending on context.

"Sometimes differing"?! Is this the new mathematics? ;^)

http://en.wikipedia.org/wiki/Order_of_operations lists a couple of
exception cases. I recall coming across differing orders between two
programming languages in the past, although to be honest I can't
remember where. It does seem to be unusual corner cases, but it's worth
noting that those corners could be very sharp indeed.

Chacun à son goût, of course.

Exactly.

Paul

Interesting! Thanks.

Brian Barker

The actual expression is: des goûts et des couleurs [ça ne se discute pas]

literal EN: Tastes and colours [that's not debatable]

:slight_smile:

I have a spreadsheet with four pricing ranges for a certain number of
units.

G4 = 601
H4 = calculated by below formula = 751.25 but should be 751.52
.
F20 = 1.25
G20 = 150 i.e. 0->150 units
h20 = F*G

I20 = 1.25
J20 = 200 i.e. 150.0001 -> 350 units
K20 = I*J

L20 = 1.25
M20 = 250 i.e. 350.0001 -> 600 units
N20 = L*M

O20 = 1.52
P20 >= 600 i.e. 600.0001 -> infinity units
Q20 = O*P

The below formula calculates H4
=IF(0<G4<=$G$20,G4*$F$20,IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),IF($M$20<G4<$P$20,(G4-$G$20-$J$20-$M$20)*$O$20+(($G$20*$F$20)+($J$20*$I$20)+($M$20*$L$20),IF(G4>=$P$20,((G4-$G$20-$J$20-$M$20-$P$20)*$O$20+(($G$20*$F$20)+($J$20*$I$20)+($M$20*$L$20)+($O$20*$P$20))))))))))

Is there a simpler way to achieve the right answer i.e. 751.52 as I
have checked all the cell values and they all read as indicated here.

Where am I making a mistake?

Sorry if this appears critical, but there are a number of things to say
here.

Criticism is not bad, it is a method of proposing different or better solution to a problem. Your thoughts are welcome and appreciated.

o To get the best help from others, you really do need to explain your
problem in detail - and that means words, not formulae or values. Once
you have defined your problem, it is very likely someone can help you.
Indeed, explaining your problem concisely and completely may well help
you to see how to solve it yourself!

The spreadsheet is what I use to calculate the exact units purchased and used each month for a prepaid electricity meter. Those same unit values could build a trend and so additional units could be bought in the colder months, without knowing what additional currency purchase value( i.e. ZAR200 or ZAR500) would be needed e.g. Assuming there was say 100 units of electricity left at the end of the month and based on an average the house will probably use 750 units of electricity next month. Therefore an amount of 650 units needs purchasing. Based on the pricing structure on the number of units required being provided by the local electricity authority, what is the currency value that must be taken to an electricity vendor to purchase the required 650 electricity units?

o You have laid your values out as a row (in row 20) - as if it's a
vector, a twelve by one array. But surely it is a four by three array?

Then you need another column to show what number of units were purchased in each given month for each of the four price brackets....

Setting this out as four rows of three columns would help you see what
the likely solution was. And you have not used those products (H20,
K20, etc.) in your formula: instead, you have effectively recalculated
them all, so they are just distracting.

o I always hope it's obvious (but it's clearly not) that you cannot
define what you are trying to calculate by quoting a formula that you
know doesn't work. This must indicate what you *don't* want! Please,
please omit all the detail and say what you do want. (Forcing yourself
to do this is a good first step in solving any problem.)

I agree, however the end value I want is determinant on the remaining electricity unit balance at the end of the month, which can be highly variable.

o Any formula as complicated and repetitive as this cannot be the right
way to do things. Apart from anything else, you might not notice any
slip you might have made in the middle of the formula, and it would be
quite easy to miss it during testing. Anything repetitive should be
reduced to a simpler formula that nevertheless deals with all situations.

I agree this is a complicated formula and so far during testing I have managed to ascertain that despite the 350.1 -> 600 being 250, for some reason my formula is not taking heed of the 250 Max unit value i.e. 251 units instead of being 250 units plus 1 unit at 1.52 being the pricing range for the next number of units.

o You have apparent logical expressions such as 0<G4<=$G$20. Whilst
A<B<C is meaningful in mathematics, where in the documentation did you
find any suggestion that it makes sense in spreadsheet lingo?!

Actually in the Formula Wizard as it mentions the first field needs to be the test value, the next field needs to be the action if the test field is true and the third field in the IF formula needs to be applied if the test field evaluation is false.

Since
it's undefined, I'm having to guess what this will mean to Calc. First,
0<G4 will be evaluated, giving the result TRUE or FALSE. Now the rest
of the expression reduces to TRUE<=$G$20 or FALSE<=$G$20 - which make no
sense, of course. In practice, it seems that the logical values are
interpreted as numbers - with FALSE being zero and TRUE being one - and
this numerical value is then compared with $G$20. But these values are
150, 200, and so on, which will always be greater than either zero or
one, so all the conditions will evaluate to TRUE!

No offence, but moving back to school mathematics the phrase 0<G4=<G20 would ask a true/false question i.e. is G4 BOTH greater than 0 AND less than or equal to G20. If the g4 value is only 10 then the expression is true, thus allowing the corresponding action to be exercised. If the g4 value was 25 the result of the initial test would have been false and the other corresponding action taken.
The above is my interpretation of the IF formula in Calc.

o Your last IF function has a argument for truth but not one for
falsehood, so the logical value FALSE will be substituted. You have
given no alternative because your final condition is bound to be true if
the previous ones have all been false, so you don't need it. Remove
that IF!

My last IF I did battle with as given the previous pricing range no value requiring the 600 price range would ever be below 600 units. Even putting in the same formula for both True and False possibilities yielded the wrong answer i.e. 751.25 and not 751.52.

Further testing has just revealed that because the first three price brackets are identical, changing the first price changes my end formula, which to me indicates that the formula for units over 600 isn't even being used. :frowning:

o Remember that multiplication precedes addition and subtraction in
evaluation, so many of your parenthesis pairs are unnecessary. You may
think they clarify things, but in my experience they obfuscate the
meaning of a formula and mask errors.

I tend to agree but do remember my Maths teacher drumming BODMAS into us all repeatedly, so it is really only done to clarify what cells are being 'BODMASed'.

There is a lot here that anyone helping is still having to guess, but
the problem is almost certainly best solved using VLOOKUP(). I haven't
had time to look at it yet ...

I am afraid I disagree as the end number I need is determinant by a variable input, which itself attracts different pricing based on its value.

I trust this helps (so far).

There is much to think about and I have to admit laying out the table in a different format is my least favourite or preferred but if someone can prove to me I would get the answers I seek, then the least favourite would have to be implemented. How to re-arrange the table to cater for four pricing ranges, each with a different max number of units, over twelve months, as well as proving annual totals and monthly averages is currently beyond me.

I hope this clears the confusion a little.

Hylton

To get the best help from others, you really do need to explain your problem in detail - and that means words, not formulae or values. Once you have defined your problem, it is very likely someone can help you. Indeed, explaining your problem concisely and completely may well help you to see how to solve it yourself!

The spreadsheet is what I use to calculate the exact units purchased and used each month for a prepaid electricity meter. [...]

Sorry, but you've missed my point. It's not that anyone particularly needs to know the purpose of your spreadsheet, but that the exact purpose of the *formula* needed to be specified. Originally, you just gave the formula, saying it didn't work, but not specifying exactly what it was supposed to calculate. You needed to specify the purpose of your formula mathematically - separately from the formula that you knew wasn't correct. (Except that I think you may still think the formula is correct.)

You have laid your values out as a row (in row 20) - as if it's a vector, a twelve by one array. But surely it is a four by three array?

Then you need another column to show what number of units were purchased in each given month for each of the four price brackets....

No, you don't need "another" column: the four by three array is a separate table specifying the price structure. You will, of course, retain a column for units - but this will be in your main table showing the months.

You have apparent logical expressions such as 0<G4<=$G$20. Whilst A<B<C is meaningful in mathematics, where in the documentation did you find any suggestion that it makes sense in spreadsheet lingo?!

Actually in the Formula Wizard as it mentions the first field needs to be the test value, the next field needs to be the action if the test field is true and the third field in the IF formula needs to be applied if the test field evaluation is false.

Exactly so - but 0<G4<=$G$20 is *not* a spreadsheet test value - at least, not one meaning what you want it to mean.

Since it's undefined, I'm having to guess what this will mean to Calc. First, 0<G4 will be evaluated, giving the result TRUE or FALSE. Now the rest of the expression reduces to TRUE<=$G$20 or FALSE<=$G$20 - which make no sense, of course. In practice, it seems that the logical values are interpreted as numbers - with FALSE being zero and TRUE being one - and this numerical value is then compared with $G$20. But these values are 150, 200, and so on, which will always be greater than either zero or
one, so all the conditions will evaluate to TRUE!

No offence, ...

None taken!

... but moving back to school mathematics the phrase 0<G4=<G20 would ask a true/false question i.e. is G4 BOTH greater than 0 AND less than or equal to G20. If the g4 value is only 10 then the expression is true, thus allowing the corresponding action to be exercised. If the g4 value was 25 the result of the initial test would have been false and the other corresponding action taken.

As I acknowledged, A<B<C makes complete sense in mathematics - school or otherwise!

The above is my interpretation of the IF formula in Calc.

Oh, dear: then you are doomed to continue to fail. As I explained before, A<B<C is not a logical spreadsheet expression that is TRUE when both A<B and B<C. Instead, A<B appears to be evaluated first, then the resulting value of this part is (mis)interpreted as a number (zero for FALSE and one for TRUE), and finally this *number* is compared with C. As I also mentioned, if you want the idea of B being between A and C, you have two comparisons and you need *in a spreadsheet* an expression something like AND(A<B;B<C). As it is, all your conditions will be TRUE.

Your last IF function has a argument for truth but not one for falsehood, so the logical value FALSE will be substituted. You have given no alternative because your final condition is bound to be true if the previous ones have all been false, so you don't need it. Remove that IF!

My last IF I did battle with as given the previous pricing range no value requiring the 600 price range would ever be below 600 units.

Exactly: so, as I mentioned, you don't need to test that condition: that IF is superfluous.

Even putting in the same formula for both True and False possibilities yielded the wrong answer i.e. 751.25 and not 751.52.

That's because of more fundamental errors in the formula.

Further testing has just revealed that because the first three price brackets are identical, changing the first price changes my end formula, which to me indicates that the formula for units over 600 isn't even being used. :frowning:

Exactly: all your conditions are TRUE. Since you nested the IFs, the fact that the first one evaluates to TRUE means that all the later ones are never used.

There is a lot here that anyone helping is still having to guess, but the problem is almost certainly best solved using VLOOKUP(). I haven't had time to look at it yet ...

I am afraid I disagree as the end number I need is determinant by a variable input, which itself attracts different pricing based on its value.

As you will have seen, I have suggested an alternative route. But using VLOOKUP() would have made sense referred to a price table: to calculate each part of the contribution to your cost, you need to look up and use in your formula both the price of each band and the limits of each price band.

There is much to think about and I have to admit laying out the table in a different format is my least favourite or preferred but if someone can prove to me I would get the answers I seek, then the least favourite would have to be implemented. How to re-arrange the table to cater for four pricing ranges, each with a different max number of units, over twelve months, as well as proving annual totals and monthly averages is currently beyond me.

I don't think it is. The values you have in each monthly row are mostly just repetitions of values in the basic price table. You could harvest the relevant values directly form such a (small) table instead of repeating them for each month in your main table.

Brian Barker