cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
abmayfield
Level VI

trailing cumulative sum formula

I have what is likely to be a very easy formula need. I have a temperature dataset and need to calculate a trailing cumulative sum over the past 120 days (known as "degree-heating weeks" since I will convert this sum to weeks by dividing by 7). I know how to get a trailing average, and I know how to get a cumulative sum, but is it possible to get a trailing, 120-day cumulative sum? In this case, when the temperature passes 31.3C, a "score" of 0.25 is given (see "DH-6 hrs" column for formula.). Basically, I want the cumulative sum of these 0.25 values over a 120-day window, but I only know how to partition by date/month/year, etc. (see "#DHWs" script for example), not by an actual window of time. I feel confident that this must be doable!

Anderson B. Mayfield
11 REPLIES 11
txnelson
Super User

Re: trailing cumulative sum formula

If you add the following to the formula

If( Row() == ????, 
     new table("row" || char(row()),new column("Values", set values(holdMatrix)),
new column("dates", set values(dateMatrix), format("m/d/y"))); ); (x / 24) / 7;

and just replace the ???? with the row number of where you think the data is going funny, you will then see a data table with all of the values that were added into the value, and the dates the values were collected from.  That might give you a clue as to what is going on.

Jim
txnelson
Super User

Re: trailing cumulative sum formula

Below is a new formula that you can use for debugging.  It will create the data table for whatever row number you put into the If() statement at the end.  If you do not want to create a data table, then just change the row number that you are trying to find, to -1.  There will never be a -1 row, so a table will not be created.  Anyway, see if you can see what is going on and where an error is occurring.

 

Make sure the table is sorted correctly and any rows you don't want to be processed are deleted from the table.  The script does not honor Excluded rows.

If( Row() == 1,
	holdMatrix = [];
	dateMatrix = [];
	rowMatrix = [];
);
If( Is Missing( :Name( "Date Time (UTC)" ) ) == 0,
	If(
		Is Missing( :Name( " Sea Surface Temperature (deg C)" ) ) == 1, holdMatrix = holdMatrix || 0,
		:Name( " Sea Surface Temperature (deg C)" ) > 31.3, holdMatrix = holdMatrix || :time difference 2,
		:Name( " Sea Surface Temperature (deg C)" ) <= 31.3, holdMatrix = holdMatrix || 0
	);
	dateMatrix = dateMatrix || Informat( :date, "m/d/y" );
	rowMatrix = rowMatrix || Row();
	If( N Cols( dateMatrix ) & N Cols( holdMatrix ) != 0,
		While(
			dateMatrix[N Cols( dateMatrix )] > dateMatrix[1] + In Days( 120 ) & N Cols( holdMatrix ) > 0 &
			N Cols( dateMatrix ) > 0,
			dateMatrix[1] = [];
			holdMatrix[1] = [];
			rowMatrix[1] = [];
		);
		If( N Cols( dateMatrix ) & N Cols( holdMatrix ) != 0,
			x = Sum( holdMatrix ),
			x = .
		);
	,
		x = .
	);
,
	x = .
);
If( Row() == 22159,
	New Table( "row" || Char( Row() ),
		New Column( "RowNum", set values( rowMatrix ) ),
		New Column( "Values", set values( holdMatrix ) ),
		New Column( "dates", set values( dateMatrix ), Format( "m/d/y" ) )
	)
);
(x / 24) / 7;
Jim