Struggling with nested IF formulas in Google Sheets - google-sheets

Struggling to get this to work as I need. Basically a set of nested IF formulas with some conditions (see bottom of post for the rules)
I have 3 columns - J, K and L
Column J
contains the last login date (represented by 01/01/1970 if it's never been logged into)
Column K contains the last interaction date (represented by 01/01/1970 if no interaction or blank if within the last 90 days)
Column L contains the last activity date for a different metric. (blank if within the last 90 days)
I have another column, M - which I am intending to use as the calculated field.
This is what I am trying to achieve:
J
K
L
M
01/01/1970
01/01/1970
Recent Activity
26/06/2021
26/06/2021
01/01/1970
01/02/2021
Recent Activity
30/06/2021
01/01/1970
23/03/2020
30/06/2021
So the rules are as follows:
If Col J contains 01/01/1970 OR Col J date is before 25/03/2021 then check Col K.
If Col K contains 01/01/1970 then check Col L. If Col K IS BLANK then show "Recent Activity"
If Col L IS NOT BLANK then show Col L Date. If Col L IS BLANK then show "Recent Activity"
If Cols J, K and L all contain a date, get the most recent date from them
I have pulled out a few tufts of hair trying to get this to work, so any help would be greatly appreciated!

Looking at your rules, is 25/03/2021 a static date or should it be derived from a value somewhere? If static, then try this for the IF statements:
=arrayformula(if(J1:J<>"",if(isblank(J1:J)+isblank(K1:K)+isblank(L1:L)=0,if(K1:K>J1:J,if(L1:L>K1:K,L1:L,K1:K),if(L1:L>J1:J,L1:L,J1:J)),if(J1:J<datevalue("2021-03-25"),if(K1:K=datevalue("1970-01-01"),if(L1:L<>"",L1:L,"Recent Activity"),"Recent Activity"),J1:J)),))
There might be better functions, but this is mainly done with IF().
if(J1:J<>"" will run the subsequent formula as long as there are values in col J.
if(isblank(J1:J)+isblank(K1:K)+isblank(L1:L)=0 checks each row to see if there is a date in col J, K and L.
If there is, then this gets the largest date (it's a bit clumsy but someone else might be able to help):
if(K1:K>J1:J,if(L1:L>K1:K,L1:L,K1:K),if(L1:L>J1:J,L1:L,J1:J))
An alternative to finding the largest date on each row could be:
transpose(dmax(transpose(J:L),{sequence(1,max(if(J:L<>"",row(J:J))))},{J:L}))
or:
query(transpose(query(transpose(J1:L),"select "&regexreplace(join("","Max(Col"&sequence(max(if(J:L<>"",row(J:J))))&"),"),",$",)&" ",0)),"select Col2",0)
The other IFs test out the other scenarios.

based on your rules try in row 1:
=INDEX(IF(ISDATE_STRICT(I:I)*ISDATE_STRICT(J:J)*ISDATE_STRICT(K:K),
VLOOKUP(ROW(I:I), SPLIT(SORT(FLATTEN(ROW(I:I)&" "&I:J), 1, ), " "), 2, ),
IF(LEN(I:I&J:J&K:K), IF(I:I<"25/03/2021"*1, IF(J:J="01/01/1970"*1,
IF(((I:I="01/01/1970"*1)+(I:I<"25/03/2021"*1))*(J:J="01/01/1970"*1),
"Recent Activity", ), "Recent Activity"), I:I), )))

I think doing this as an arrayformula is unnecessarily complicated.
Here is a sample sheet made specifically for this question. In it, i placed this formula in cell M2 on a tab called MK.Idea and dragged it down:
=IF(J2,IF(J2>1*"25/3/21",J2,IFERROR(1/(1/MIN(N(K2),N(L2))),"Recent Activity")),)
Does it give the expected results? maybe you could add some more sample data on the sheet and test it out?

Related

Google Sheets- Countif Formula Count Cells specific text Multiple Criteria

I currently have a google sheet in which I count values in Column L against a reference sheet 'Ref' and then against a status Column W
IF(AND(COUNTIF(Ref!$A$1:$A$100,L2),W2 <>"Cancelled",W2 <>"Postponed",W2 <> "Dropped"),"Y", "N")
However the values have now changed in Column L. Instead of defined values in each cell they will be a string of values in each cell, the permutations will be too great to enter into my reference sheet.
How do I still count the values in column L to correctly return a 'Y' or 'N'
I have tried
=IF(AND(COUNTIF(L2,{"*LEE*","*LON*","*LAM*"}),W2 <>"Cancelled",W2 <>"Postponed",W2 <> "Dropped"),"Y", "N")
=IF(AND(COUNTIF(Ref!$A$1:$A$100,"*"&L2&"*"),W2 <>"Cancelled",W2 <>"Postponed",W2 <> "Dropped"),"Y", "N")
=IF(AND(COUNTIF(L2,{"*LEE*","*LON*","*LAM*"}),W2 <>"Cancelled",W2 <>"Postponed",W2 <> "Dropped"),"Y", "N")
The values I which need to count will feature one of the following phrases 'LEE', 'LON','LAM'.
The new values that are entered into column L will look like the following
<Area1: Y - LEE>,<Area2: Y - MIL>
<Area3: WF>,<Area4: Y - MUN>
<Area1: YY - MUN>,<Area2: YY - LON>,<Area3: YY-LAM>
So I need to be able to search within the cell for 1 of the 3 phrases.
Any help greatly appreciated
Thanks
I have a solution that might work, using arrayformula() in a single cell.
If I've understood correctly, it looks like you're checking Col L to see if cells contain LEE LON or LAM. You're also checking to see that they appear in Ref!$A$1:$A$100.
On your sheet that contains col F and Col W (Status), try this in row 1 (I've used AB1):
=arrayformula({"Check";if(if(L2:L<>"",regexreplace(regexreplace(regexreplace(flatten(query(transpose({if(regexmatch(split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"LAM|LEE|LON"),split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"|")}),"",9^9)),"\ ",""),"(\|)+","\|"),"^\||\|$",),)<>"",if(REGEXMATCH(join(" ",Ref!A2:A),if(L2:L<>"",regexreplace(regexreplace(regexreplace(flatten(query(transpose({if(regexmatch(split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"LAM|LEE|LON"),split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"|")}),"",9^9)),"\ ",""),"(\|)+","\|"),"^\||\|$",),)),if(regexmatch(W2:W,"Cancelled|Postponed|Dropped"),"N","Y"),),)})
To show the working, I've got three helper columns, AD, AE and AF:
AD1 gets LAM LEE or LON from col L:
=arrayformula({"LAM|LEE|LON entry";if(L2:L<>"",regexreplace(regexreplace(regexreplace(flatten(query(transpose({if(regexmatch(split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"LAM|LEE|LON"),split(regexreplace(L2:L,"[^A-Z]",char(9999)),char(9999)),"|")}),"",9^9)),"\ ",""),"(\|)+","\|"),"^\||\|$",),)})
AE1 checks the values in col AD to see if they appear in Ref!A2:A:
=arrayformula({"LAM|LEE|LON match";if(AD2:AD<>"",REGEXMATCH(join(" ",Ref!A2:A),AD2:AD),)})
AF1 checks if the value in col AE is true, then the status from col W:
=arrayformula({"Check";if(AE2:AE=true,if(regexmatch(W2:W,"Cancelled|Postponed|Dropped"),"N","Y"),)})
The 3 helper columns are combined in AB1.

Formula for tabulating daily running counts of a given column

I'm trying to write a formula that gives a running count of Issues for a given day. In other words: the output should enumerate each Issue for a given date (returning blank if Issue is blank), and then start again at 1 for the first issue in a subsequent date.
I've hard-coded the expected outputs in the "desired output" column (column I):
Sample dataset is in this sheet. Key pieces:
Column B contains the date
Column E contains the T-shirt size severity of each Issue
Column F contains a numerical translation of column E
Column G contains a binary output of whether there was an Issue
In my attempt (column J), I've gotten close using
=ArrayFormula(MMULT((ROW($B3:$B)>=TRANSPOSE(ROW($B3:$B))) * EXACT($B3:$B,TRANSPOSE($B3:$B))^1, ($G3:$G)^1))
...but it's not quite what I want, as:
this repeats values instead of giving blanks (e.g. row 8, 11)
this gives 0s instead of giving blanks (e.g. row 3, 4)
See Validation (column L).
Any ideas on how to get to what I'm looking for?
Just wrap your formula in the IF function
=ArrayFormula(IF(F3:F="",,YOUR.....FORMULA))
In other words
=ArrayFormula(IF(F3:F="",,
MMULT((ROW($B3:$B)>=TRANSPOSE(ROW($B3:$B))) * EXACT($B3:$B,TRANSPOSE($B3:$B))^1, ($G3:$G)^1)
))

Google Sheets - how to get the first and the last value?

Thank you for your time!
I am trying to make a trade journal in Google Spreadsheets.
What I want is the entry price and exit price of the trades,
Which are cell C10 and cell C11 in the screenshot image below.
I just manually typed the correct value - 960 and 2200.
Fortunately, the entry price for C10 is always what's in cell H2,
Because the first input will always be "Buy" in column F.
However I'm stuck finding the exit value.
I want it to find the last non-zero value of column H, only when column F contains "Sell".
What formula can I write?
enter image description here
You can do it with a combination of
INDEX
QUERY
COUNTA
Query for entries in column H where F is equal to "sell" and H is not 0.
From the retrieved subset of data, get the one with the last index (by counting the total amount of indices with COUNTA).
Sample formula:
=INDEX(
QUERY(F2:H, "select H where (F = 'sell' and H <> 0)"),
COUNTA(
QUERY(F2:H, "select H where (F = 'sell' and H <> 0)")
),
1
)
You can also do it without QUERY() (even though it is a really cool function).
=INDEX(H:H,MAX(IF((F:F="Sell")*H:H,ROW(F:F)),-1))
Find last row number where "Sell" appears in Column F and the corresponding value in Column H is non-zero. Since the row number monotonically increases, MAX() always gives us the last occurrence. If the row says "Buy" instead, or the value is 0, then IF() returns -1. We cannot use 0 because INDEX() evidently returns the whole range if we do that.
Use that row number as an index for column H to get the corresponding value. If no valid value was found, we get a #NUM error, which you can handle with IFERROR() if you'd like.
Note: we can't use a VLOOKUP() and approximate match because Action is unsorted.

Google Spreadsheets repeating X text Y amount of times in a column

I'm using Google Spreadsheets, and I have information populating the sheet through a Form. The data comes through as a timestamp, text, text separated by commas and number separated by commas (Columns A, B, C and D, respectively). Here's a link to a simplified example sheet I made to show my problem.
What I want to do is have the Week in B listed as many times as there are activities. So In B2, it says "Week 2", and in C2 there is "Running, Skiing", and so I would like "Week 2" to be written twice going down. In C3, there is only one activity, so I would only want "Week 2" to be written once this time. In the "I" column I showed what I would like to happen.
What I can do (Column G) is use a combination of Transpose, Split and Rept functions to list B2 (Week 2), F2 amount of times (twice). However, at an arbitrary distance below, I then want to repeat the same the same thing with B3 and F3, and so on and so on. I think I need to use ArrayFormula function, but I don't exactly understand how to in this scenario.
Hope I explained my problem properly, if any more information is needed I'll provide it, thanks.
Erase D column. Rewrite D column.
G2:
=REPT(B2&", ", F2)
Drag fill down.
This should give your table:
=ARRAYFORMULA(TRANSPOSE(SPLIT(TRANSPOSE(QUERY({G2:G5& ", ",C2:D5 & ","},, 500000)), ", ", 0)))

Formula for summation on amount of equal column values

Given a spreadsheet with two columns, say A and B, each containing n values under it, all text; is there a formula that allows me to fill just one cell containing the amount of equal values in columns A and B?
Example:
A B
-----
1 M M
2 L M
3 L L
4 M M
5 M L
-----
3
Since columns A and B both contain an M in rows 1 and 4, and an L in row 3, the result is (i.e. 2+1).
A simple solution is to use QUERY function in google spreadsheet:
=SUM(QUERY(A1:B5, "Select 1 where A = B"))
Or using SUMPRODUCT:
=ARRAYFORMULA(SUM(((A:A)=(B:B)) * (1) ))
One of possible solution will be to add the following formula in column C: =N(EXACT(A1,B1)),
copy it throughout the column down to the last row and then sum up the column C values using =SUM(C1:C5).
Here we go:
=IF(EQ(LEFT(A0, 1), "A"),
SUM(ARRAYFORMULA(N(EXACT(TRANSPOSE(A1:A5), TRANSPOSE(B1:B5))))),
"")
Reading: if the value in row 0 (it doesn't exist, but my example above does ;) ) is equal to the text "A", take the sum of an array N, otherwise put in an empty string. ("")
Array N is build by taking the transpose of columns A and B. (Turning them, so they look like a row) and comparing the values. (Burnash gave me the options "N" and "EXACT") The formula N transforms this into a 1 or 0.
Copy paste the formula in an entire row and what do you know... It worked! That was hellish for something so trivial.
Thanks anyway.

Resources