Get previous score of user x - google-sheets

Consider the following data set:
A:User | B:Date | C:Score | D:DiffLastResult
1: John 2021-01-01 7
2: Jane 2021-01-01 7
3: James 2021-01-01 8
4: John 2022-01-01 4
5: Jane 2022-01-01 9
6: James 2022-01-01 10
7: John 2022-06-01 10
8: Jane 2022-06-01 5
9: James 2022-06-01 7
Now, I want in column D to have the abs difference between the current score and the previous score (for the given user). So, for instance, user James' last score is 7. Previous score of James was 10, so the delta is minus 3, which should be displayed in cell D9. In cell D6, I want to have a value of 2 (10-8, previous score of James, in context of the score of 2022-01-01).
This list is simplified, for the purpose of asking this question. In my real file, the list of names is unorderded, non-repetitive (not all users have the same amount of scores)
I am using Google Sheets. I have tried using vlookup, lookup, and index/match combinations, but I keep getting the first score of James (instead of the previous one). The list is sorted on date ASC.
Can somebody point me in the right direction? Many thanks.

try:
=ARRAYFORMULA(IFNA(VLOOKUP(
A1:A&COUNTIFS(A1:A, A1:A, ROW(A1:A), "<="&ROW(A1:A))-1, {
A1:A&COUNTIFS(A1:A, A1:A, ROW(A1:A), "<="&ROW(A1:A)), C1:C}, 2, )))

Related

How to count occurrence in previous rows based on two columns value

I'm trying to count the number of occurrence in previous rows based on two conditional values using Google Sheet.
Let say this is my table :
Row A
Row B
Row C
Row D
1
John
Smith
2
Marty
Butler
3
John
Herbert
4
John
Smith
5
Philip
Rand
6
John
Smith
7
Marty
Butler
Is there a formula that exist that can count those occurrences. The idea is that when I log a new name, if Row B and C already exist it increase the value in Row D by 1 so I would know that it is the nth entry under that name. In my example, Row D would looks like this:
Row A
Row B
Row C
Row D
1
John
Smith
1
2
Marty
Butler
1
3
John
Herbert
1
4
John
Smith
2
5
Philip
Rand
1
6
John
Smith
3
7
Marty
Butler
2
Delete everything in Column D (including the header) and place the following in D1:
=ArrayFormula({"Header";IF(B2:B="",,COUNTIFS(B2:B&C2:C,B2:B&C2:C,ROW(A2:A),"<="&ROW(A2:A)))})
The "Header" text can be edited as you like.
The COUNTIFS reads, in plain English, "Count how many times this first and last name combination has occurred within only the rows up to the current row."

Loosing my primaryKey during Query({Importhtml1; Importhtml2;...}) in Google Sheets

I need to query up to 50 Sources with dynamic length. Thats why it has to be an all in one command.
The Length of each Source is changing very often. Each Source has RowA as Primarykey. But after the query over all sources, the key isnt unique anymore.
Let´s say my Sources look like this:
SourceA SourceB SourceC
James 5 Jack 3 Jane 5
Steve 3 James 1 Steve 1
Then this Code:
=Query({Importhtml("ScrA";"table";1); Importhtml("ScrB";"table";1); ...})
returns this:
Result:
James 5
Steve 3
Jack 3
James 1
Jane 4
Steve 1
As u can see, i don´t know from which Source "Steve" is coming - but i need to know...
Is there any way to get a result like this (Remember that all Sources have changing Lengths)?
ScrA James 5
ScrA Steve 3
ScrB Jack 3
ScrB James 1
ScrC Jane 4
ScrC Steve 1
you will need to do it like this:
=ARRAYFORMULA(QUERY({
{REGEXREPLACE(INDEX(IMPORTHTML("ScrA"; "table"; 1);;1)&""; ".+"; )&"ScrA"\
IMPORTHTML("ScrA"; "table"; 1)};
{REGEXREPLACE(INDEX(IMPORTHTML("ScrB"; "table"; 1);;1)&""; ".+"; )&"ScrB"\
IMPORTHTML("ScrB"; "table"; 1)}};
"where Col2 is not null"; 0))

Make an equivalent of a "For" loop

I'm trying to figure out a way to capture information that's essentially a "For" loop in the programming world. I have one sheet that contains unique names and unique quotes:
SHEET 1
Column A Column B
======== ========
John Hello
Mike Hi
Paul Hey
Joe
And then I have another sheet with multiple names, multiple quotes and a number of times quoted (where 0 times does not exist) and the sheet will continue to add new names and quotes every hour:
SHEET 2
Column A Column B Column C
======== ======== ========
John Hi 2
John Hey 1
Mike Hello 8
Sean Hey 4
Paul Hi 1
Jane Hello 2
John Hello 1
... ... ...
OUTPUT EDIT:
Add the number of times that John, Mike and Paul quoted Hello found in Sheet 2, Column C
Add the number of times that John, Mike and Paul quoted Hi found in Sheet 2, Column C
Add the number of times that John, Mike and Paul quoted Hey found in Sheet 2, Column C
SHEET 3
Column A Column B
======== ========
Hello 9
Hi 3
Hey 1
Create a pivot table from Sheet 2 with ColumnB for Rows, ColumnC for Values and ColumnA for Filter. Adjust filter to select only John, Mike and Paul then lookup Hello, Hey and Hi in Sheet1 from the pivot table with say:
=vlookup(B2,'Pivot Table 1'!A$1:B$3,2,0)
in Sheet1 B2 copied down to suit.

Google Spreadsheet SUMIFs equivalent

I need a SUMIFs equivelent in google spreadsheet. It only has SUMIF, no IFS.
here is my data:
# Salesman Term (Month) Amount
1 Bob 1 1,717.09
2 John 1 634.67
3 Bob 1 50.00
4 Bob 1 1,336.66
5 Bob 1 0.00
6 Bob 1 55.00
7 Bob 300 23,803.97
8 Bob 300 24,483.91
9 Bob 300 20,010.03
10 Bob 300 41,191.62
11 Bob 300 40,493.14
12 Bob 300 10,014.01
13 John 1 100.00
13 John 100 100.00
I want to add everything that BOB sold that the term is equal to or less then 100. I also want to SUM everything that bob sold that the term is greater then 100. Same for John.
You need to use the FILTER function combined with the SUMfunction. In your example with Bob, your function would be like (assuming your data columns is from A to C):
=SUM(FILTER(C:C;A:A="Bob";B:B<=100))
The new Google Spreadsheet, now has the SUMIFS function available.
You can use the following formula:
=SUMIFS(F1:F14,D1:D14, "=Bob",F1:F14, ">=100",E1:E14, "<100")
If you want to perform the calculation for John as well, I can recommend the following:
=QUERY(D1:F14, "SELECT D, SUM(F) WHERE E<100 AND F>=100 GROUP BY D LABEL D 'Who', SUM(F) 'Total'")
See example file I created: SUMIFS on DATA

How to get the data for the next two weeks?

I've several straight-forward planners, like this:
| date | person | person 2 | description |
2013-03-01 peter pam painting
2013-03-18 john carl cleaning
2013-03-20 max anne washing
On a different sheet, I want to filter the 'events' for the next 2 weeks only. How can I achieve this? I tried several ways, but none of them are working.
With =ARRAYFORMULA(DAYS360(B2:B;NOW())) I can get a daynumber in a different column. 0 = today, 1 = yesterday, -1 tomorrow et cetera. In fact, I need to filter the days -1 to -14. Sometimes there are only 2 events, sometimes 5 in 2 weeks.
Edit: Some things I found
First, I filter the correct daynumbers with =filter(A2:A50;A2:A50>-14;A2:A50<0)
Then, I do a vlookup =vlookup(G32,A1:E49;5;false)
(where G32 is the filtercommand, A2:A50 the daynumbers, A1:E49 all the data)
This is a good opportunity for the QUERY command and Query Language.
I'm assuming your data is in 4 columns, A:D, in "sheet 1".
=query('sheet 1'!A:D,"select * where datediff( A, now()) < 14 and datediff( A, now()) > 0")

Resources