Lookup Array Formula to calculate difference - google-sheets

Hoping to build an ArrayFormula that's clearly beyond what I understand, so please bear with me. I'm using the following formula to grab the value of the Last Non-Empty Cell and subtract the value of the cell immediately above it.
=ArrayFormula((LOOKUP(2,1/(NOT(ISBLANK(Sheet3!A:A))),Sheet3!A:A))-INDEX(Sheet3!A:A, CountA(A:A)-2,1))
I'd like to use a HLOOKUP function to match names from a vertical list, to identify the last non-empty cell in the corresponding column. I'm able to get the correct value from the 'Names' column with the formula below, but not sure how to integrate this into the ArrayFormula.
=HLOOKUP(A4,Sheet3!A1:E30,1,FALSE)
A correct formula should retrieve the value in the last non-blank cell of a column containing the name in 'Data Test'!A:A
Please see sample sheet for reference: Data Test

The way I understand the data it is reasonable to assume that the range in each column is consecutive.
We will also have to calculate the subsidy change for everyone separately because some of these formulae do not work with ArrayFormulae.
This formula finds the last row of the respective column and the second to last row and subtracts the two, if there is an error (because we try to subtract a string for Eric) we use the last and only value.
=IFERROR(
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 1,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1) -
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 2,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1),
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 1,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1))

Related

Formula Help: IF, Countif, Split. Override

I am currently running the following formula
=IF(P202="","",COUNTIF(SPLIT(P202," "),"*XXX*")+COUNTIF(SPLIT(P202," "),"*YYY*"))
This is to count the values XXX & YYY in column P. This returns values 0 - 4 primarily in column AC
However I have another column. Which is column W, in this column I have values such as 'cancelled' and 'postponed'.
What I would like to happen, as an example the above formula returns a value of 2 but if Column W was updated with cancelled. Then that value would alter the return to 0 as the event is now cancelled.
Thanks
try:
=INDEX(IF(P202="",,SUM(IFERROR(REGEXMATCH(""&
IF(W202="canceled", 0, SPLIT(P202, " ")), "(?i)xxx|yyy"), 0)*1
for array try:
=INDEX(IF(P202:P="",,MMULT(IFERROR(REGEXMATCH(""&
IF(W202:W="canceled", 0, SPLIT(P202:P, " ")), "(?i)xxx|yyy"), 0)*1,
SEQUENCE(COLUMNS(SPLIT(P202:P, " ")), 1, 0

Combine two formula in one cell and get one result

Hi I have 2 formulas in google sheet that works great alone, but how can I put it together and get 1 result in the same cell, not side by side when we use "&"
Formula 1: =COUNTIFS(B1,"<>")*1-COUNTIFS(E1,"<>")*0
(when I have some text in cell B1 and E1 just count cell B1)
Formula 2: =(COUNTIFS(B1,"")*0+COUNTIFS(E1,"<>")*1)
(when I have some text in cell E1 and B1 is blank just count cell E1)
Already tried: =COUNTIFS(B1,"<>")*1-COUNTIFS(E1,"<>")*0&(COUNTIFS(B1,"")*0+COUNTIFS(E1,"<>")*1) unwanted result: 1 1 in the same cell
try:
=IF((B1<>"")*(E1<>""), 1,
IF((E1<>"")*(B1 =""), 1, 0))

Google Sheets: Adjusting an array formula to auto-appear in every row

The formula in question is =IF (ISBLANK(H2),"", ARRAY_CONSTRAIN(ARRAYFORMULA(IF( (MOD(SUM(INT(MID(REPT("0",20-LEN(H2))&H2,ROW($1:$31),1)*(MOD(ROW($1:$31),2)+1)/10)+MOD(MID(REPT("0",20-LEN(H2))&H2,ROW($1:$31),1)*(MOD(ROW($1:$31),2)+1),10)),10)=0), "✔", "❌")), 1, 1))
In English it checks if H2 contains a valid credit card (passing Luhn's algorithm, discussion / sample data here). The expected output is valid = ✔; invalid = ❌; if blank then nothing.
I'm trying to adjust this to appear in every row, but can't seem to nail it down. (Using the trick like for a formula =LEFT(H2,4)&" "&MID(H2,5,6), if it's =arrayformula(LEFT(H2:H100,4)&" "&MID(H2:H100,5,6)) it appears in every row without having to manually refill it when a new row is inserted).
Sample google sheet.
Try this:
=ARRAYFORMULA(
IF(
H2:H = "",
"",
IF(
MOD(
MMULT(
MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 2, 2), 1)
+ MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 1, 2), 1) * 2
- (MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 1, 2), 1) * 2 > 9) * 9,
SEQUENCE(10, 1, 1, 0)
),
10
) = 0,
"✔",
"❌"
)
)
)
If you want a more general solution (for card numbers longer than 20 digits), replace 20 with MAX(LEN(H2:H)) + MOD(MAX(LEN(H2:H)), 2), and 10 with (MAX(LEN(H2:H)) + MOD(MAX(LEN(H2:H)), 2)) / 2.

Google Spreadsheet, operations with above row cell in same column with arrayformula

I have arrayformula in the first row of a column so my values and calculations can start in Row 2 and for all the column length.
I have this situation:
https://docs.google.com/spreadsheets/d/11oDra7Vja4-5C0Uix7JTgLLSMG3gPj-6fkajXlWqqQk/edit?usp=sharing
I need a simply arithmetic operation:
Subtract above value of the same column for every row.
I'm using:
=arrayformula(IF(row(A:A)=1; "What I have now"; IF(ISBLANK(A:A); ""; A1:A-A2:A)))
but as you see is wrong.
How to do that?
UPDATED QUESTION:
And then in the second sheet I need a SUM operation with some blank cells in column:
How to do that?
https://docs.google.com/spreadsheets/d/11oDra7Vja4-5C0Uix7JTgLLSMG3gPj-6fkajXlWqqQk/edit#gid=931743679
If you want to have the array formula ion the header this is a bit weird as you need to allow the formula to technically access row 0, we can do this by constructing ranges.
=ArrayFormula(IF(
--(ROW(A1:A) > 2) + -ISBLANK(A1:A) = 1;
{0; A1:A} - {0; A2:A; 0};
""))
--(ROW(A1:A) > 2) + -ISBLANK(A1:A) = 1 Checks if the row is populated and not one of the first two rows in a way that works nicely with array formulas
{0; A1:A} - {0; A2:A; 0} does the following:
0 Data 156 123 110 95 42
- - - - - - -
0 156 123 110 95 42 0
= = = = = = =
0 33 13 15 53 42 42
N N Y Y Y Y N <- Is shown
^ ^ ^
| | Because Row is blank
| |
Because row not > 2, thus it is never evalauated even though the second would cause an error
I think this is quite tricky. The problem is that in an array formula the number of cells in each array must match - you can't mix an array starting in row 1 with an array starting in row 2 if they go all the way to the bottom of the spreadsheet.
Here is one way of getting the result you want
=arrayformula({"What I need";"";offset($A$1,1,0,count(A:A)-1)-offset($A$1,2,0,count(A:A)-1)})
You will need to change the ; and , for your locale.
I have built up an array using the {} notation to define the elements. In my locale a ; means go to the next row, so I have defined the first two cells directly as strings. After that I've chosen to use Offset to get the range A2:A5 (1 row down from A1, 0 rows across and 4 cells high) and subtract the range A3:A6 (2 rows down from A1, 0 rows across and 4 cells high) it so that gives me the other 4 cells.
B1 "What I need"
B2 ""
B3 A3-A2=33
B4 A4-A3=13
B5 A5-A4=15
B6 A6-A5=53
but will need an IF statement adding if there are any blank cells between the numbers.
In the particular case of your updated question where there are fewer numbers in column D than column C, the formula would be
=arrayformula({"Special Case";"";offset($D$1,1,0,count(D:D))+offset($C$1,2,0,count(D:D))})
But in the general case of there being blank cells anywhere, you would have to test everything
=arrayformula({"General Case";"";if(offset($D$1,1,0,rows(C:C)-2)="","",if(offset($C$1,2,0,Rows(C:C)-2)="","",offset($D$1,1,0,rows(C:C)-2)+offset($C$1,2,0,Rows(C:C)-2)))})

Using sumif by row in an arrayformula

I've got a sumif at the start of every row of my data adding up numbers if they are >0 and another doing the same for numbers <0 like this:
=SUMIF(P6:X6;">0")
This works and all but it's quite a pain to drag the cel down every time I add more data. Is there a way for me to turn this into a ARRAYFORMULA that just keeps going down.
The formula for sums ">0" is:
=arrayformula(mmult(A2:C*--(A2:C>0), transpose(A2:C2 * 0 + 1)))
and for sums "<0":
=arrayformula(mmult(A2:C*--(A2:C<0), transpose(A2:C2 * 0 + 1)))
transpose(A2:C2 * 0 + 1)) is an array of 1: [1, 1, 1, ...] It's the part of mmult function to convert the result into row.
--(A2:C>0) double minus is for converting booleans into 1 (if true) and 0 (if false)

Resources