Computing conditional SLOPE - excel

I have a simple spreadsheet, where column A is a bunch of dates (ascending) and column B is a bunch of values. Finding the OLS slope is easy:
SLOPE(B2:B161,A2:A161)
But I don't want the slope of everything. I want to see the slope for each month. So if C3 is "3", I'd want to do something like:
SLOPE(IF(MONTH(A2:A161)==C3,B2:B161), A2:A161)
Which is wrong, but hopefully conveys what I'm trying to do. How do I actually do this?

You've got the right idea more or less. Try the following, but instead of just pressing enter to commit the value to the cell, press Control+Shift+Enter (CSE) which turns it into an array formula and makes it behave the way you'd like.
=SLOPE(IF(MONTH(A2:A161)=3,A2:A161),B2:B161)

Related

Excel - equivalent of Javascript reduce function?

Is there an Excel equivalent of the Javascript reduce function? Essentially, I want to apply a function to each cell in a range, and then sum up each result.
The reason I ask is because I have the following sheet:
For each expense, I'm calculating the "change", i.e. the amount needed to round it up to the nearest pound. I'm then summing that at the bottom.
I want to be able to do this without having the extra column and summing those values.
Thanks.
Use SUMPRODUCT(it avoids the need of Ctrl-Shift-Enter) and ROUNDUP(just shorter, personal preference):
=SUMPRODUCT(ROUNDUP(C3:C7,0)-C3:C7)
OK, so I just found out there's something called an array formula where you type your formula and then press Ctrl+Shift+Enter (and it'll put curly brackets around your formula to show that it worked).
So for my example above, I'd type
=SUM(CEILING.MATH(C3:C7)-C3:C7)
Then press Ctrl+Shift+Enter and it'd change it to
{=SUM(CEILING.MATH(C3:C7)-C3:C7)}
and that gives me the £2.11 I'm expecting.

How to correctly formulate a sum function with range in excel

I'd like to write this function in excel:
It's a function that calculated how much experience is needed for a particular level.
level 2 is 83xp, level 99 is over 13,000,000 for example.
I have all the levels arranged in this way:
I just can't figure out the excel formula to put into cell 4B to calculate the function in the image. I'm thinking it should be something like
=floor(sum[1 to cellToLeft.value - 1](floor(x + 300*2^(x/7))/4))
but that's not quite right
This seems to work for me:
{=FLOOR(SUM(FLOOR(R4C[-1]:RC[-1]+300*2^(R4C[-1]:RC[-1]/7),1))/4,1)}
Make sure you enter as an array formula.
If you don't use R1C1 mode then you need something like this:
{=FLOOR(SUM(FLOOR(A$4:A12+300*2^(A$4:A12/7),1))/4,1)}
This will be an array formula in excel answer. Place the level you want (the value for L) in N4, or change the reference to suit your needs. Enter the following formula in the cell you want the answer to be in remembering to press CONTROL+SHIFT+ENTER when finishing the formula instead of just ENTER. You will know you have done it right when {} appear around your formula in Excel.
=FLOOR(SUM(FLOOR(ROW($A$1:INDEX(A:A,$N$4-1))+300*2^(ROW($A$1:INDEX(A:A,$N$4-1))/7),1)/4),1)
Level 1 will cause an error since L-1=0. Since I am assuming it takes 0 for level 1, I threw in a error trap. anytime the formula returns an error it give a value of 0 instead. With the following formula you can change the $N4 reference to where ever you have your level 1 value. remember to use C+S+E when you enter the formula and copy downward as far as your levels go.
=IFERROR(FLOOR(SUM(FLOOR(ROW($A$1:INDEX(A:A,$N4-1))+300*2^(ROW($A$1:INDEX(A:A,$N4-1))/7),1)/4),1),0)

How can I use functions like NUMBERVALUE() across a whole column/range in Excel?

I have a column of data that I want to get the average of. (F column below, GREEN)
Unfortunetely, each cell has the unit in it, thus I cannot simply use AVERAGE().
Before I continue, please note that I'm not asking for a better way of removing the units (That's a different question.)
Also note that in my included image, just below each formula is the formula in text.
My first attempt at getting the average was to make another column next to it using =LEFT(F,3) (Column L, RED) however trying to take the average of that failed (Column N, PINK), presumably because the result was a text string.
So I then tried using wrapping LEFT() in NUMBERVALUE(), giving me =AVERAGE(NUMBERVALUE(L17:L36)) but that just returns the that row's value (Column N, PURPLE)
Next I tried using doing the whole thing at once using =AVERAGE(NUMBERVALUE(LEFT(G17:G36,3))) (Column N, GREY) but that obviously wasn't going to work either.
Finally, I made a column using =NUMBERVALUE(LEFT(F36,3)) (Column G, BLUE) and then taking the AVERAGE() of that (Column I, GOLD).
That worked, but how can I use NUMBERVALUE() and LEFT() for a whole column?
I want something like =AVERAGE(NUMBERVALUE(LEFT(G17:G36,3))) to work.
Your formula of =AVERAGE(NUMBERVALUE(LEFT(G17:G36,3))) is actually correct. However you are missing one step, you need to convert it to an array formula. After entering in the formula you need to make sure you hit Ctrl+Shift+Enter to tell excel you want to do an array formula or Command+Enter on a Mac.
That should do it for you.
The formula you suggested, entered as an Array Formula will work
=AVERAGE(NUMBERVALUE(LEFT(G17:G36,3)))
completed with Ctrl-Shift-Enter.

Apply Function to every cell in Excel

I have Terz-spectra data from acoustic measurements and I want to calculate the overall level. The values for each Terz-band are in decibel. Therefor the calculation formula is a bit lengthy. It would be L_overall=10*log10(10^(L_a/10)+10^(L_b/10)+10^(L_c/10)+10^(L_d/10)...).
As there are many frequencies, I don't want to write the whole formula by hand. I also don't want to use another column to do the inner calculation (10^(x/10)) before doing the outer calculation (10*log10(x+y+z+...)). So I wonder if there is a function in Excel that allows to apply a calculation on each cell like there is in Matlab for instance (cellfun, arrayfun).
I hope to end up with something similar like L_overall=10*log10(10^(B10:B80/10) which would allow me to do all the calculation in simply one cell.
Addition:
I have forgotten to say that I want the terz-data unaltered. I don't want to change that as I have to use it to plot that spectrum. So ideally I want all the work done in only one cell. I could do that by the lengthy formula, but it would take quite some time to type that for all frequencies.
Of course I could simply use another column and do the math there but that seems also inconvenient.
Actually the formula would be something like this:
=10*LOG(SUM(INDEX(10^(B10:B80/10),)),10)
Or this:
=10*LOG(SUMPRODUCT(10^(B10:B80/10)),10)
It's a bit clunky, but you could do 10^(L_a/10) for one cell, then click and drag the lower right corner to make that run for all cells.
Then create another cell which takes 10*log10 of the SUM of those cells.
If your goal is only to insert a specific formula in a bunch of cells you could use this:
For Each rCell In Application.Selection
rCell.Formula = "=10*log10(10^(L_a/10)+10^(L_b/10)+10^(L_c/10)+10^(L_d/10)...)"
Next rCell
Simply select all the cells in which you want to insert the formula and then run the sub. Make sure your formula is valid.

how to calculate the products of various cells with irregularity

I have many cells to multiply and add. But i cannot get a formula to do so. Can you help.
The example is:
I have a weight and rate cells just next to each other which needs to be multiplied and the subsequent weight and rate is also in the same row. Like this I have more than 40 cells to multiply and add the sum together.
There will be many cells without any value and there will be negative cells for which I do not want the calculation to happen.
Can you please help!
I'm not completely clear on how your data is organized - I'm assuming it's in columns. You could do something like this, entering as an array-formula (typing ctrl-shift-enter instead of just enter):
=SUM(IFERROR(IF(ratearray>0,ratearray*1,0),0)*IFERROR(IF(weightarray>0,weightarray*1,0),0))
See an example:

Resources