Array formula and MAX value of 2 cells - google-sheets

I'm trying to get max value between 2 cells (one is same column, previous row, another is in the same row, but different column) with an array formula. I use array, because I need it to be updated automatically row after row with each new entry in column A without adding formula to new cells each time.
Just MAX formula is following:
I3512=MAX(I3511,C3512)
This array formula gives same result in each following cell
I3513 = ARRAYFORMULA(IF(ISBLANK(A3513:A),,MAX(I3512,C3513)))
This formula also, gives me the same result in each row, as counting the max from C column.
I3513 = ARRAYFORMULA(IF(ISBLANK(A3513:A),,MAX(I3512:I,C3513:C)))
Also I tried this version, but it fails in the middle, showing wrong values.
I3513 = ARRAYFORMULA(IF(ISBLANK(A3513:A),,if(I3512:I<C3513:C,C3513:C, I3512:I)))
How to apply MAX formula to all rows in column I after row 3513, finding between previous cell and another column same row.
Suggested formula result

if just two columns use IF in row 3513:
=ARRAYFORMULA(IF(A3513:A="";; IF({I3512:I; ""}>C3513:C; {I3512:I; ""}; C3513:C)))

Use OFFSET
For example:
C3 =
{
"MAX PNL";
"N/A";
ArrayFormula(
IF(
OFFSET(A3:A1000, -1, 0) > B3:B1000,
OFFSET(A3:A1000, -1, 0),
B3:B1000
)
)
}
Bear in mind with this technique you can't use ranges like A3:A as this will give you an error:
This is because since it references two rows, it gets confused and always requires more rows.

Related

Google Sheets keep max value constant

Refer the sheet in the figure
For the H column, I want Hj = Gj+MAX(F)-Fj , where j = row number.
The given formula works for the second row, but when I drag to other rows, MAX value also changes. I want MAX(F2:F8) to be constant and other values to change. How can I do that?
Fix the max range like this F$2:F$8 ($ fixes the range).
Instead of dragging the formula down from cell H2, you could use an arrayformula in cell H2:
=arrayformula(if(F2:F<>"",G2:G+max(F:F)-F2:F,))
F2:F<>"" evaluates the calc whilst each cell in column F is not empty, otherwise the , at the end of the formula does nothing.

Arrayformula to compare two columns and skip blank cells

I have two columns on two different sheets with a long list of values; I'm trying to compare these two columns to check if an entry in the first one exists in the second one. In the first column there are also blank cells.
I tried different formulas but all of them is returning a value for blank cells, instead of a blank cell.
These are the formulas I tried:
=arrayformula(iferror(if(match(A1:A,AnotherSheet!A1:A,0),"yes"),"no"))
=query(arrayformula(iferror(if(match(A1:A,AnotherSheet!A1:A,0),"yes"),"no")), "Select * where Col1<>''")
=query(arrayformula(iferror(if(match(A1:A,AnotherSheet!A1:A,0),"yes"),"no")), "Select * where Col1 is not empty")
All of them is returning "no" for blank cells in the first column, I would like to have a blank cell instead. Thank you
=ARRAYFORMULA(IF(LEN(A1:A), IFERROR(IF(MATCH(A1:A, AnotherSheet!A1:A, 0), "yes"), "no"), ))

Working with hierarchical data in Google Sheets

If I have hierarchical data in a Google Sheet as in columns A and B in the example below, how can I write a formula that will fill the corresponding cells in column C with the product of the "parent" value in A1, and the "child" values in column B. That is, The formula in C8 for example, will search upward in column A until it finds the value 5 in A6, then multiplies it by 8, the value in B8.
Obviously I'm trying to avoid having to put the "parent" value in every row in column A.
After a fiddling around and jogging my memory on ArrayFormulas I figured out how to get the result I wanted. Here's the formula I'm using:
=ArrayFormula(index($B$2:$B2,MAX(IF(ISNUMBER($B$2:$B2),ROW($B$2:$B2)))-1,1))
(Note: Row 1 contains headings)
Edit: Explanation of how I arrived at this solution:
Ordinarily, IF(ISNUMBER(cell)), and ROW(cell) would return TRUE/FALSE, or a row number respectively. When used in an array formula and a range of cells as input instead, what you get is a list of TRUE/FALSE values, and row numbers evaluated for each cell in the input range. Wrap MAX around that and it will return the position in those two lists where IF(ISNUMBER()) is TRUE and the row number returned by ROW() is the highest, effectively searching from the bottom of the range.
The absolute reference for the first element in the range to be searched (in this case $B$2) keeps the range anchored to the same starting location when dragging the formula into other cells, while the lower bound of the range (in this case $B2) to be searched grows vertically.
Finally, the INDEX takes the input range, row number returned by the above (-1 row because of the header) and column position (1, since there is only one column) in order to return the desired value.
One way you could do this would be to associate each value in column B with a category or key that can be used to lookup the value for A in a separate table. This abstracts it somewhat, so you can change the values for the A column without having to have them in every row, but there's no empty cells.
i.e.
and lookup table:
In column C
= VLOOKUP($A1, <range for category lookup table>, 2, 0) * $B1
(And then this formula can be filled down)
More on VLOOKUP in Google Sheets here
Alternatively I suppose you could use a formula to find the last non-empty row in column A, or something along those lines, but this is more of a hack than a proper way to structure your data. Tables aren't really designed to be used in a hierarchical fashion like what you've shown. But they can easily represent hierarchical data using techniques like what I've suggested.

How to use index match formula that automatically expands in Google Sheets

In Google Sheets I am using a filter function to pull in Names into column A and a Timestamp into column B. Every time a second occurrence of the name shows up into columns A & B of the list I want column C next to the prior occurrence to reference the new timestamp. In column D I will then calculate the difference from the names timestamp and the next occurrence of that same name.
Currently I am using the following formula:
=IFERROR(INDEX(B3:B,MATCH(A2,A3:A,0)))
If I drag this formula down it does what I need it to do, but due to how many rows are being added to the first two columns, rows are being added to the bottom of the sheet due to the filter and the formulas keep needing to be dragged down. The durations in column D are being calculated with the following formula, that automatically arrays the results and automatically expands with the filter results:
=IFERROR(ARRAYFORMULA(IF(C2:C="","",C2:C-B2:B)))
I would like my index match formula to do the same, but it seems I cannot use the index formula with an arrayformula.
I attempted to achieve this by using a vlookup combined with an offset for the range. The first row is giving me the result I want, but all the subsequent rows are not referencing the offset range, probably because the offset isn't changing with each new array result here is that attempt:
=IFERROR(ARRAYFORMULA(VLOOKUP(A2:A,OFFSET(A2:B,1,0),2,FALSE)))
Any ideas how this could be accomplished by placing a formula in one cell, or would this have to be accomplished with a script?
I have added an example spreadsheet of the current method HERE
Thanks in advance for any help.
Formula
Instead of
INDEX, MATCH and OFFSET
try the following formula
=ArrayFormula(IFERROR(VLOOKUP(
TRANSPOSE(VALUE(REGEXEXTRACT(QUERY(TRANSPOSE(
IF(FILTER(ROW(A2:A),LEN(A2:A))<TRANSPOSE(FILTER(ROW(A2:A),LEN(A2:A))),
IF(FILTER(A2:A,LEN(A2:A))=TRANSPOSE(FILTER(A2:A,LEN(A2:A))),
TRANSPOSE(FILTER(ROW(A2:A),LEN(A2:A))),
),)
),,2000000),"(\d+)"))),
FILTER({ROW(A2:A),B2:B},LEN(A2:A)),2,0)))
Formula description
This part creates a square matrix showing the row number of the value that matches if it's below of the current row:
IF(FILTER(ROW(A2:A),LEN(A2:A))<TRANSPOSE(FILTER(ROW(A2:A),LEN(A2:A))),
IF(FILTER(A2:A,LEN(A2:A))=TRANSPOSE(FILTER(A2:A,LEN(A2:A))),
TRANSPOSE(FILTER(ROW(A2:A),LEN(A2:A))),
),)
This part takes the smallest row that matches the current row (the next occurrence of the row value)
TRANSPOSE(VALUE(REGEXEXTRACT(QUERY(TRANSPOSE( ),,2000000),"(\d+)")))
This part returns the related value, if any, otherwise a blank:
IFERROR(VLOOKUP( ,FILTER({ROW(A2:A),B2:B},LEN(A2:A)),2,0)))

Google Spreadsheet: How to display a value from the cell which is in the same column as the largest value in the other row

I've tried combination of MAX, HLOOKUP and CELL but it doesn't work. I want to display a name of the oldest person. In the following link you can find a screenshot of the example table.
image of the example table
=INDEX(1:1, 1, MATCH( MAX(2:2), 2:2, 0) )
You specify a whole row by its number: 2:2
You find the MAX in that row: MAX(2:2)
You MATCH the found maximum in that range, exactly: MATCH( MAX(2:2), 2:2, 0)
This returns the number representing the offset of the row/column.
You access the contents of the cell with INDEX, specify the range in which you wish to access cells, the row offset beginning with one, the column offset beginning with zero: INDEX(range, row, column)

Resources