Google Sheets Select X where (max(y)<=z) - google-sheets

my title is potentially not that enticing. But I am trying to create a semi-dynamic formula in order to find a "stock on hand" up to a particular date in time. There is a set number of locations ids 1-10, and two product types 3 & 4.
It is not guaranteed that each location will have a stock count at the date in question. I want to use query to find THE MOST RECENT stock count where location and product type and <= date
here is the basic formula
=QUERY(Sheet1!A160:E3530,"SELECT D WHERE ((B = "&$H$1&")) AND (E <= date '"&TEXT(MAX($M$3),"yyyy-mm-dd")&"') AND ((A = "&G2&"))", true)
But I need to figure out how to use MAX to find the most recent date within the date range specified.
Any help appreciated!
EDIT 23/06/2021
You will note this is a fraction of the data I have in my set (in the example sheet), so most numbers show as zero, but the formula
=MAXIFS($C$3:$C$6040,$A$3:$A$6040,I3,$B$3:$B$6040,$J$2,$E$3:$E$6040,(MAX(QUERY($A$3:$E$6040,"SELECT E WHERE (E <= date '"&TEXT($R$2,"yyyy-mm-dd")&"') AND ((A = "&I3&")) AND ((B = "&$J$2&"))", true))))
works on my full data. So this finds the most recent record of equipment type 3 or 4, up to the specified date and from a specified yard. Further filtering is done based on a change type of "converted, removed,dead,added,etc". What I want to do now is do a monthly or fortnightly line chart over time, eg the 14th and 29th of each month, or the 20th of each month and plot the the sum of each column J:Q. To start I hoped to use the date in U:U and populate the V:AC accordingly.
I have played with the onEvent script but I am struggling to make progess here

Related

Query data based on time interval/frequency

I'm trying to have a calendar display a series of activities based on their type, time and frequency for an easier visualization of data.
So far, I have managed to create a formula that correctly fetches the data that I have on a repository and displays it on the calendar. However, I'm not sure how I can have it account for entries that have a frequency (happening every x days).
For an easier understanding here are screenshots of both the table and the schedule
And here's the current formula I'm using to display the event/activity title in each day/hour at C12 for example:
=IFERROR(
INDEX(Repository!$K:$K,
MATCH(
C$10,
IF(
(Repository!$G:$G=$G$8)*
(Repository!$H:$H=$K$8)*
(Repository!$N:$N>=$B12)*
(Repository!$N:$N<$B12+TIME(2,0,0)),
Repository!$D:$D),
0)
),
"")
What I'm currently missing on the formula is a way to correctly account for the start/end date as well as frequency and understand if each day falls under the specified criteria. In case the frequency is 0 then I'd like to have it discard the end date at all (in case for some reason I end up forgetting to set the end date).
I have tried to work with the formula provided to account for the frequency but nothing that I tried seemed to work.
Minimal example requested by #GabrielCarballo
Entry on the table with a 2 days frequency:
Expected result on the schedule:
So basically, the formula on each cell should check for the start date, end date and frequency of the activity and identify if the specific date on the schedule falls under the specified timeframe.
In this minimal example, the activity starts on the 7th December and repeats every 2 days until the 14th of December.
use in C6:
=INDEX(IFNA(VLOOKUP(TEXT(C4:P4+B6:B14, "e-m-d-h-m")&G$2&K$2, SPLIT(FLATTEN(MAP(
Repository!D$4:D, Repository!O$4:O, Repository!P$4:P, Repository!N$4:N,
Repository!G$4:G, Repository!H$4:H, Repository!K$4:K, LAMBDA(d, o, p, n, g, h, k,
IF(DAYS(o, d)>=SEQUENCE(1, MAX(DAYS(o, d)), 0, p), TEXT(d+SEQUENCE(1,
MAX(DAYS(o, d)), 0, p)+IF(ISODD(HOUR(n)), (HOUR(n)*"1:00")-"1:00", HOUR(n)*"1:00"),
"e-m-d-h-m")&g&h&"×"&k, )))), "×"), 2, )))

Google Sheets filter by todays date

I have created a google sheet of sporting events, this then filters to different websites using the ninjatables wordpress plugin.
Not all websites need to display all the games.
The 'Games' sheet contains a list of all the events throughout the year, I then have a query to filter the results into individual sheets for individual sites, displaying only certain sporting events for that site, such as;
Site 1 = Rugby and Football
Site 2 = Football and Basketball
Everything so far works fine.
I am now trying to add a date filter so that games before todays date are no longer visible.
=query(Games!A:Y, "where A > date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and (F = 'Football') or (F = 'Rugby')",1)
If I change the greater than (>) sign to less than(<), it filters out the results to only show the games before todays date just fine, but for some reason my current query does not filter out games before today.
I have tried changing the formating on my date column on the games sheet (mm/dd/yy, dd/mm/yyyy, yyy-mm-dd, etc) not sure that that has anything to do with it, but couldnt hurt to try.
If anyone could shed any light as to where I may be going wrong it would be greatly appreciated.
Solution:
You can use greater than or equal >= as an operator.
EDIT: Boolean operations are evaluated left to right, so A > date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and (F = 'Football') are evaluated first, then this is or'd with (F = 'Rugby').
This is fixed by grouping the column F conditions with parenthesis.
=query(Games!A:Y, "where A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and ((F = 'Football') or (F = 'Rugby'))",1)
This will display entries greater than or equal to today's date, and if column F is (Football or Rugby)
Sample Query:

In Google Sheets, how to check if Cell A (Date) is within the Date range of Cell B and C

I have a sheet with a timeline that shows a month per row in column A and an amount in USD next to that month in column B.
I want to be able to specify amounts in column G with a start and end date for that amount in columns E and F.
What I am trying to achieve is that the values in column B are automatically calculated by looking at the start and end dates specified in columns E and F and then taking the corresponding value from column G if the date in column A falls in between the date range specified in E and F.
I have found many suggestions for similar problems online but wasn't able to get any of them to work for my specific case. Any help is very welcome
You could do it as an array formula like this:
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("`E3:E"&count(E3:E)+2)),"YYMM"))*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))*transpose(indirect("G3:G"&count(G3:G)+2)),(INDIRECT("G3:G"&count(G3:G)+2)+2)^0))
The idea is to develop a 2D array where the rows are the months and the columns are the amounts for matching time periods. Then use the standard Mmult method to get the row totals of the array.
Using indirect for the ranges makes the formula longer but using full-column references would be slow as it would result in a nearly 1000 X 1000 array for a default-sized sheet.
EDIT 1
Or shorter
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("E3:E"&count(E3:E)+2)),"YYMM"))
*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))
,INDIRECT("G3:G"&count(G3:G)+2)))
because you can combine the row totals step with multiplication by column G.
EDIT 2
Alternatively you could just employ a much simpler pull-down formula using SUMIFS:
=ArrayFormula(sumifs(G$3:G,eomonth(E$3:E,-1)+1,"<="&A2,F$3:F,">="&A2))
This uses Eomonth to change all the start dates to the first of the month so they can be compared to the dates in column A correctly. The formula still has to be entered as an array formula because of the Eomonth calculation.
Note
The equivalent pull-down formula to the original array formulas above would be
=ArrayFormula(sumifs(G$3:G,text(E$3:E,"YYMM"),"<="&text(A2,"YYMM"),text(F$3:F,"YYMM"),">="&text(A2,"YYMM")))
but this gives zero for all rows - the reason for this is not obvious to me at time of writing.

Query formula Google Sheets with time

Does anyone know how I can add to the below query to find results between 2 time periods and sort from earliest to oldest.
'"&B5&"' in the query = MONDAY.
P in TempCSV is time from splitting date & time in column D with this formula in column O =iferror(split(D2, " ")) and this continues down the column for all other rows.
I cant use date and time from D as the date is creation date and not current date in relation to what I'm trying to achieve.
=query(TempCSV,"Select A,D where A contains 'Something' and A contains 'Anotherthing' and C contains '"&B5&"' **where P >=05:00:00 and P <=12:00:00**")
I have successfully got the following to work in another range but this is due to the date and time being correctly recorded in the same cell.
=query(TPM,"SELECT A,B,C where A contains 'Something' and A contains 'Anotherthing' and C >= datetime '"&text($C$5,"yyyy-mm-dd 05:00:00")&"' and C < datetime '"&text($C$5,"yyyy-mm-dd 12:00:00") & "'")

How to compare data within one column in Google Spreadsheets

I need to calculate a column in google spreadsheet.
Column B is a price column (priceUSD). I want to be able to calculate if the price the following day is 5 % higher or more, then it should return a 1.
Otherwise a 0 (if price is less than 5% higher the following day)
I do not want to add more columns for this only using column D
(column A is a date column)
A B C D
date priceUSD Addres Over5
=if(B1*1,05>B2) then return 1
Maybe, in D3, something like:
=ArrayFormula(1*(B2:B900*1,05<B3:B900))

Resources