Calc Function Wizard - IF Result differences

Hi,

Entering a formula into the Function Wizard in Calc shows up two different answers in the 'Result' and 'Function Result' answer box.

If the editing cursor is placed in a certain section of the formula, only the 'Function Result' answer box holds the correct answer i.e. 751.52004. Pressing 'OK' puts the 'Result' answer box value in the calculated cell, which unfortunately is the wrong answer i.e. 751.25.

I have looked at the help file to see if there was a difference between the Result answer boxes but found none.

The initial formula is:
=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))))))))))

In addition I have tried changing the formula without success to try and reduce the multiple nested IF functions:

i.e.:
=sum((IF(0<G4<=$G$20,G4*$F$20,0),(IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),0),(IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),0),(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),0),(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)),0)

The SUMIF function was also tried but my brain just gave up.

I cannot see how the VLOOKUP function will help either as I am not looking up values from cells, I am calculating a single value in H4 based in a variable input in G4.

Have a peak at the uploaded spreadsheet on Nabble:

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

You will see that provided you enter a value for G4 below 600 the correct answer is shown i.e. for 600 in G4 H4=750. The moment G4 exceeds 600, even by 1, there is a problem.

There must be an answer and or my formula syntax is wrong unbeknown to me.

Help would be very much appreciated

Sincerely
Hylton

Hylton,

If you can breakup the formula into smaller units you might find the
problem.

Often when I have had a similar problem I found the my problem
was I reversed test in an IF clause.

Hi,

The initial formula is:
=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))))))))))

this is waaay too convoluted to be easily checked.

There must be an answer and or my formula syntax is wrong unbeknown to me.

Sure, there is. As your request looks very much like your previous one,
I highly encourage you to read Brian's answer to that one: it's filled
will good advice. IOW, when you can write your problem down using common
speech (not some spreadsheet language), I'm sure you'll turn having
found the solution by yourself.

-> I share Brian's idea about using VLOOKUP() through a 3 lines set of data.

HTH,

Entering a formula into the Function Wizard in Calc shows up two different answers in the 'Result' and 'Function Result' answer box.

Certainly: there would otherwise be no point in having two boxes. Suppose you use the Function Wizard to construct =3+SQRT(4). The function result is 2 but the formula result is 5.

In addition I have tried changing the formula without success to try and reduce the multiple nested IF functions:
i.e.:
=sum((IF(0<G4<=$G$20,G4*$F$20,0),(IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),0),(IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),0),(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),0),(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)),0)

I cannot see how the VLOOKUP function will help either as I am not looking up values from cells, I am calculating a single value in H4 based in a variable input in G4.

But your formula refers to the values in row 20, which are thus the values you need to look up. Your IF conditions are your current way of selecting the appropriate values from that row.

Have a peak at the uploaded spreadsheet on Nabble:

Will do - but I'll need more than a peek!

You will see that provided you enter a value for G4 below 600 the correct answer is shown i.e. for 600 in G4 H4=750.

I think this may be more by luck that judgment, as the prices for your first three ranges are identical - which doesn't test your formula effectively.

There must be an answer and or my formula syntax is wrong unbeknown to me.

Oh, the formula is certainly wrong. There will be a right one ...

Brian Barker

As I noticed you're practically around the corner from me, I got
interested, and had a look at your spreadsheet :slight_smile:

[tl;dr: I got solutions, but need some help uploading the file. Also,
it may be that you could work it out in a *much* simpler way, using
what you already have, but a) something is broken in your other
formulas, and b) that's if they are supposed to do what I think they
are.]

I can see what you are trying to do, but it took some figuring out. I'm
not convinced you have everything laid out correctly in that file. For
example, your "Max Units" column has a complicated calculation that
doesn't actually just figure out the difference between the two
"brackets" (as I called the groups). For example, between the 0->150
bracket and the 150->300 bracket, the difference should be 300 - 150,
but instead you calculate something using an IF. I don't know why, but I
wasn't really focusing on that, it just seems odd (and possibly
incorrect) to me.

As for your problem at hand. The formula you are using is waaaaay too
complex. I would not use something like that, I would either split it
up into more columns, or write a user function for it. I actually
played a little with it to figure out how it worked, and ended up
amending your spreadsheet to include both solutions.

As I understand the problem, you have a value that needs splitting up
into the different price brackets, and then a per bracket price is
calculated, and you need the total of the per bracket prices. The split
is by amount, that is to say the first bracket is the first 150 units of
the value, the second bracket is the next 200 units, and so on. Each
bracket has a unit price, and once you figure out how many units of the
value falls into each bracket, you can work out the price for those
units, and then total it to get a final price for the value. Sorry if
that is a convoluted explanation, but it's the best way I could think
of to describe it.

It is not clear to me how VLOOKUP could be used for this, as the
problem as I understand it isn't one of matching your cell to a given
list of criteria and inserting a corresponding value.

I also can't explain my solutions here, as I haven't simply "fixed"
your formula. I have saved your file with both solutions. I would
gladly upload it for you to look at, but I'm not sure how to upload
files. I know I can't do it via email, as attachments apparently get
stripped, and I can't see how to do it from nabble, unless you only get
that option when logged in. If so, what are my login details? As I'm
subscribed to the list, I would assume that means I have a login for
nabble, or is it a separate thing? All I know about nabble is that it
has to do with the mailing lists...

The user function solution is elegant, and simply replaces your formula
with a user function call, and the user function is written in LO
Basic. Very straight forward. I have only used your original columns
as input to that function. I have assumed only four price brackets, so
if any more are added, the function calls and the function itself will
need some amending. The other solution involves adding a whole bunch of
columns to your spreadsheet to calculate intermediate steps. This does
mean no additional coding skills required, and the benefit is each step
is then simple and clear, your solution can be easily checked, and you
have the break-downs by price bracket. Again four brackets are assumed,
but should more be made, you can simply add more columns, which you
would have to do anyway. The downside is more columns, but as the ones
I added all contain formulas, they can be hidden once the formulas are
copied to all relevant rows.

Note that I messed around a bit with the spreadsheet, possibly removing
some formulas in the process, so don't simply use the spreadsheet as I
give it to you. Read and understand my solution, then implement it in
your spreadsheet. The user function code can probably be copied
straight out, though.

So yes, I have a solution for you, I just need some assistance
uploading it. The moral of the story, though, is that you shouldn't try
to make such convoluted formulas. Use columns for intermediate steps,
and keep each step simple. Or use user defined functions if you must
have it in one step and have some coding skills. If there is a simpler,
built-in function that can do this, or that can simplify your formula to
manageable proportions, I don't know what it is.

Paul

P.S. Looking at your "Max Units" column now, it seems like this may be
one of the values I am calculating, so my solutions could have been a
little simpler. However, this is not clear to me, as those cells
calculate their value based on values in their row, and not in the row
of the "Last Day Closing Balance" column. I assume the two sets of rows
will contain the same data, but I don't know that for sure. If so, why
don't you simply total up the "Rand Value" columns in the second set of
rows?

And looking at this shows even more problems with the spreadsheet.

For one, there is a row above the month rows with values, what are they
for? Now that I look closer, I see why they are there, but between them
and the column headers, things are unclear. I guess that doesn't matter
so long as you understand it, but it makes it not obvious to anybody
trying to help you. It does mean my solutions could have been simpler.
But taking out the duplications I have made is an exercise I leave to
the reader :slight_smile:

Secondly, there is a problem with your formula in the "Over 600 units"
column. That formula gives an incorrect (as far as I understand its
purpose) result, meaning that you can't simply total up those columns to
achieve your goal. Or maybe I don't understand its purpose. More
problems with the clarity of your headings, and the complex formulas.

Thirdly, don't use "SUM" for simple calculations. It seems to work, but
I think it's a bad idea. Instead of "=SUM(C4:C4)" simply use "=C4" and
for "=SUM(Y21*W21)" simply use "=Y21*W21".

And if you can fix the formula so that you can simply total up that
row, then it just shows that the whole thing is so complex that even
you didn't see that there was a *very* simple solution to your problem.
Having two sets of rows for the same data does mean possible overlaps
in functionality. Perhaps the solution isn't to fix your formula, but
to go over the whole spreadsheet, and simplify, or at least
re-evaluate the purpose of, each column. You might find you have more
of the intermediate steps than you realise, they're just badly named.

Whew, this email has become waaay longer than I planned. And sorry, it
wasn't meant to sound too critical. I think it's great that you're
putting this all together, and I realise that what seems confused to me
makes perfect sense to you. But when I see the formula you had problems
with, and how much of the intermediate steps you may actually have had
but I couldn't initially find, it just suggests that your thinking about
the problem has become over-complicated, and you need to take a step
back and re-think it all, in a step by step manner. Happens to me
all the time.

Hi,

Entering a formula into the Function Wizard in Calc shows up two
different answers in the 'Result' and 'Function Result' answer box.

If the editing cursor is placed in a certain section of the formula,
only the 'Function Result' answer box holds the correct answer i.e.
751.52004. Pressing 'OK' puts the 'Result' answer box value in the
calculated cell, which unfortunately is the wrong answer i.e. 751.25.

I have looked at the help file to see if there was a difference between
the Result answer boxes but found none.

The initial formula is:
=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))))))))))

In addition I have tried changing the formula without success to try and
reduce the multiple nested IF functions:

i.e.:
=sum((IF(0<G4<=$G$20,G4*$F$20,0),(IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),0),(IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),0),(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),0),(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)),0)

The SUMIF function was also tried but my brain just gave up.

I cannot see how the VLOOKUP function will help either as I am not
looking up values from cells, I am calculating a single value in H4
based in a variable input in G4.

Have a peak at the uploaded spreadsheet on Nabble:

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

You will see that provided you enter a value for G4 below 600 the
correct answer is shown i.e. for 600 in G4 H4=750. The moment G4 exceeds
600, even by 1, there is a problem.

There must be an answer and or my formula syntax is wrong unbeknown to me.

Help would be very much appreciated

Sincerely
Hylton
--

The spreadsheet structure you are using is not so adequate for this
calculation.

I have a suggestion for your formula in H4. But, before I post it,
you should know that there are better possibilities, and that my
suggestion doesn't solve all your problems in that spreadsheet.
Additionally, the formula I am suggesting might not be optimized, and
it is certainly not the only solution.

Anyway, in your posted spreadsheet, Electricity.ods -> Data2 sheet ->
Cell H4, I suggest the following formula as one possibility:

=(0<G4)*(G4<=$G$20)*(G4*$F$20)+($G$20<G4)*(G4<=$G$20+$J$20)*($G$20*$F$
20+(G4-$G$20)*$I$20)+($G$20+$J$20<G4)*(G4<=$G$20+$J$20+$M$20)*($G$20*$
F$20+$J$20*$I$20+(G4-($G$20+$J$20))*$L$20)+($G$20+$J$20+$M$20<G4)*($G$
20*$F$20+$J$20*$I$20+$M$20*$L$20+(G4-($G$20+$J$20+$M$20))*$O$20

Hopefully, the email width format won't mess up my intention (a reply
to this email probably will).

Best Regards,
Ady.

You can do what you need using VLOOKUP(), seeking values from a small table (much as you already have in rows 58 to 61). And you can probably more easily use MATCH(), which works fairly similarly, but returns a pointer instead of a value. But both of these methods suffer from the problem that you need to seek multiple values from your table in each calculation, so the formulae become rather cumbersome, with a necessity to retrieve essentially the same information repeatedly.

It's worth saying that it's generally not a good idea to repeat similar information - as you have at lines 21 to 32. Instead, either use values from a minimal table (as already mentioned) or embed the values in the formulae.

Let's redraw your table more simply - say in rows 71 to 74 of columns A and B:
         1.25
150 1.25
350 1.25
600 1.52
(Nothing is needed in A71.)

Then you can calculate what you need using something like:
=MIN(G4;$A$72)*$B$71
+MAX(0;(MIN(G4;$A$73)-$A$72)*$B$72)
+MAX(0;(MIN(G4;$A$74)-$A$73)*$B$73)
+MAX(0;(G4-$A$74)*$B$74)
where G4 is your sample data cell.

Each line of this formula (I've split it only for ease of reading, of course) calculates the contribution to the cost from one of the four price bands. Each MIN() function ensures that the upper limit of the quantity is either the value itself or the upper limit of the band - whichever is smaller. So where are the IFs to test which bands are needed?, you ask. Well, each unnecessary contribution would be calculated here (wrongly) as negative, so the MAX() functions, with their first arguments of zero, cancel these incorrect contributions. Try it: it gives the answers you want.

I trust this helps.

Brian Barker

Ok, so I just used a file hosting service found via Google search.
Here's the file I modified with my two solutions:

http://www.filedropper.com/electricity_1

It didn't work the first time or two that I tried to download the file
to check, not sure why, but it worked the third time, so maybe
persevere if it doesn't work first time for you.

When I tried to open the file again, I got told the security settings
prevented macros from running, and the cells that used the user
function to calculate your answer showed "#VALUE", I think it was. You
can reduce the security settings to allow macros to run, of course, but
perhaps this is not the best way forward after all, although it is
still an elegant solution for some values of elegant.

So if using just formulas, I liked Brian's use of MAX(0, value) instead
of my IF(value > 0, value, 0), so I would probably substitute that
first off, to make the formulas I've used more legible. I would still
strongly recommend intermediate steps for each line of Brian's formula,
and then one simple SUM to get the final answer.

=MIN(G4;$A$72)*$B$71
+MAX(0;(MIN(G4;$A$73)-$A$72)*$B$72)
+MAX(0;(MIN(G4;$A$74)-$A$73)*$B$73)
+MAX(0;(G4-$A$74)*$B$74)

Also, while Brian has, as I understand it (without actually trying it
out), combined the information into a simple table, much as I first felt
should be done, this does mean the price brackets can't change per
month, while my solution does allow for this. Your choice.

Hope this helps, and feel free to ask me for any clarifications of what
I've done.

Paul

P.S. On a separate note, how does one sign macros for use with LO? I've
come across the same issue with Excel, and never got round to finding
out. If one can sign the macros, can the end user choose which signed
macros to trust, or does one essentially have to buy a
trusted certificate, and end users only get the option of trusting all
such certificates? This is how I understand Excel to work, though I may
be wrong.

I really feel there should be an easy way to generate "self-signed
certificates" for one's own macros, and end users should be given a
white-list of certificates which they can trust, such that you can
create macros and sign them, and simply tell your end users to add your
certificate to their trust list, and all macros from you will be run
without questions, while all other macros will not be run, without the
end user having to choose to allow your macros each time, like medium
security caters for. Perhaps this should be a feature request? Or am I
horribly missing the point of macro security?

<trimmed>

I have replied to Paul privately and am looking forward to learning a bit more on the user formula he mentions. Any other pointers would be appreciated especially as he also indicates that using VLOOKUP would not work, in this case. MIN and MAX are still under testing here :slight_smile:

I am however still waiting for someone to indicate to me which part of the formula in the Formula Wizard the boxes apply i.e. what is the difference between the 'Result' and 'Function Result' answer box.

Regards
Hylton

appreciated especially as he also indicates that using VLOOKUP would
not work, in this case. MIN and MAX are still under testing here :slight_smile:

Brian indicated that VLOOKUP would work; I don't say different, I'm
just not sure how in this situation. I'm not that familiar with VLOOKUP
though, so it may not be that difficult, but it seems like it would
still be complicated. I still feel that the best approach is to
put columns for intermediate steps; VLOOKUP might make more sense after
that.

I am however still waiting for someone to indicate to me which part
of the formula in the Formula Wizard the boxes apply i.e. what is the
difference between the 'Result' and 'Function Result' answer box.

The "Result" box shows the result of the complete formula for the cell,
while the "Function Result" box shows the result of just the function
currently under the cursor. For example, let's say you have typed
"=SQRT(SUM(A1:A2))", and A1 was 4 and A2 was 5. If you put your cursor
on "SQRT", both boxes would hold the answer 3, as that is both the
final result of the formula, and the result of "SQRT(9)". If, however,
you put your cursor on "SUM", then the "Result" box would still show 3,
as that is still the result of the complete formula, but the "Function
Result" box would now hold 9, as that is the result of "SUM(A1:A2)".

Paul

Let me rather put it this way: I don't think using VLOOKUP solves the
underlying problem, which is that too much is being done in one
formula.

For any complicated formula, I feel the best solution is either a
custom written user function, or intermediate steps in columns. Hide
those columns, or put them on another worksheet if you don't want to
see them, but they should still be there. Makes things much easier.

Paul

Another trick that occurred to me when viewing that spreadsheet was
this one: Some of the column headers contain values that are needed in
calculations. Say for instance you have a column header in cell A1 that
reads "Max units 250". You can extract the value (250) with

"=VALUE(RIGHT(A1,3))"

Or, you can simply put 250 in the cell, and custom format it with this
format string:

"Max units "###

This way it shows as "Max units 250", but the cell contents is just
250, and can be used in calculations.

Neat trick.

Paul

P.S. Note that there is an issue across saves when using this trick to
format a number as a per hour rate, at least in the South African
locale. There's been a previous thread about this, and I still need to
file a bug report. Basically if you use a cell format like

"R "#,##0.00 " / hour"

It will be fine until you save, but things get messed up when you
reopen your document.

Jay,

Hylton,

If you can breakup the formula into smaller units you might find the
problem.

Often when I have had a similar problem I found the my problem
was I reversed test in an IF clause.

Thanks, I have checked all the IF tests and mathematically they are correct.

Hi,

The initial formula is:
=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))))))))))

this is waaay too convoluted to be easily checked.

I would agree, but it should still work.

There must be an answer and or my formula syntax is wrong unbeknown to me.

Sure, there is. As your request looks very much like your previous one,
I highly encourage you to read Brian's answer to that one: it's filled
will good advice. IOW, when you can write your problem down using common
speech (not some spreadsheet language), I'm sure you'll turn having
found the solution by yourself.

-> I share Brian's idea about using VLOOKUP() through a 3 lines set of data.

I have no problem using VLOOKUP but this data does not lend itself towards using VLOOKUP, to my minor knowledge.

If someone could give me more of an explained example of VLOOKUP using the pricing structure and unit limits on each, that can be expanded across 12 months, I will look into it further.

Entering a formula into the Function Wizard in Calc shows up two
different answers in the 'Result' and 'Function Result' answer box.

Certainly: there would otherwise be no point in having two boxes.
Suppose you use the Function Wizard to construct =3+SQRT(4). The
function result is 2 but the formula result is 5.

Tnx, Now I see the difference i.e. the Function result is of the function/formula the cursor is on whilst the result is the end answer.

In addition I have tried changing the formula without success to try
and reduce the multiple nested IF functions:
i.e.:
=sum((IF(0<G4<=$G$20,G4*$F$20,0),(IF($G$20<G4<=$J$20,(G4-$G$20)*$I$20+($F$20*$G$20),0),(IF($J$20<G4<=$M$20,(G4-$G$20-$J$20)*$L$20+(($G$20*$F$20)+($J$20*$I$20),0),(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),0),(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)),0)

I cannot see how the VLOOKUP function will help either as I am not
looking up values from cells, I am calculating a single value in H4
based in a variable input in G4.

But your formula refers to the values in row 20, which are thus the
values you need to look up. Your IF conditions are your current way of
selecting the appropriate values from that row.

Have a peak at the uploaded spreadsheet on Nabble:

Will do - but I'll need more than a peek!

You will see that provided you enter a value for G4 below 600 the
correct answer is shown i.e. for 600 in G4 H4=750.

I think this may be more by luck that judgment, as the prices for your
first three ranges are identical - which doesn't test your formula
effectively.

This minor statement about the price ranges being identical caused me to test by starting the first at 1 and incrementing each range by one until 4. Changing the value in G4 made NO change to my calculated field <H4>, so guess its time to revisit which formulae to use :frowning:

There must be an answer and or my formula syntax is wrong unbeknown to
me.

Oh, the formula is certainly wrong. There will be a right one ...

Still searching for the needle in the haystack...

Hi :slight_smile:
Uploading files to Nabble is usually easier. Either click on the links in
this email, or navigate from
LibreOffice.org
(errr, or google-it using "LibreOffice Nabble" (or something) for your
search)

Just above the space to entire your "Message" is a row of buttons including
one called "More". The top option in "More" is to "upload file" and that
gives you a "Browse" option that is a lot like attaching things to emails.
However, when you have uploaded the 'attachment' it injects a line of html
'code' into the message and you can move that around a bit if you want. If
you know html 'coding' then you can also edit the text that people would
click on to reach your attachment but it's usually pretty good at giving it
a reasonable name.

Regards from
Tom :slight_smile:

Hi Paul,

Ok, so I just used a file hosting service found via Google search.
Here's the file I modified with my two solutions:

http://www.filedropper.com/electricity_1

It didn't work the first time or two that I tried to download the file
to check, not sure why, but it worked the third time, so maybe
persevere if it doesn't work first time for you.

When I tried to open the file again, I got told the security settings
prevented macros from running, and the cells that used the user
function to calculate your answer showed "#VALUE", I think it was.

Ditto but ignored it. :frowning:

You
can reduce the security settings to allow macros to run, of course, but
perhaps this is not the best way forward after all, although it is
still an elegant solution for some values of elegant.

There is always sending it as an email attachment :slight_smile:

So if using just formulas, I liked Brian's use of MAX(0, value) instead
of my IF(value > 0, value, 0), so I would probably substitute that
first off, to make the formulas I've used more legible. I would still
strongly recommend intermediate steps for each line of Brian's formula,
and then one simple SUM to get the final answer.

=MIN(G4;$A$72)*$B$71
+MAX(0;(MIN(G4;$A$73)-$A$72)*$B$72)
+MAX(0;(MIN(G4;$A$74)-$A$73)*$B$73)
+MAX(0;(G4-$A$74)*$B$74)

Also, while Brian has, as I understand it (without actually trying it
out), combined the information into a simple table, much as I first felt
should be done, this does mean the price brackets can't change per
month, while my solution does allow for this. Your choice.

I like Brian's MIN/MAX idea too but the price might well need to change on a month, it just depends on when the local authority increases prices.

Hope this helps, and feel free to ask me for any clarifications of what
I've done.

Time for coffee. My brain is getting frazzled going thru each of the replies. I am very thankful for them though.

Hylton
P.S: Like the idea of Macro security.

The initial formula is:
[...]

this is waaay too convoluted to be easily checked.

I would agree, but it should still work.

Any formula that cannot be effectively checked cannot be *relied on* to work, of course.

-> I share Brian's idea about using VLOOKUP() through a 3 lines set of data.

I have no problem using VLOOKUP but this data does not lend itself towards using VLOOKUP, to my minor knowledge.

If someone could give me more of an explained example of VLOOKUP using the pricing structure and unit limits on each, that can be expanded across 12 months, I will look into it further.

As you now, I've now abandoned VLOOKUP() as being the easiest or best way of solving your problem, but to answer your question, my idea was that you would have a separate small table of the price bands and values (instead of repeating them for each month), let's say in A1 to B4:
    0 1.25
150 1.25
350 1.25
600 1.52

Now an expression such as VLOOKUP(X;A1:B4;2) - where X represents your input value - will find the price for the band including your value. The function searches the A column for the value, finds the highest band boundary not exceeding that value, and then returns the corresponding value from the B column (the second column because of the "2"). That's a start, but the problem is that you'd need to use the idea repeatedly even for each input value, as you need to include values from lower price bands in your calculation too. The MATCH() function is similar, returning the row number instead of a value, and you could use that - but with the same drawback. With these schemes, you would not repeat the price band values across each monthly row but refer back to the small table in the formula for each month's calculation - which I think is a better structure in any case.

In the end, it's easier, I think, to use MAX(0;...) to switch on or off the contribution from each band to the calculation.

Brian Barker

I like Brian's MIN/MAX idea too but the price might well need to change on a month, it just depends on when the local authority increases prices.

That idea is equally applicable to your current arrangement - with the price band values repeated for each month. Or you could embed the values into the formula.

Time for coffee.

Sounds a good scheme.

Brian Barker

I still think the VLOOKUP function can help tremendously but it does, in practice, need one or two more columns in the lookup table.

The table as it exists has the number of units in each range, rather than the values at the breakpoints. These breakpoint values are vital, as they are what are needed to make VLOOKUP work in the first place. They can be calculated, based on the units per range, but the actual numbers must be in the first column of the lookup table.

The other column that would help immensely is the summed amount for all the lower-level ranges. The best illustration I can think of from the US income tax calculation:

Looking at only the rates for income over $100,000 (for smaller amounts the tax is looked up in a table rather than calculated), for one filing status, we see that the marginal tax rate is:

     25% for taxable income > $100,000 and <= $142,700
     28% for $142,700 < income <= $217,450
     33% for $217,450 < income <= $388,350
     35% for income > $388,350

In the past the worksheets would have shown the total tax at the top of the previous bracket, plus the rate on the income above the threshold. For example, if the income was $150,000 the worksheet would have said $27,729 + 28% of the amount over $142,700. This can be replicated in the lookup table.

The worksheets now start with the marginal rate and subtract another value representing the difference in rates for the lower income. That same range now says 28% minus $12,221. The answer is the same, just arrived at differently -- and can still be replicated in the lookup table.

I'm not sure how all this fits into the 12-month consideration introduced after the original question, but I hope it explains how VLOOKUP can make the conditionals a lot easier -- if not for this particular case, then for someone else with a similar question.

Dave Liesse