I am trying to merge two datasets df1, df2.
The data in my 1st dataset (df1) looks like this below
Id ServiceDate
234 2004-02-10
234 2003-11-05
234 2002-06-07
117458 2002-03-14
117458 2003-03-17
117458 2004-07-05
2195623 2002-04-12
2195623 2002-08-15
2195623 2002-09-10
This is the data in my second dataset (df2)
Id Effective_Dt Effct_End_Dt Capacity
234 2004-01-01 2004-12-31 10
234 2002-01-01 2003-12-31 17
117458 2000-03-14 2004-12-31 11
2195623 1995-04-01 2003-05-25 22
2195623 2003-05-26 2004-04-17 27
2195623 2004-04-18 2004-12-31 25
1) I am trying to merge these two datasets by ID
2) Inaddition to merging by="Id" , the Capacity value should be based on the df1$ServiceDate
between df2$Effective_Dt
and df2$Effct_End_Dt
.
For example, the expected output should look like this below
Id ServiceDate Capacity
234 2004-02-10 10
234 2003-11-05 17
234 2002-06-07 17
117458 2002-03-14 11
117458 2003-03-17 11
117458 2004-07-05 11
2195623 2002-04-12 22
2195623 2003-08-15 27
2195623 2004-09-10 25
I can do a merge by Id but not sure how to include the second logic which is including the Capacity based on the date constraint. Any help is much appreciated. Thanks.