Google Sheet sum and multiply numbers based on dates - google-sheets

I am trying to sum the packages(B) if dates are equal, then compare this sum with the Orders per Day column(C) and multiply by the Rate Per Package(D) depending where the sum falls within the Orders per Day number.
I would like to return just the total number if possible.
DEMO:
https://docs.google.com/spreadsheets/d/1oSFnjogyXYybsqIYgQW2m45bbyAVBTKr3EVgxcGWFQo/edit?usp=sharing

You can try the following 2 formulas
In cell F2 place
=QUERY({A2:D},"select Col1, sum(Col2)
where Col1 is not null
group by Col1 label sum(Col2)''",0)
Then in cell H2 use
=ArrayFormula(IFS(G2:G>=80,G2:G*D9,
G2:G>=59,G2:G*D8,
G2:G>=39,G2:G*D7,
G2:G>=19,G2:G*D6,
G2:G>=14,G2:G*D5,
G2:G>=9,G2:G*D4,
G2:G>=4,G2:G*D3,
G2:G=1,G2:G*D2,
G2:G="",""))
(You can adjust ranges to your needs)
Functions used:
QUERY
ArrayFormula
IFS

Related

Google Sheets - Count Unique Values of Importrange Data between Given Dates

I have a sheet with a Name Column and corresponding Date column. I'm attempting to count the number of times unique Names appear for a specific Date range. The data is in a separate spreadsheet, so I'm using importrange in the formulas.
I created a sample spreadsheet here - we can pretend that the "Data" tab is on a separate spreadsheet file. I have the below formula so far but I'm not sure how to tell it to only count the unique Name values between the Dates in cells B2 and C2.
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8/edit#gid=0","Data!A:B"),"Select Count(Col1) where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2 <= date '"&TEXT(C2,"yyyy-mm-dd")&"' Label Count(Col1) ''")
Thanks for your help!
This formula should do it:
=COUNTUNIQUE(QUERY(IMPORTRANGE("1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8","Data!A2:B"),"select Col1 where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2<= date '"&TEXT(C2,"yyyy-mm-dd")&"'",0))

Google sheets Query function with Arrayformula

For each of the email id, I want to get latest 10 records by timestamp. How do I get the results with arrayformula? Query function is not important as long as I can still achieve this with arrayformula. Here is the sample data:
https://docs.google.com/spreadsheets/d/1YAHA02VM-5MXzVKhkxu_eODPKObpoz441mGX8lOFu5M/edit?usp=sharing
Try this on another sheet, row 1:
=arrayformula(query({query({Sheet1!$A:$C},"order by Col1 desc,Col2",1),{"Dupe position";countifs(query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0),row(Sheet1!$A2:$C),"<="&row(Sheet1!$A2:$C))}},"select Col1,Col2,Col3 where Col1 is not null and Col4 <= 10 order by Col1",1))
You can adjust the number of records found by adjusting Col4 <= 10, and also the final sort by altering order by Col1 at the end of the formula.
Explanation
This gets the data from Sheet1, sorts it by date desc then email asc:
query({Sheet1!$A:$C},"order by Col1 desc,Col2",1)
Then to the side of this data, a COUNTIFS() is used to get the number each time an email appears in the list above (since it's sorted desc, 1 represents the most recent instance).
countifs(<EmailColumnData>,<EmailColumnData>,row(<EmailColumn>),"<="&row(<EmailColumn>))
In place of <EmailColumnData> in the COUNTIF() is:
query({Sheet1!$A2:$C},"select Col2 order by Col1 desc,Col2",0)
In place of <EmailColumn> above, we only want the row number so we don't need the actual data. We can use:
Sheet1!$A2:$C
Various {} work as arrays to bring the data together.
Eg., {a,b,c;d,e,f} would result in three columns, with a, b, c in row 1 and d, e, f in row 2. , is a new column, ; is a return for a new row.
A final query around everything gets the 3 columns we need, where the count number in col 4 is <=10, then sorts the output by Col1 (date asc).
On second thoughts, maybe this is bit cheeky, but this might do it ( taken from conditional rank idea )
=ArrayFormula(filter(A2:C,countifs(A2:A,">="&A2:A,B2:B,B2:B)<=10,A2:A<>""))
EDIT
The above assumes (because the data is time-stamped) dups shouldn't occur. If they do and the data is pre-sorted, you can use row number as a proxy for time stamp as suggested by #Aresvik.
Alternatively, you could count separately
(a) only rows with a later timestamp
plus
(b) rows with the same time stamp but with earlier (or identical) row number
=ArrayFormula(filter(A2:C,countifs(A2:A,">"&A2:A,B2:B,B2:B)+countifs(A2:A,"="&A2:A,B2:B,B2:B,row(A2:A),"<="&row(A2:A))<=10,A2:A<>""))
I have added a new sheet ("Erik Help") with the following formula in A1:
=ArrayFormula({"Submitted Time","Email","Score";SORT(SPLIT(FLATTEN(QUERY(SORT(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(IF(Sheet1!B2:B=TRANSPOSE(UNIQUE(FILTER(Sheet1!B2:B,Sheet1!B2:B<>""))),Sheet1!A2:A&"|"&Sheet1!B2:B&"|"&Sheet1!C2:C,),,COUNTA(Sheet1!A2:A)))," ",0,1)),SEQUENCE(MAX(COUNTIF(Sheet1!B2:B,Sheet1!B2:B))),0),"LIMIT 10")),"|",1,0),1,0)})
The number of records is set after LIMIT.
The order is set by the final two numbers: 1,0 (meaning "sort by column 1 in reverse order," which, as currently set, is sorting in reverse order by date/time).

How to get the total number of counts for last not null date in google sheets based on another column?

This is a follow up question to:
How to get the total number of counts for last not null date in google sheets?
Question
How to get the most recent number of companies that I have applied?
For example, in the google sheet below, I have applied to 5 companies in last date of march4, so the answer is 5.
NOTE: there are 8 values for march4 but only 5 companies are not-empty.
Public sheet
https://docs.google.com/spreadsheets/d/10NzbtJhQj4hQBnZXcmwise3bLBIAWrE0qwSus_bz7a0/edit#gid=517697699
Try this:
=query({B2:C},"select Col1 where Col1 is not null order by Col2 desc limit "&countifs(C:C,max(C:C),B:B,"<>")&" ",0)
To get the number of companies on the most recent applied date:
=countifs(C:C,max(C:C),B:B,"<>")
It counts how many times the max() date occurs in Col C, where Col B has a value.
"<>" is used to denote 'not empty'.

Excel - sum given a condition in a relative column

Imagine I have this (time)sheet:
Hours | Text
------+----------------------
3 | fixing PRA-345
4.5 | refactoring PRA-222
5 | PRA-345 and stuff
And I want to calculate how much cumulative time one has spent on a ticket with a given number.
In other words sum the hours based on the text in a neighbouring cell.
Can you do it without extra column? what I did was to make an extra column that returned either the number, if given text was present (via REGEXMATCH) or 0. And then I ran a SUM on that column. Having this solved without extra column would be nice ;)
Expected output
In my case if would be enough for a given string to find the total sum of hours. So if I cell(say it's D1) has the hardwired text, such as "PRA-345" I want the cell to the left(E1) to display the total hours(8 in this case)
Is this what you need?
=sum(filter(B5:B,regexmatch(C5:C,E5)))
Reference:
FILTER
SUM
Instead you can try
=QUERY({A1:A11,ArrayFormula(REGEXEXTRACT(B1:B11,"PRA-\d+"))},
"select Col2, sum(Col1) where Col1 is not null
group by Col2 label Col2 'Tickets', sum(Col1) 'Sums' ",1)
Functions used:
QUERY
ArrayFormula
REGEXEXTRACT

Google Sheet formula to find the minimal sum of pairs in array

I'm looking for solution for my problem. I have a sheet to summarize lap times for some competition. We make 3 laps in each qualification. We are qualifying to finals by 2 best laps one after another. So we sum first and second lap or second and third lap and then choose the smallest one sum. I've managed to get array of pairs and filter out empty cells (run not finished). Number of pairs may vary form 1 to 20.
Now is my question. How to find the smallest sum of pairs from my array in one elegant formula?
Here is my sample sheet: example sheet
=QUERY(QUERY({A17:B17;B17:C17;D17:E17;E17:F17;G17:H17;H17:I17};
"select Col1+Col2
where Col1 is not NULL
and Col2 is not NULL");
"select min(Col1)
label min(Col1)''")
I know this isn't exactly your question and fair play if it gets marked down, but in your quest for an 'elegant formula', I was wondering if there was a more general way to get the pairs in the first place.
You can do it with by using two ranges offset by one cell together with the mod of the column number:
=ArrayFormula(query(
query({transpose({A17:H17;B17:I17;mod(column(A17:H17),3)})},"select Col1+Col2 where Col1 is not null and Col2 is not null and Col3>0")
,"select min(Col1) label min(Col1) ''"))

Resources