Jump to content
Sign in to follow this  
rivet

custom function strange behavior

Recommended Posts

Hi CFD, longtime user, first time builder!

I need to build a list of dates, from a starting date, up to the 1st of the month after Get ( CurrentDate ).
Each date will start on the 1st, and will spaced by an interval of so many months.

EXAMPLE
ƒ serviceSchedule ( Date ( 5 ; 1 ; 2017 ) ; 1 )    //monthly
= 5/1/2017, 6/1/2017, 7/1/2017, 8/1/2017, 9/1/2017, 10/1/2017, 11/1/2017, 12/1/2017, 1/1/2018, 2/1/2018, 3/1/2018

ƒ serviceSchedule ( Date ( 5 ; 1 ; 2017 ) ; 3 )   //quarterly
= 7/1/2017, 10/1/2017, 1/1/2018

 

I have the loop start on January of the startDate year, and loop from there.  I do get the results but the behavior of the CF is peculiar, which I am guess might be use of local variables ($).  

Attached is a test file, try the function in the data viewer and note that you have to click evaluation twice to get the result.

 

 

// serviceSchedule ( startDate ; interval )


Case (

	// Initialize
	not $loopStopDate ;
  
	Let ( [

		$itvl = interval ;
		$sd = startDate ;
		$startYear = Year ($sd) ;
		$cd = Get ( CurrentDate ) ;
		$loopStopDate = Date ( Month($cd)+1 ; 1 ; Year($cd) ) ;
		$loopInterval = 1 ;

		$dte = Date ( 1 ; 1 ; $startYear ) 
  
	];

		serviceSchedule ( $dte ; $itvl )
  
	) ;



	// Ignore and up date
	startDate < $sd ;
	
	Let ( [

		$dte = Date ( 0 + $loopInterval ; 1 ; $startYear ) ;
		$loopInterval = $loopInterval + $itvl

	];

		serviceSchedule ( $dte ; $itvl )
 
	) ;



	// Add to array up date
	startDate < $loopStopDate ;
	
	Let ( [

		$array = case ( isEmpty ($array) ; "" ; $array & "¶" ) & $dte  ; 
		$dte = Date ( 0 + $loopInterval ; 1 ; $startYear ) ;
		$loopInterval = $loopInterval + $itvl

	];

		serviceSchedule ( $dte ; $itvl )
 
	) ;



 	 // END
	startDate > $loopStopDate ;
	
	Let ( [
		result = $array ;

		$array = "" ;
		$cd  = "" ;
		$dte = "" ;
		$itvl = "" ;
		$loopInterval = "" ;
		$loopStopDate = "" ;
		$sd = "" ;
		$startYear = "" 

     ];

		result 

     )


)

 

serviceSchedule.zip

Share this post


Link to post
Share on other sites

That seems unnecessarily complicated. You just need to feed the date + the interval back into the function:

// serviceSchedule ( startDate ; interval )
 

Let ( [
    cd = Get ( CurrentDate ) ;
    stopDate = Date ( Month(cd)+1 ; 1 ; Year(cd) ) ;
    startDate = Date ( Month( startDate ) ; 1 ; Year(startDate) )
    ];

If ( startDate <= stopDate ;
    List( startDate ;
    serviceSchedule (  Date ( Month( startDate ) + interval ; 1 ; Year(startDate) ) ; interval ) ) ; "" )

  )

 

Share this post


Link to post
Share on other sites

Thanks Fitch for taking a look at this.  I sensed it might be over built but I still need an extra step.   The schedules are monthly, bi-monthly and quarterly.  So I would only want to start the list once the calculated date is on or after startDate.

i.e. startDate of May1
monthly schedule would start month 5 and onward
quarterly schedule would start month 7 and onward.

 

 

 

Edited by rivet

Share this post


Link to post
Share on other sites

There might be a more elegant solution, but try this:

Let ( [
    cd = Get ( CurrentDate ) ;
    stopDate = Date ( Month(cd)+1 ; 1 ; Year(cd) ) ;
    mb = Choose( Month( startDate ) ; "" ; 1 ; 3 ; 3 ; 5 ; 5 ; 7 ; 7 ; 9 ; 9 ; 11 ; 11 ; 1 ) ;  // bimonthly
    mq = Choose( Month( startDate ) ; "" ; 1 ; 4 ; 4 ; 4 ; 7 ; 7 ; 7 ; 10 ; 10 ; 10 ; 1 ; 1 ) ;  // quarterly
    m = Choose( interval ; "" ;  Month( startDate ) ; mb ; mq ) ;
    startDate = Date ( m ; 1 ; Year(startDate) )
    ];

If ( startDate <= stopDate ;
    List( startDate ;
    serviceSchedule (  Date ( Month( startDate ) + interval ; 1 ; Year(startDate) ) ; interval ) ) ; "" )

  )

 

Share this post


Link to post
Share on other sites

I will give it a try, thanks again.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.