I am trying to set up a sheet that I can use as a template to sort data, find perticular values and create a chart. Everything is working fine for the maximum magnitude and average magnitude values I am looking for. I am running into an issue in the formula in the images attatched. Below is the full code.
=VLOOKUP(MAX(ABS(INDIRECT("J"&P3&":J"&Q3))),INDIRECT("J"&P3&":M"&Q3),4,FALSE)
With this code I am fining the maximum value in the vertical direction, then returning the corresponding magnitude value.
When I was just inputting the cell values it was working fine (taking a long time but it was working) however to make it quick and easy to apply to the other data files (they are in the same layout with the same number of values but have different values) I have adjusted it to the above formula.
In the P and Q columns I have found the row of the top and bottom of the ranges I am interested in and then have substituted these values into my working formula.
This method works great for the average magnitude and maximum magnitude so I know it should work, but when used the formula above it finds the incorrect value. For some reason it evaluates an array to a single value that is the second in the list.
1: evaluated formula before the miss-step (the array that is the range I am looking for)
2: evaluated formula after the miss-step (3.5 which is not close to the maximum absolute value)
Any help would be great.
You cannot convert the max(abs(...)) and use that for the lookup; that only works for positive numbers. You need to pass processing to a second VLOOKUP if the first fails with IFERROR.
You would be better off with a slightly more advanced sub-formula that can retrieve the absolute maximum than using an array formula to achieve the same.
Additionally, the non-volatile INDEX can replace the volatile INDIRECT.
As a non-array formula,
=iferror(vlookup(max(max(index(j:j, p3):index(j:j, q3)), abs(min(index(j:j, p3):index(j:j, q3)))), index(j:j, p3):index(m:m, q3), 4, false),
vlookup(-max(max(index(j:j, p3):index(j:j, q3)), abs(min(index(j:j, p3):index(j:j, q3)))), index(j:j, p3):index(m:m, q3), 4, false))
In other words, if you cannot find the max(abs(...)) then look for the -max(abs(...)).
Related
I am trying to find the closest value in this Poisson Distribution table to help create some variation in my NHL game simulator. Right now I have the minimum value set to =(MAX(H4:R14)/1.25) and the max as MAX(H4:R14). My rand (random) value is set to =RAND()*($V$4-$U$4)+$U$4. My question is, is how do you find the closest value in the table when compared to the random value? Returning the closest match (percentage) is ideal and the table's values will change whenever the teams change. Eventually, I am trying to return the respective column and row values (goals for each team), but this is step 1 and haven't been able to figure it out yet - even with index and match. Feel like it should be fairly straightforward...
You may benefit from SUMPRODUCT and array formula to get closes match and goals values in rows and section. Just as example:
The upper table would be your data. The bottom table is just to check it it works, you can delete. This bottom table is just an absolute value of cell minus your rand. The minimun one is highlighted in green and that's the closest match. Notice how the formulas get the position of that match and the goals values:
Formula to get ROW VALUE:
=SUMPRODUCT(--(ABS(H4:R14-U6)=MIN(ABS(H4:R14-U6)))*G4:G14)
Formula to get COLUMN VALUE:
=SUMPRODUCT(--(ABS(H4:R14-U6)=MIN(ABS(H4:R14-U6)))*H3:R3)
Both of this formulas are array formulas, so to introduce them yo must press CTRL+ENTER+SHIFT or they won't work!
To get the closest match is just INDEX classic formula (we add +1 due to goals indexed as 0,1,2...):
=INDEX(H4:R14,U9+1,V9+1)
See it working!:
I've uploaded the file in case you want to check the formulas:
https://docs.google.com/spreadsheets/d/1wV8bn6B-jZ4jCxAonGdWFMluAEybzwkA/edit?usp=share_link&ouid=114417674018837700466&rtpof=true&sd=true
Please, notice this will work properly as far as there is a single closest match. If by any case two cells are both closest match, formula will return incorrect result.
And remember, as I said before, second table is just to explain the solution, you can delete it and everything will work.
You could create a 2nd table where you calculate the absolute or squared difference between your Poisson table values and the random value.
Then you can simply check for the smallest value in the 2nd table.
In your example, the 2nd table of differences could be created with the formula
=ABS($H$4:$R$14-$U$6)
Let's assume that this 2nd table is just below the 1st table, i.e. in H16:R26
Then the smallest value in this 2nd table is obtained by
=SMALL($H$16:$R$26,1)
from there you can get the row number through
=SUM(($H$16:$R$26=SMALL($H$16:$R$26,1))*($G$4:$G$14))
and the column number through
=SUM(($H$16:$R$26=SMALL($H$16:$R$26,1))*($H$3:$R$3))
Then, to find the closest match in the original table, you can use the INDEX() function and refer to the row and column calculated in the previous step.
You can of course skip all the intermediary steps and write everything into a single formula:
=LET(
table,$H$4:$R$14,
diff,ABS(table-$U$6),
closest,SMALL(diff,1),
closest_row,SUM((diff=closest)*SEQUENCE(ROWS(table))),
closest_col,SUM((diff=closest)*SEQUENCE(1,COLUMNS(table))),
INDEX(table,closest_row,closest_col)
)
Which looks like this:
Note: the last formula uses dynamic arrays and functions only available Excel O365.
I am trying to make a formula that could count the max sum of any number of consecutive days that I indicate in some cell. Here is the dataset and the formula:
Dataset
The formula that calculates the maximum sum of three consecutive days:
=MAX(IFERROR(INDEX(
INDEX(E2:AI2,0)+
INDEX(F2:AI2,0)+
INDEX(G2:AI2,0),
0),""))
As you can see the number of days here is determined by the number of rows in the formula that start with "Index". The only difference between these rows is the letters (E, F, G). Is there any way I could reference a cell in which I could put a number for those days, instead of adding more rows to this formula?
Another approach avoding use of Offset is to use Scan to generate an array of running totals, then subtract totals which are N elements apart (where N is the number of consecutive cells to be added):
=LET(range,E2:AI2,
length,A1,
runningTotal,SCAN(0,range,LAMBDA(a,b,a+b)),
sequence1,SEQUENCE(1,COLUMNS(range)-length+1,A1),
sequence2,SEQUENCE(1,COLUMNS(range)-length+1,0),
difference,INDEX(runningTotal,sequence1)-IF(sequence2,INDEX(runningTotal,sequence2),0),
MAX(difference))
The answer here was posted by another user on another website, so I will repost it here:
One way to achieve this without relying on a VBA solution would be to use the BYCOL() function (available for Excel for Microsoft 365):
=BYCOL(array, [function])
The array specifies the range to which you want to apply your function, and the function itself is specified in a lambda statement. In the end, you want to get the minimum value of the sum of x consecutive days. Assuming that your data is stored in the range E2:AI2 and the number of consecutive days is stored in cell A1, the function looks like this:
=MIN(BYCOL(E2:AI2,LAMBDA(col,SUM(OFFSET(col,,,,A1)))))
The MIN() part ensures that you get only the smallest sum of the array (all sums of the x consecutive values) returned. The array is simply the range in which your data is stored; it is named in the lambda argument col and consequently used by its name. In your case, you want to apply the sum function for, e.g., x = 4 consecutive days (where 4 is stored in cell A1).
However, with this simple specification, you run into the problem of offsetting beyond cells with values toward the right end of the data. This means that the last sum you get would be 81.8 (value on 31 Jan) + 3 times 0 because the cells are empty. To avoid this, you can combine your function with an IF() statement that replaces the result with an empty cell if the number of empty cells is greater than 0. The adjusted formula looks like this:
=MIN(BYCOL(E2:AI2,
LAMBDA(col,IF(COUNTIF(OFFSET(col,,,,A1),"")>0,"",SUM(OFFSET(col,,,,A1))))))
If you do not have the Microsoft 365 version, there are two approaches that would also work. However, the two approaches are a bit more tedious, especially for cases with multiple days (because the number of days can not really be set automatically; except for potentially constructing the ranges with a combination of ADDRESS() and INDIRECT()), but I would still argue a bit neater than your current specification:
=MIN(INDEX(E2:AF2+F2:AG2+G2:AH2+H2:AI2,0))
=SUMPRODUCT(MIN(E2:AF2+F2:AG2+G2:AH2+H2:AI2))
The idea regarding the ranges is the same in both scenarios, with a shift in the start and end of the range by 1 for each additional day.
Another approach getting to the same result:
=LET(range,E2:AI2,
cons,4,
repeat,COLUMNS(range)-cons+1,
MAX(
BYROW(SEQUENCE(repeat,cons,,1)-INT(SEQUENCE(repeat,cons,0,1/cons))*(cons-1),
LAMBDA(x,SUM(INDEX(range,1,x))))))
This avoids OFFSET (volatile, slowing your file down) and the repeat value, consecutive number and/or the range are easily changeable.
Hope it helps (I answered to the max sum, as stated in the title). Change max to min to get the min sum result.
Edit:
I changed the repeat part in the formula to be dynamic (max number of consecutive columns in range), but you can replace it by a number or a cell reference.
The cons part can also be linked to a cell reference.
Also found a big in my formula which is fixed.
I have a series of paired measurements (two for each sample). I need to calculate the mean for each pair and then the general mean based on those pairwise averaged values, but I can only include those pairwise means that don't exceed a certain threshold.
I'm trying to work out an excel formula for the general mean that takes into account all of the above, without having to create additional rows/columns.
Now, the formula
=AVERAGE(IF(ABS(A1-A2)<$C$1,AVERAGE(A1,A2)),IF(ABS(A7-A8)<$C$1,AVERAGE(A7,A8)), ... )
... doesn't do the job because it replaces with zeros those pairwise means that exceed the threshold (value of $C$1), while they just need to be skipped. "AVERAGEIF" doesn't work because it doesn't accept non-contiguous ranges (at least when they are entered as such), using "INDIRECT" to build non-contiguous ranges doesn't help either because it doesn't accept formulas. Any help appreciated.
Not using helper column here is making formula look ugly but will give you the desired result. Have a try.
=AVERAGE(IF(CHOOSE({1;2;3},A1-A2,A4-A5,A7-A8)<C1,CHOOSE({1;2;3},AVERAGE(A1,A2),AVERAGE(A4,A5),AVERAGE(A7,A8))))
This is an array formula so commit it by pressing Ctrl+Shift+Enter.
See image to see the difference in the result from above formula and formula in question that is yielding incorrect result as you mentioned.
This is where Excel has some mysterious behaviour I think.
My first thought was to make each if statement return "" if the condition wasn't satisfied
=AVERAGE(IF(ABS(A1-A2)<$C$1,AVERAGE(A1,A2),""),IF(ABS(A7-A8)<$C$1,AVERAGE(A7,A8),""))
but this just gives you an error.
However, if you make it return a reference to an empty cell (say B1) it works fine
=AVERAGE(IF(ABS(A1-A2)<$C$1,AVERAGE(A1,A2),$B$1),IF(ABS(A7-A8)<$C$1,AVERAGE(A7,A8),$B$1))
I want to have a cell at the top of a column of data which uses a worksheet function to record the total number of cells below it which contain data. There are no gaps in the column, so I figure I don’t need to use COUNTA, it would be more efficient to find the first blank cell. To this end I have the following function in cell R12:
=MATCH(TRUE,INDEX(ISBLANK(OFFSET($R$12,1,0,1000,1)),0),0)-1
This worked fine until I tried to use a named reference cell to define the resized range a bit more flexibly… replacing the above with
=MATCH(TRUE,INDEX(ISBLANK(OFFSET($R$12,1,0,ROW(last_cell)-ROW(),1)),0),0)-1
gives #N/A! in the cell. As a formula =ROW(last_cell)-ROW() works fine on its own so it’s a puzzle to me why it doesn’t work in the compound formula… even replacing a 1 in the OFFSET parameters with ROW(A1) throws an error.
I can work round it, but this behaviour is really annoying! Can anyone shed any light on this?
The problem you are having is that the OFFSET function is expecting a long integer as its [height] parameter and you are shoving an array of integers at it. Yes, there is only one integer in the array but it is still an array and OFFSET is jumping ship at the first sign of potential trouble. If you evaluate the formula as suggested by Grade 'Eh' Bacon above, you will see that the result of that simple math subtraction is wrapped in braces (e.g. { and } ). You need to remove any indication that the [height] parameter is being fed an array or OFFSET will keep choking.
=MATCH(TRUE,INDEX(ISBLANK(OFFSET($R$12, 1, 0, MIN(ROW(last_cell)-ROW()), 1)),0),0)-1
There are any number of basic Excel worksheet functions that can take an array of 1 and turn it into an integer. I've used the MIN function. MAX, SUM, AVERAGE, etc. would all work. They take an array of numbers and return a single integer, even if that array of numbers has only one number.
On a related topic, I find it admirable that you are trying to reduce the calculation cycles in your workbook but you are missing one important consideration. The first thing you should do is throw out the OFFSET function altogether.
=MATCH(TRUE,INDEX(ISBLANK($R$12:INDEX($R:$R, ROW(last_cell)+1)), , ), 0)-1
OFFSET is a volatile formula that recalculates whenever anything in the workbook changes. Opting for the INDEX function equivalent takes the formula out of volatile mode and it will only recalculate when something that affects its outcome changes.
You may be interested in the way OFFSET erroneously treats floating point errors. See OFFSET_Floating_Point_Error for more on that.
Having had a chance to play around a bit, I'm still confused!
OFFSET itself doesn't seem to have a problem accepting the return values of ROW and COLUMN as parameters. To use a trivial example, this formula works:
=COUNTBLANK(OFFSET($R$12,ROW(1)+1,0,ROW(R20)-COLUMN(),1))
Trying different ways of eliminating OFFSET from the expression, I came up with:
=MATCH(TRUE,INDEX(ISBLANK(INDIRECT(ADDRESS(13,18)&":"&ADDRESS(1012,18))),0),0)-1
Which works, at the cost of swapping OFFSET for INDIRECT (which I'm hoping is the lesser of 2 evils!) However I would prefer to use:
=MATCH(TRUE,INDEX(ISBLANK(INDIRECT(ADDRESS(ROW()+1,COLUMN())&":"&ADDRESS(ROW(last_cell),COLUMN()))),0),0)-1
Which doesn't work, giving #N/A! again, as does changing any of the explicit integers to ROW or COLUMN expressions.
Individually, I've tried OFFSET, INDEX, ISBLANK and MATCH with ROW and COLUMN expressions and they all seem to work, so it seems to be something about using them in compound formulae which is throwing the error.
On the illustration all formulas are array. The range that each formula spans is bordered, and the first formula on each block is written on the top of that block.
Range A4:A103 is an input vector (which is numeric), range C4:G23 is a given (input) permutation of the rows of A4:A103 (necessarily positive non-zero integer numbers not greater then the length of the input vector).
Let us I interpret the permutation matrix as set of rows.
How to compute for each row in a constant number of cells the minimal number in the input vector? By the constant number of cells, I mean solution, that would require fixed number of cells for each row, regardless of the number of columns in permutation. (In the production case each dimension is much, much bigger; there is about 100 columns in the permutation matrix.)
I don't ask for VBA solutions. If it is necessary the solution can use a free and publicly available Excel add-on, like MoreFunc, but I'd prefer to keep it vanilla Excel 2007 or later.
I thought that the formula {=MIN(INDEX(INDIRECT($A$2);$C4:$G4))} would solve my problem. Surprisingly, Excel seems to not take into account the array nature of the formula, and evaluates it as if it was written as =MIN(INDEX(INDIRECT($A$2);$C4) which is equivalent to dysfunctional =INDEX(INDIRECT($A$2);$C4).
On the other hand, we can see the argument to the MIN is understood as array in the range I4:M4.
INDEX works in some strange ways!
Normally INDEX can't return an array - although you seem to have found the one exception to that - when it's an array formula entered in a range.
You should be able to use OFFSET to return the required array that will work within MIN, i.e. with this formula
=MIN(N(OFFSET(INDIRECT($A$2);$C4:$G4-1;0)))
confirmed with CTRL+SHIFT+ENTER