Select data based on date match between 2 tables - google-sheets

I have 2 tables, in one there are transactions in USD, in the other there are monthly USD<>CHF exchange rates.
I want to automate the picking of the correct exchange rate for a given date.
tables
The formula would be inserted into 'Table 1 - Exch. Rate' column.
It should check what date it is ('Table 1 - Date') and then check which 'USD<>CHF' matches the date in Table 2.
For example, for Jan 30th it would insert 0.93061.
I played around with 'Query' but can't make it work. Is this the way to do it or is ther a better function?

you could go with a simple vlookup or xlookup function.
=index(if(len(A2:A),xlookup(A2:A,F:F,G:G,),))

Related

How to check for overlapping dates

I am looking for a solution on either Google sheets or app script to check for overlapping dates for the same account. There will be multiple accounts and the dates won't be in any particular order. Here is an example below. I am trying to achieve the right column "check" with some formula or automation. Any suggestions would be greatly appreciated.
Start Date
End Date
Account No.
Check
2023-01-01
2023-01-02
123
ERROR
2023-01-02
2023-01-05
123
ERROR
2023-02-25
2023-02-27
456
OK
2023-01-11
2023-01-12
456
OK
2023-01-01
2023-01-15
789
ERROR
2023-01-04
2023-01-07
789
ERROR
2023-01-01
2023-01-10
012
OK
2023-01-15
2023-01-20
012
OK
I also found some similar past questions, but they don't have the "for the same account" component and/or requires some sort of chronological order, which my sheet will not have.
How to calculate the overlap between some Google Sheet time frames?
How to check if any of the time ranges overlap with each other in Google Sheets
Another approach (to be entered in D2):
=arrayformula(lambda(last_row,
lambda(acc_no,start_date,end_date,
if(isnumber(match(acc_no,unique(query(query(split(flatten(acc_no&"|"&split(map(start_date,end_date,lambda(start_date,end_date,join("|",sequence(1,end_date-(start_date-1),start_date)))),"|")),"|"),"select Col1,count(Col2) where Col2 is not null group by Col1,Col2",0),"select Col1 where Col2>1",1)),0)),"ERROR","OK"))(
C2:index(C2:C,last_row),A2:index(A2:A,last_row),B2:index(B2:B,last_row)))(
counta(A2:A)))
Briefly, we are creating a sequence of dateserial numbers between the start & end dates for each row, doing some string manipulation to turn it into a table of account number against each date, then QUERYing it to get each account number which has dateserials with count>1 (i.e. overlaps), using UNIQUE to get the distinct list of those account numbers, then finally matching this list against the original list of account numbers to give the ERROR/OK output.
(1) Here is one way, considering each case which could result in an overlap separately:
=ArrayFormula(if(A2:A="",,
if((countifs(A2:A,"<="&A2:A,B2:B,">="&A2:A,C2:C,C2:C,row(A2:A),"<>"&row(A2:A))
+countifs(A2:A,"<="&B2:B,B2:B,">="&B2:B,C2:C,C2:C,row(A2:A),"<>"&row(A2:A))
+countifs(A2:A,">="&A2:A,B2:B,"<="&B2:B,C2:C,C2:C,row(A2:A),"<>"&row(A2:A))
)>0,"ERROR","OK")
)
)
(2) Here is the method using the Overlap formula
min(end1,end2)-max(start1,start2)+1
which results in
=ArrayFormula(if(byrow(A2:index(C:C,counta(A:A)),lambda(r,sum(text(if(index(r,2)<B2:B,index(r,2),B2:B)-if(index(r,1)>A2:A,index(r,1),A2:A)+1,"0;\0;\0")*(C2:C=index(r,3))*(row(A2:A)<>row(r)))))>0,"ERROR","OK"))
(3) Most efficient is to use the original method of comparing previous and next dates, but then you need to sort and sort back like this:
=lambda(data,sort(map(sequence(rows(data)),lambda(c,if(if(c=1,0,(index(data,c-1,2)>=index(data,c,1))*(index(data,c-1,3)=index(data,c,3)))+if(c=rows(data),0,(index(data,c+1,1)<=index(data,c,2))*(index(data,c+1,3)=index(data,c,3)))>0,"ERROR","OK"))),index(data,0,4),1))(SORT(filter({A2:C,row(A2:A)},A2:A<>""),3,1,1,1))
HOWEVER, this only checks for local overlaps. not globally. You can see what I mean if you change the dataset slightly:
Clearly the first and third pair of dates have an overlap but G4 contains "OK". This is because each pair of dates is only checked against the adjacent pairs of dates. This also applies to the original reference cited by OP - here's an example where it would give a similar result:
The formula posted by #The God of Biscuits gives the correct (global) result :-)

In Google Sheets, is there a way to output the values of the last month listed in each year in a table of monthly data spanning many years?

I have a table of raw financial data telling me what the value of an account was at the end of a given month (denoted in the date format MM/1/YY to normalize). There are multiple years worth of data in this table such that I may have something like this (please ignore the exact values, they are just placeholder):
1/1/18 $123
2/1/18 $21
3/1/18 $34
4/1/18 $45
5/1/18 $76
6/1/18 $1
7/1/18 $123
8/1/18 $4343
9/1/18 $2
10/1/18 $53
11/1/18 $123
12/1/18 $12
1/1/19 $213
2/1/19 $123
3/1/19 $34
4/1/19 $123
I'd like to create an array (or ideally a pivot) using this data such that it will summarize what the last value in a given year was, including partial years.
So for the above, it would output:
12/1/18 $12
4/1/19 $123
Or even better...do the cleaning up of formatting to change it to just the full year:
2018 $12
2019 $123
When I attempt to pivot the raw data, none of the "Summarize By" options seem to result in the desired output.
The raw data table is expected to grow in length over time, so the ideal solution stays current without needing manual updates. Thus, as the table grew to 2020, it would just add that more recent month's value from that year to the end of the output.
=ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(YEAR(INDIRECT("A1:A"&COUNTA(A1:A)))),
QUERY(SORT({A1:A,B1:B}, 1,0,2,0), "select year(Col1),Col2"), {1,2}, 0)))
In Google Sheets, you can do these using Sortn to sort on year using option 2 (ignore duplicates):
=sortn(sort(filter({year(A:A),A:B},A:A<>""),2,false),999,2,1,true)
The data is pre-sorted descending on date (using sort), so sortn picks out the row with most recent date in each year.
Breakdown of steps
(1) Add year to data using {} notation. Note this converts blank cells to 1899, which will have to be removed:
{year(A:A),A:B}
(2) Filter out blank cells:
filter({year(A:A),A:B},A:A<>"")
(3) Sort in descending order of date:
sort(filter({year(A:A),A:B},A:A<>""),2,false)
(4) Sort in ascending order of year, but dropping all dates except the first (most recent) in each year:
=sortn(sort(filter({year(A:A),A:B},A:A<>""),2,false),999,2,1,true)

When using SUMIF function, is there a way to accept timestamp values (e.g. 2/12/2019 19:56:16) as the range to test against the criteria?

I'm working with two columns,
column A: a list of timestamps
column B: a list of numbers
I'm trying to use the sumif(range, criteria, [sum range]) function to check if the month in column A is January, and so on for all months.
The issue is that I need a way to either convert the range for the sumif to month names, or use criteria other than a string (because a timestamp isn't going to ever be "=January".
My thought was that I could do either
=sumif(TEXT(TO_DATE(A2:A),"mmmm"), "=January", B2:B)
or
=sumif(A2:A, TEXT(TO_DATE(A2:A),"mmmm")="January", B2:B)
these are more or less pseudo-code, I'm trying to convey my thought process
I understand I could create a new column that converts the timestamps into months, but is there anyway I can preform this sumif, without having to make a new column?
Using SUMIFS, with dates you set the extents, this works in excel and google sheets:
=SUMIFS(B:B,A:A,">=" & DATE(2019,1,1),A:A,"<" & DATE(2019,2,1))
With Google Sheets you can also use filter:
=SUM(FILTER(B:B,MONTH(A:A)=1))
"=2" = February, "=3" = March, etc.
=ARRAYFORMULA(SUMIF(MONTH(A2:A), "=2", B2:B))
if you want to select it from a drop-down menu use:
=ARRAYFORMULA(SUMIF(MONTH(A2:A), "="&MONTH(F2&1), B2:B))

Combining the select clause in query function with Indirect function. - Google Spreadsheets

So basically I need a dynamic select statement which changes references when dragged across rows or columns.
Example of what I need.
=sum(query('Sheet1'!$A$1:$F$621, Indirect("Select"&$F&"Where A='ABC' AND B="&"Sheet2!"&$A1)))/20
Sample Sheet 1 (Data Sheet)..--Since I am not allowed to use images till i reach rep 10 lol :)
Column 1 - Sales Sites (ABC, DEF, GHI.....)
Column 2 - Sales Roles (SM, ASM, SE.....)
Column 3 - Sales in Month Jan
Column 4 - Sales in Month Feb
Column 5 - Sales in Month Mar
Sample Sheet 2 (Desired Output)
Description (in pivot terms):
Site wise (filter)
Role wise (Rows)
Month wise (Columns - Sum of Jan, Feb etc)
Value (Sum of Jan/20)--To get day wise sales numbers
FYI:
I have tried using pivots, but google spreadsheets don't allow use of calculated fields in pivots in any manner (for the /20 in the formula), hence trying to achieve the same results by formula.
I know a table on the basis of the pivot table could help solve this problem, but to make it more efficient I am trying to avoid using 2 tables.
Many Thanks for your help in advance, please let me know if you need additional info to understand the scenario.

Sum / Counting items in a column that match 3 criteria

Thank you ahead of time for anyone who can help me with this, I think I am close, but it still isn't working.
I have a simple sheet activity reporting sheet that I am asking staff to complete over the upcoming year - It has 5 columns:
Column A: Date -In format (4/4/2013 13:30:00)
Column B: Title -In format (text string)
Column C: Attendance -In format (Numbers)V
Column D: Vol led - In format (text string)
Column E: Staff Led - In format (text string)
Using this data I am 90 % positive that I can aggregate on a different summary sheet that contains some static data like months (in the B column) to aggregate on. I am having trouble configuring the criteria in the filters though to cause the correct output to either sum or count .
Quantity of events ed by either staff or vol, if neither box is checked the event should not be counted) Right now I am trying this but it is not working
=SUM(FILTER('Hostel Activities'!A:A,MONTH('Hostel Activities'!A:A)=$B3, NOT(AND(ISBLANK('Hostel Activities'!D:D),ISBLANK('Hostel Activities'!E:E)))
Total number of attendance in a month for activities led by staff or volunteers Right now I am trying this but it is not working
=SUM(FILTER('Hostel Activities'!A:A,MONTH('Hostel Activities'!A:A)=$B3, NOT(AND(ISBLANK('Hostel Activities'!D:D),ISBLANK('Hostel Activities'!E:E)))
THIE WORKS! ## Heading ##Total number of volunteer led activities in a month for activities Right now I am using this and it IS working
=COUNT(FILTER('Hostel Activities'!A:A,month('Hostel Activities'!A:A)=B3,not(isblank('Hostel Activities'!E:E))))
Thank you for any assistance and/or guidance
Danny
The first problem I see with your first two formulas is that you're calling SUM on your FILTER result. But the FILTER is returning the column A, which are dates. So, your basically summing dates, which will surely not yield the result you're looking for. Why are you not using COUNT, as you did on your last formula?
Second, the first two formulas you pasted are identical, how do you expect them to return different results?
It seems that for the first two want to sum an OR condition. You can do this two ways (that I can think of now). The simpler to understand is just to sum two COUNT(FILTER(... formulas, one for each criteria, e.g.
=COUNT(FILTER('Hostel Activities'!A:A,month('Hostel Activities'!A:A)=B3,not(isblank('Hostel Activities'!D:D)))) + B6
Assuming that on B6 is the other COUNT formula (the 3rd one, that already works).
Another option would be to use an OR function as criteria for the FILTER. Like this:
=COUNT(FILTER('Hostel Activities'!A:A,month('Hostel Activities'!A:A)=B3, OR(NOT(ISBLANK('Hostel Activities'!E:E)), NOT(ISBLANK('Hostel Activities1!D:D))) ))
I believe I have figured out a method that works by making some adjustments in the formulas and the source data.
Basically
IN THE SOURCE REPORTING DATA:
I combined columns D and E into the same column and added data validation so the coordinator has to enter if the activity is led by staff,volunteer, or neither.
IN THE MONTHLY AGGREGATION REPORT:
To count the number of activities led by either staff or volunteers I used this :
=COUNT(FILTER('Hostel Activities'!A:A,month('Hostel Activities'!A:A)=B3,'Hostel Activities'!D:D="Staff"))+E3
*E3 is the count of volunteer led activities which is found using this formula:
=COUNT(FILTER('Hostel Activities'!A:A,month('Hostel Activities'!A:A)=B3,'Hostel Activities'!D:D="Volunteer"))
Adding up the number of participants in activities run by either staff or volunteers was a little more difficult, but I was able to do it by adding up 2 unique equations. I would prefer using an OR statement in the filter criteria, but I just couldn't get that to work. This is how I was able to make it happen:
=SUM(FILTER('Hostel Activities'!C:C,month('Hostel Activities'!A:A)=B3,'Hostel Activities'!D:D="Staff")) + SUM(FILTER('Hostel Activities'!C:C,month('Hostel Activities'!A:A)=B3,'Hostel Activities'!D:D="Volunteer"))
Thank you all for your assistance

Resources