Lookup a value from a range of dates in excel - vb.net

Please help.
We have two tables. A list of accounts and the other is a change log. I need to add a new column in table 1 where the value is the amount in table 2 for the correct account and correct validity period.
ex.
table 1:
account# beginning period ending period
1 January 1, 2012 January 31, 2012
2 January 12, 2012 February 12, 2012
table 2:
account # amount valid period beg valid period end
1 10 january 1, 2009 december 5, 2010
1 20 december 6, 2010 june 1, 2011
1 30 june 2, 2011 december 1, 2012
2 13 january 15, 2011 december 15, 2011
2 20 december 16, 2011 february 20, 2012
Thanks.

Although it is a bit complex requirement it could be done with built-in functions (although it can look a bit obscure :-) ). Specifically I mean function SUMIFS.
It has several parameters.
The first one is an area with values to be summed. It is B8:B12 in this example.
The second is an area whith values to be checked with some condition. It is A8:A12.
The third is a criterion to be applied for area from second parameter. It is (inter alia) account #.
So the formula says: sum all values from rows in B8:B12 where account # (A8:A12) is equal to desired account # (e.g. A3).
Ok, it is not all, you need specify the time range. It would be a bit clunky because you must check if two time period are overlapping (it would be easier to check if one date is in specified period).
But it could be done because SUMIFS can take another pairs of criteria range and criterion. But it cannot be used for OR condition, so you had to combine more SUMIFS.
Nice article about overlapping ranges is e.g. http://chandoo.org/wp/2010/06/01/date-overlap-formulas/
BTW: you have to format cells in B2:C3 and C8:D12 as a date to be able to compare them.

Related

Custom Week Number in SQL

I am trying to add a column to my data set that has a custom week number based on a specific start date.
For example, lets say that that a product launched Thursday September 6, and I want to measure signups by week. In this case, I would want September 6 - September 12 to be Week 1, September 13 - 19 as week 2, etc.
The two options I have found are using the datefirst function and creating a custom calendar table, but which of these would be easier to implement and more efficient?
Thanks!
Much better if you use date first - date end so that the users can also understand it easily.

Trying to format date to save worksheet

I am looking to save an individual worksheet from a workbook with today's date in the filename. ex. (c:\HotDogS\sales\Daily_12_04_16.xslx)
The problem I am encountering is getting the date to show right in the filename.
I have 1 cell that has the simple formula of =TODAY(). The cell is formatted for mm/dd/yy.
The formula I am trying to use to save the date for the sheet is:
=left(B3,2)&"_"&mid(B3,4,2)&"_"&right(B3,2)
So I am expecting a date of 12/04/16 to come out as 12_04_16, but I am getting is, 42_08_08.
Can somebody shed some light on this??
Thank you!!
Assuming TODAY is 4 December 2016, i.e. serial day number 42708, your formula is:
=left(B3,2)&"_"&mid(B3,4,2)&"_"&right(B3,2)
Left(B3,2) is 42. Mid(B3,4,2) is 08. Right(B3,2) is 08.
So your final result is 42_08_08.
You probably want to use (as an Excel formula):
=TEXT(B3,"mm\_dd\_yy")
Or in VBA you could use
Format(Range("B3").Value, "mm_dd_yy")
Excel stores dates as the number of days (and fractions of a day) since 0 January 1900. Therefore
1 January 1900 is day 1
31 January 1900 is day 31
1 February 1900 is day 32
29 February 1900 (even though it doesn't exist - but the bug has been maintained for backward compatibility) is day 60
1 January 1901 is day 367
4 December 2016 is day 42708
NOW() is day 42709.328 (it's about 7:52am on 5 December 2016 at the moment)
Referencing a cell formatted as date returns the underlying date serial number.
To get a date formatted as you want use
=TEXT(B3,"dd\_mm\_yy")

match event windows (in month) using sas sql

I have two files, one with event date, the other one is the security prices I need to match the month with event month 0, -1 (previous month), and +1 (the month after event month). So e.g., one observation has event date 1/1/2010, I have the daily bond prices from 1/1/2009 to 1/20/2010, but I need to match January,2010 to December 2009 prices and February 2010. I have the following codes to do that,
PROC SQL;
CREATE TABLE test AS
select *
FROM bondprice as a, sdc as b
where b.Participant_CUSIP=a.cusip
& INTCK('MONTH',b.Alliance_Date,a.time)>=-1
& INTCK('MONTH',b.Alliance_Date,a.time)<=1 ;
QUIT;
DATA test1;
SET test;
BY deal_number CUSIP code time;
IF first.code THEN price1=price;
IF first.code THEN day1=time;
IF last.code THEN price0=price;
IF last.code THEN day0=time;
RUN;
clearly this observation should be only matched in 1-month Jan 2010 (0,0) and 2-month Dec 2009 to Jan 2010 (-1,0), because it does not have 3-month Feb 2010's prices. However, if using my codes above, it will yield as a valid obs who has 3-month price since it fell into the range of >=-1 and <=1.
So the first part of code is not accurate (the >= and <= range): it yields any prices that falls into December 2009 to Feb 2010, this is wrong because of two situations. First, if the bond prices stops in between Dec 2009 and Jan 2010, it will be counting as a valid 2-month and 3-month observations. Second, if the bond prices begins in between Jan 2010 and Feb 2010, it will be counting as a valid 3-month observation. Both of these two situation will yield invalid observations, since the first situation do not have Feb 2010 prices but still counts as 2-month and 3-month, and the second situation do not have either Dec 2009 or Jan 2010 prices, but still counts as 3-month.
Will someone help me with this? How can I modify the code so that the 3-month window and 2-month window contains the relative months?
I hope I describe the problem clearly. Please let me know if anything is unclear.
I've uploaded an example to this problem here.
https://www.dropbox.com/s/hx9ahst7nts4k0q/ex1.xlsx?dl=0

MS Access grouping query spanning start and end dates

I would like to get a running tally of how many widgets were/are rented at any one time, by month, by year. Data is held in an MS Access 2003 db;
Table name: rent_table
Fields:
rentid
startdate
enddate
rentfee
rentcost
bookingfee
Something like; Count number of rentid's that fall between month/year, then group them?
e.g. if a widget was rented from 5th Jan 2014 to 8th April 2014 it would appear as a count in Jan, Feb, Mar and April tally's.
Many thanks.
EDIT
More details (sorry);
Access db is fronted by classic ASP.
If possible I don't want to create any new tables.
No input is required in order to run the report.
There are around 350-400 widgets that could be rented at any one time.
Each widget is rented exclusively.
Report output example;
Month | Year | NumRented
Jan 2014 86
Feb 2014 113
...
Can a query pick up dates within dates? So literally do a count of the table where date >Dec 31st 2013 AND <1st Feb 2014 (to grab a count for all of January 2014) and would that include the example of the rent starting on the 5th Jan? So I could just do twelve counts for each year?
create a calendar table, e.g.
table = cal_yyyymm with one column dt_yyyymm as numeric field
populate the table with ... say 5 or 10 years of data
201401 201402 201403 ... 60 or 120 rows, a small table
make a sql
Select
dt_yyyymm,
count(*) as cnt
From cal_yyyymm
Left Join rent_table
On format(startdate,"yyyymm") >= dt_yyyymm
And dt_yyyymm >= format(enddate,"yyyymm")
think about the complications in the data -- --
widget was rented from 5th Jan 2014 to 8th Jan 2014
and again rented from 11th Jan 2014 to 21st Jan 2014
does this count at 1 or 2 in the month?
if it is 1, then the sql gets more complicated because
the rent_table first needs to have its dates converted
to yyyymm format, and second needs to be de-duped on rentid,
and third then joined to cal_ On the dates...

Range for Oracle Date Data Type: "January 1, 4712 BC to December 31, 9999 AD", Does the range has any logic behind it?

Range for Oracle Date Data Type: "January 1, 4712 BC to December 31, 9999 AD", Does the range has any logic behind it?
I mean the range has any historic significance or it has something related to programming and memory size etc.
I am just wondering, why only from January 1, 4712 BC to December 31, 9999 AD.
That's the Julian date?
http://en.wikipedia.org/wiki/Julian_day
Wikipedia says 4713, hmm...off by 1...
I guess the upper limit is just because of the 4 digits.
Oracle doc says
http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#i1847
Julian dates allow continuous dating
by the number of days from a common
reference. (The reference is
01-01-4712 years BCE, so current dates
are somewhere in the 2.4 million
range.)
EDIT
I guess the reason for 4712 instead of 4713 is that the conversion requires Y >= -4712:
http://en.wikipedia.org/wiki/Julian_day#Converting_Julian_calendar_date_to_Julian_Day_Number
I suspect that it's set the upper limit for ease of formating to the highest 4 digit year, and then deduced the start of range by the capackity of the underlying type.
4712: have a look at julian day: http://en.wikipedia.org/wiki/Julian_day#Alternatives
9999: the highest value presented in 4 digits - at end of this year we will have another "milleniumbug"