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

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.

Related

Find Last Non-zero (+Non-blank) Row Meeting Criteria in Google Sheets

I am trying to get the last non-zero, non-blank value of a row within a column (Column F in my example image) wherein that row ALSO matches a Campaign name (Column D).
Most search results yield an Excel-specific variation of =LOOKUP(1,1/(L:L>0),L:L), but this doesn't work in Google Sheets.
I am trying to solve for Cell F23 = 2374.
I found and modified a formula which returns the last non-zero, non-blank value within a column reliably, but I don't know where to mix the additional filter (basically, D$2:D22 = D23) into the INDEX function.
Here is what I'm working with:
=if(
{{separate_formula_that_fetches_value_from_other_sheet}})=0,
INDEX((FILTER(D$2:F22,NOT(ISBLANK(D$2:F22)))), (ROWS(FILTER(D$2:F22,NOT(ISBLANK(D$2:F22))))),3)
)
Here is the example table:
Thank you for any help!
If you are trying to find inside RANGE B2:F22 which...
value in Column F is not empty and greater than 0, and
value in Column D matches D23,
try this, didn't test it, but it should work I think:
=LAMBDA(FILTER,
INDEX(FILTER,COUNTA(FILTER))
)(FILTER($F$2:$F$22,$F$2:$F$22>0,$D$2:$D$22=$D23))

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.

Google sheet SUMIFs error "Array arguments to SUMIFS are of different size." even though they are fixed size

This is my formula:
=SUMIFS(Transactions!$B2:$B1000, "=2020_10", Transactions!$H2:$H1000, "=Salary", Transactions!$E2:$E1000)
So I'm trying to say "if the row has a date (string) of 2020_10 in col B, and a value of "Salary" in col H, add up col E (which contains a monetary amount)"
As you can see I've tried specifying the exact number of rows to try and ensure the "array arguments" are of the same size but it still throw the same error.
Transactions sheet:
Formula for col B (date as a string): =IF(ISBLANK(A1000), "None", CONCATENATE(YEAR(A1000), "_", MONTH(A1000)))
Formula for col H (Calculated Purchase Type): =IF(ISBLANK(G1000), "None", G1000)
Col E: manually entered number
Screenshot of transactions sheet:
So I'd like row 9 counted in the SUMIFS, but not the other rows (as their Calculated Purchase Type (col H) isn't "Salary").
You seem to have the arguments in the wrong order.
The syntax of the function is:
SUMIFS(sum_range, criteria_range1, criterion1, [criteria_range2, criterion2, ...])
So it's trying to sum the contents of column B, and using "=2020_10" and "=Salary" as criteria ranges.
Try
=SUMIFS(Transactions!$E2:$E1000, Transactions!$B2:$B1000, "=2020_10", Transactions!$H2:$H1000, "=Salary")
I had the arguments in the wrong order. The range you want to add up goes first:
Wrong:
=SUMIFS(Transactions!$B2:$B1000, "=2020_10", Transactions!$H2:$H1000, "=Salary", Transactions!$E2:$E1000)
Correct:
=SUMIFS(Transactions!$E2:$E1000, Transactions!$B2:$B1000, "=2020_10", Transactions!$H2:$H1000, "=Salary")
This is because I adapted an existing SUMIF (singular), which takes the range you want to add up first. Why did they swap them lol.

Multiple queries by date in the same row in google sheets

I need to sum the daily sales of each product ASIN. Sounds easy but I can`t do it.
On the left, you can see the data. At the right, the empty table that I need to create.
use:
=QUERY(K2:M; "select K,sum(M) where M is not null group by K pivot L")
Use an IF statement on the columns that you want summed.
For your example, it would be something like this:
in Cell P2
=IF($L2 = P$1, $M2, 0)
This translates to
If ((the text in column L row 2 = the text in p1), take the units from column m row 2, else take 0)
The $ are there to indicate you always want that column/row. So we always want to evaluate column L at the start and Row 1 in the comparison.
Then at the last row of the sheet you have a SUM function for the column
=SUM(P2:P100)
or whatever the end is
Working example here: https://docs.google.com/spreadsheets/d/1zhQMV6o1tF2P_kWbXDPdnVKzYnd-Dsvg2h7YiHWSaNI/edit?usp=sharing

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)
))

Resources