cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Generate sequence of dates between 2 dates

mbostrom_dtiq
New Contributor II

i have a requirement to โ€œfill in the gaps in timeโ€ per se on data from a system. Here is my use case:

A customer signed up with the company for $100 subscription monthly for 3 years. The company added services 6 months into the contract which bumped their subscription up to $120 / month, and again a couple months later. but then never again for a couple years. however our ERP only shows the effective dates based on the change. so the json from the system would look this this but I need to create records for every missing month using the previous month if no changes were made. Then when the next change made use that and generate the missing time between that month and subsequent months until the last change. this will allow us to show trending over time and the revenue for that customer by every month in which they have been a customer. what is the best way to achieve this? a parent customer can have multiple locations in which this could change for this example iโ€™ve simplified it to just 1 customer with multiple records so i believe some level of grouping and min/max dates need to be used.

[
{
โ€œParent_Customer_Idโ€: 12345,
โ€œLocation_Site_IDโ€: 98765,
โ€œParent_Customer_Nameโ€: โ€œAcmeโ€,
โ€œBusiness_Nameโ€: โ€œAcme Store 1โ€,
โ€œMonthly_Change_Tracking_Idโ€: 7,
โ€œEffective_Dateโ€: โ€œ2015-12-16T00:00:00.000โ€,
โ€œMonthly_Revenueโ€: 100
},
{
โ€œParent_Customer_Idโ€: 12345,
โ€œLocation_Site_IDโ€: 98765,
โ€œParent_Customer_Nameโ€: โ€œAcmeโ€,
โ€œBusiness_Nameโ€: โ€œAcme Store 1โ€,
โ€œMonthly_Change_Tracking_Idโ€: 21,
โ€œEffective_Dateโ€: โ€œ2016-06-16T00:00:00.000โ€,
โ€œMonthly_Revenueโ€: 120
},
{
โ€œParent_Customer_Idโ€: 12345,
โ€œLocation_Site_IDโ€: 98765,
โ€œParent_Customer_Nameโ€: โ€œAcmeโ€,
โ€œBusiness_Nameโ€: โ€œAcme Store 1โ€,
โ€œMonthly_Change_Tracking_Idโ€: 44,
โ€œEffective_Dateโ€: โ€œ2016-08-16T00:00:00.000โ€,
โ€œMonthly_Revenueโ€: 130
},
{
โ€œParent_Customer_Idโ€: 12345,
โ€œLocation_Site_IDโ€: 98765,
โ€œParent_Customer_Nameโ€: โ€œAcmeโ€,
โ€œBusiness_Nameโ€: โ€œAcme Store 1โ€,
โ€œMonthly_Change_Tracking_Idโ€: 55,
โ€œEffective_Dateโ€: โ€œ2020-12-16T00:00:00.000โ€,
โ€œMonthly_Revenueโ€: 150
}
]

2 REPLIES 2

koryknick
Employee
Employee

There two basic challenges to tackle here.

  1. Get the โ€œnextโ€ date (from the next record) to know how many records to fill
  2. โ€œCreateโ€ records to fill the gap

To get the next date, you need to first combine records within a single JSON document so you can iterate through them as a set. I would think to use a Group By Fields snap to combine the logical sets of records; this will produce an array of records per group.

Now use a Mapper to generate a new field into the group for โ€œnextโ€ or โ€œprevโ€ date on each record. Something like this:

$group.map((elem, index, array)=> elem.extend({ "Prev_Effective_Date" : (index==0 ? null : array[index-1].Effective_Date ) } ) )

For the second part, you can use the JSON Generator snap with some Velocity syntax to loop through and create the extra records. It may simplify things if you only output the โ€œbase dateโ€ and a month incrementor in the JSON Generator, then follow it with a Mapper to calculate the new โ€œEffective_Dateโ€ for the gap-fill records.

Sorry I donโ€™t have time to put together an example pipeline for you, but I hope this gives you a potential direction to work towards.

Obviously, this isnโ€™t the only way to do itโ€ฆ Iโ€™m sure there are other contributors that have other ideas.

Good luck and happy snapping!

koryknick
Employee
Employee

You could also look into using a Script snap in your choice of languages: Python, Javascript, or Ruby.