Match Value based on corresponding dates - google-sheets

I have the spreadsheet attached.
I'd like to find Client No from lookup sheet based on the date provided in the live sheet.
The same client can appear with a different client number, so i need to lookup the name and date (from live sheet) and find the corresponding client number in the lookup sheet where the date from live sheet falls between the 2 dates on the lookup sheet.
I hope this makes sense.
Any help appreciated.
Thank you

This might do what you're looking for.
=IFERROR(
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * WHERE COL4 >= DATE '"&TEXT(A2,"YYYY-MM-DD")&"' LIMIT 1",0),
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * LIMIT 1",0) )
I've added a tab Live-GK to your sheet, with this formula in C2. It has to be dragged down. There may be another approach where it can be done as an arrayformula, but I haven't figured that out.
Note that on my tab, I'm doing the lookups from Lookup-GK, since I could add more test data there. The above formula can be used as is, pasted into cell C2 in your Live tab.
Note that for debugging purposes, column H of my tab returns all of the columns, not just the client #, so the start and end dates can be verified.
Let me know if this helps you.
Explanation:
The inner filter selects all rows from the Lookup tab where:
i) the client name (column C in Lookup) matches the client name in column B (of Live), and,
ii) the start date (column A in Lookup) is less than or equal the client date in Live.
These records are sorted in descending date order.
Then the query selects the first record where the end date (column D in Lookup) is greater than the client date in Live.
If the Lookup record has no end date, this gives an error (empty query result) so IFERROR, a second query is run, but without the filtering by end date, selecting the one record with no end date, but an appropriate start date.
These seemed to work with the few test records I used. If there is a duplication of client dates, the first client # is returned. See client #1 and #7 in my test data. Some more error handling might be necessary if your client records might have overlapping date ranges, as CalculusWhiz asked.

Related

Trying to label rows in Google Sheets where there are duplicate values in one column in order of of date from another column

I have a list of 1000s of users, their assigned devices, and the device's end of life dates.
I need to label everyone's newest device (furthest end of life date) as Primary and all others as Secondary. The problem is with the duplicate users where some have multiple devices. For example, If someone has 3 devices I need to label 2 of them secondary and their newest one primary.
I hope this makes sense. I've attached an example screenshot of what I'm trying to achieve.
Tried using UNIQUE and COUNTIF and pivot tables but I'm not getting anywhere
Put this formula into D1.
What it does is, it get the data range A1:C8 to form a QUERY, which arrange the data according to 1st: User, 2nd: EndDate (in descending order),
than it uses FILTER function to filter the query with the row data of BYROW function, to found out if the date of each row is equal to the 1st date column of the filter result of the query or not, if true than apply "Primary", otherwise apply "Secondary".
If you need to change the data range, just change the ref.range of the very last line in the code.
=LAMBDA(DATA,
LAMBDA(QUERY,
LAMBDA(USER,PROFILE,DATE,
BYROW(DATA,
LAMBDA(ROW,
IF(INDEX(ROW,,1)="User",
"Device Usage",
IF(INDEX(ROW,,3)=INDEX(FILTER(QUERY,USER=INDEX(ROW,,1)),1,3),"Primary","Secondary")
)
)
)
)(INDEX(QUERY,,1),INDEX(QUERY,,2),INDEX(QUERY,,3))
)(QUERY({DATA},"ORDER BY Col1,Col3 DESC"))
)(A1:C8)
Just noticed that there are 2 unused variables, the code can be simplified a bit:
=LAMBDA(DATA,
LAMBDA(QUERY,
LAMBDA(USER,
BYROW(DATA,
LAMBDA(ROW,
IF(INDEX(ROW,,1)="User",
"Device Usage",
IF(INDEX(ROW,,3)=INDEX(FILTER(QUERY,USER=INDEX(ROW,,1)),1,3),"Primary","Secondary")
)
)
)
)(INDEX(QUERY,,1))
)(QUERY({DATA},"ORDER BY Col1,Col3 DESC"))
)(A1:C8)
This can also be done without the QUERY function:
=LAMBDA(DATA,
LAMBDA(USER_COL,
BYROW(DATA,LAMBDA(ROW,
LAMBDA(USER,DATE,
IF(USER="User",
"Device Usage",
IF(DATE=MAX(INDEX(FILTER(DATA,USER_COL=USER),,3)),"Primary","Secondary")
)
)(INDEX(ROW,,1),INDEX(ROW,,3))
))
)(INDEX(DATA,,1))
)(A1:C8)

Using Google Sheets Query to get Max Date for rows of data

I've been trying to use Google Sheets Query to find the more recent data of a particular name while pulling the rest of the data from the row. And having not luck.
This is a sample of what the Raw Data looks like:
And this is the output that I would like to achieve:
The closest I got to the output that I need is
=QUERY('Raw Data'!A:F,"select max(A), B, C, D, E group by B, C, D, E",1)
But the most important data is in F. When I add in F to 'select' the formula breaks, so I also grouped by F. Then that also pulls in the 4/1/17 data for Mark.
Anyone know whats going on? Or have a better formula for me to work with?
Thanks!
As far as what is going on, I worked with your formula and it picked up the extra date for Mark even before I added column F. As I understand it, if you want the max date per person, you really need =QUERY('Raw Data'!A:F,"select max(A),B group by B",1)
I put that in A1 in the worksheet I was using (not the same as the raw data one). So in its column A and B, I now had the maximum date and the person's name. Next to the first date, I put in column C the following formula, then dragged it down. I was simplifying to do just dates, but I believe it could be adapted to handle times if needed (if the person does multiple times a day and you want only the latest).
="'"&text(A3,"yyyy-mm-dd")&"'".
This gave me the date in single quotes, which I then used in column D, that built up the where clause, with the following formula in the first row,="(A=date "&C3&" and B='"&B3&"')" and the draggable formula =D3&" or "&"(A=date "&C4&" and B='"&B4&"')" in each subsequent row.
In the example with 3 people, the last clause ended up in D5 for me, so my final query was=QUERY('Raw Data'!A:F,"select A,B,C,D,E,F where"&D5,1), and that generated the desired results. My hesitation about this answer is whether it will scale, or whether it will too quickly hit a limit on length of the where clause.

Google Sheets: selecting cell values based on another cell MAX values

I'm trying to make a database of students using Google Sheets. It contains info about students, groups and orders; orders can change students membership in groups (taken in a group, moved up to a new group, graduated, on leave, sent down). Here are sample database sheets and here is a detailed description of my DB structure (the sheet report_Groups is slightly changed, its previous variant, described on the link, is now named old_report_Groups).
I need a query that would select a list of present members of given group on the given date. That means that for each student I have to select
the name, the latter status before given date and corresponding group. And from this result select student names, where statuses are "Taken in" or "Moved Up" and group is the same as given one.
The problem is to select the latter status. It should be MAX(status), whose "since" date ≤ given date, but there's a well-known problem of selecting more than one field together with aggregate function. Here is a question which is very close to, but query from its "best" answer gives me error "QUERY:NO_COLUMN". I've even copied the sheet Raw from there and tried to perform proposed query (with the onliest modification — replacing commas with semicolons according to my locale restrictions) on the data it was reported to work on — same error (check Raw and report_Raw sheets in my DB). Other variant (via MMULT and TRANSPOSE) works, but it's perfomance is very poor.
What can you suggest me? Thanks in advance.
Update: I've found the solution with an issue (described in my answer).
To solve the issue I need to know an answer for a different question.
Here's the solution (with an issue described below).
A. Orders_Students is filtered for selecting rows, having "since" cell value ≤ given date (report_Groups!A2):
=QUERY(Orders_Students!B:E;"select E, B, C, D where E <= date '" & TEXT(report_Groups!A2;"yyyy-MM-dd") & "'";1)
This interim result is stored at the inner_report_Groups tab (it will be referenced few times in the next query).
B. inner_report_Groups is filtered for selecting MAX("since") values and corresponding row cell values for each student:
ARRAYFORMULA(VLOOKUP(QUERY({ROW(inner_report_Groups!A$2:A)\SORT(inner_report_Groups!A$2:D)};"select max(Col1) group by Col3 label max(Col1)''";0);{ROW(inner_report_Groups!A$2:A)\SORT(inner_report_Groups!A$2:D)};{3\4\5};0)
The formula above is used as inner query in report_Groups!D2 (also in D3, D4—with appropriate indeces).
C. The second query result is filtered to get students whose status is either "Taken in" or "Moved Up" and corresponding group is equal to the given group (report_Groups!B2 (also in B3, B4—with appropriate indeces)):
=TRANSPOSE(IFERROR(QUERY(<here is the formula from step B>);"select Col1 where Col3 = '" & B2 & "' and (Col2='Taken in' or Col2='Moved Up')";0)))
The formula above is used as outer query in report_Groups!D2 (also in D3, D4—with appropriate indeces). IFERROR is intended to display nothing if query result is #N/A.
That query displays the needed results as you can see in report_Groups tab. But as the query on step B searches the whole columns of inner_report_Groups, there's only a single given date can be analysed (or the query interim results for other given dates should be placed in different columns of inner_report_Groups or at the different tab. Is there any way to give an alias for an interim result to refer it in a single cell formula instead of keeping it on different tab?

using both QUERY and FILTER together in a single statement?

I hope someone can help me; I am building some spreadsheets to help with time-tracking. I have a list of tasks, with columns for criteria including date, hours spent, category of work, and client.
I want to filter this data by month, so for example I would like to know how long I spent in a single month on correspondence. This means I need to select all the rows where category = 'correspondence' and where the dates are all from one specified month. At the moment, I am having to use a query which outputs to an intermediary table, and then run a filter function on that table in order to output to my final table. Here are my two functions:
=QUERY( 'Task List'!A4:F , "select A, B, E, F where C = 'Correspondence'" )
that gives me the first table, with just the rows where the category is "Correspondence". Then, on that table, I have to run the next function:
=filter(J4:M,J4:J>=date(2015,4,1),J4:J<=date(2015,4,31))
To get only the rows from this month of April. If possible I would like to remove the intermediary table (which serves no other purpose and just clutters my sheet).
Is it possible to combine these statements and do the process in one step?
Thanks.
That is indeed possible.
Since you didn't specify in which column the dates are to be found (in the 'raw' data), I assumed for this example that dates are in col F. The easiest way would be to use the MONTH() function. However, when used in query(), this function considers January as month 0. That's why I added the +1. See if this works ?
=QUERY( 'Task List'!A4:F , "select A, B, E, F where C = 'Correspondence' and month(F)+1 =4 ")
I came to this question needing to filter by weeknum() and year() as well as query by contains(). It can be helpful to combine the query and filter functions for similar but more dynamic date and text matching needs. If for example the OP had needed to show this data by week, that is not available in the Google Query Language.
The filter function does not have the contains function so you are limited to exact match text or using Reg-Ex. The Query Lanuague does not have the Weeknum functions.
Combining Filter and Query can be useful in scenario similar to this question but with a dynamic timeline (no hard set month or date such as rolling timeline) and where the text your matching is not exact (when you need to use a contains function from query language).
Here is an example for combining filter and query in Google sheets.
=(sum(Filter(QUERY(FB!$A:$Z, "select Q where B contains 'Apple'"), Weeknum (QUERY(FB!$A:$Z, "select E where B contains 'Apple'")) = Weeknum($A8))))
In this example I queried Facebook ads data export for any posts which contained the word 'Apple' in their title, and where Weeknum() matched the ongoing weeks on my sheet, in order to pull weekly data from multiple sources into one table to build reports, with minimal updating required as the timeline runs on.
It selects Q(spend) Where B(title) contains Apple, and Weeknum(E) matches week number on current row of sheet(A8). I have found this useful many times. Query + Filter Example Sheet Here.
If OP wanted to pull this info dynamically as the months went on if A column contained months in order the formula could be pulled along and would automatically pull data from query data filtered by matching month month.
=(sum(Filter(QUERY( 'Task List'!A:Z , "select A, B, E, F, J where C contains 'Correspondence'" ), Month(QUERY( 'Task List'!A4:F , "select J where C contains 'Correspondence'" )) = Month('$A2'))))

query one sheet based on cell in another sheet

My master data sheet is named MASTER.
In my sheet named PRINT I want to be able to use a date picker, or somehow be prompted for a date, and then a Query pulls data from Master where date selected matches dates in MASTER column B. I have tried the following Query statement but it will only work if I change both sheets Date columns to TEXT format. I need to keep my date formats as is. If all that makes sense, I would really appreciate some help. My end goal is I just want a huge list of fabrication jobs, listed by date, to be queried and pulled to another sheet for printing. Leaving all Master data in tact and where the PRINT sheet can simply have a date change done by any user and get the list they need.
=Query(MASTER!A:P,ʺselect * where B ='ʺ&A2&ʺ'ʺ,0)
The above Query is on my TEST sheet where I have a date picker in column A2
Try:
=Query(MASTER!A:P,"select * where B =date"""&text(A2, "yyyy-mm-dd")&""" ",0)
and see if that works for you ?

Resources