<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Rolling Sum of Previous Row Values with Condition in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312641#M56533</link>
    <description>&lt;P&gt;Yes, that is right.&lt;/P&gt;&lt;P&gt;But I want to maintain the original data table without excluding some rows to get this result.&lt;/P&gt;&lt;P&gt;Can this be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Sep 2020 21:22:06 GMT</pubDate>
    <dc:creator>bzanos</dc:creator>
    <dc:date>2020-09-24T21:22:06Z</dc:date>
    <item>
      <title>Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/310952#M56425</link>
      <description>&lt;P&gt;My data starts with two column, Lot# &amp;amp; Qty.&lt;/P&gt;&lt;P&gt;I need help to create script to calculate rolling sum (current row + previous 2 rows) of previous row values.&lt;/P&gt;&lt;P&gt;The logic is explained as below table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lot1.JPG" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/26915i20107014B40C0E63/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lot1.JPG" alt="Lot1.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 23:38:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/310952#M56425</guid>
      <dc:creator>Lino</dc:creator>
      <dc:date>2023-06-09T23:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311119#M56433</link>
      <description>&lt;P&gt;One way might be to use a formula like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2020-09-22 at 3.04.46 PM.png" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/26919iDBDF89E55F0B9C0B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2020-09-22 at 3.04.46 PM.png" alt="Screen Shot 2020-09-22 at 3.04.46 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1,
	:Qty,
	If( Row() == 2,
		Lag( :Qty, 1 ) + :Qty,
		:Qty + Lag( :Qty, 1 ) + Lag( :Qty, 2 )
	)
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For the first row there is nothing to sum, so it just uses row 1&lt;/P&gt;
&lt;P&gt;For the second row there are only two values, the current and previous, so its row+lag(1)&lt;/P&gt;
&lt;P&gt;All rows after the second it takes the sum of the current row plus the previous two rows :lag(1) and lag(2)&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 19:07:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311119#M56433</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2020-09-22T19:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311539#M56465</link>
      <description>&lt;P&gt;Thanks for proving idea of using formula.&amp;nbsp;&lt;/P&gt;&lt;P&gt;My dataset is not able to do rolling sum directly using the formula because some lot data is not take into count.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example,&lt;/P&gt;&lt;P&gt;Lot# 3 and Lot# 3.2, only Lot# 3.2 qty is count in rolling sum because it is final lot qty.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additional qty added in Lot#3. It become Lot#3.2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is the same for Lot# 7, 7,2, 7,3 and Lot# 10, 10.3.&lt;/P&gt;&lt;P&gt;Lot# 7.3 qty and 10.3 is count in rolling sum because it is final lot qty.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need guidance from the community.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lot3.JPG" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/26926i0ED5979FCC318F59/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lot3.JPG" alt="Lot3.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 13:25:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311539#M56465</guid>
      <dc:creator>Lino</dc:creator>
      <dc:date>2020-09-23T13:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311580#M56467</link>
      <description>&lt;P&gt;Right, I get it now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Need to find the max value for each lot, then take the rolling sum of the current and last two max lot values.&lt;/P&gt;
&lt;P&gt;I feel like there is a lurking solution, might need to think a little more on that one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 14:52:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/311580#M56467</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2020-09-23T14:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312307#M56520</link>
      <description>&lt;P&gt;I will figure out how to get this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just in case anyone has solution for this, please share as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 15:26:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312307#M56520</guid>
      <dc:creator>Lino</dc:creator>
      <dc:date>2020-09-24T15:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312384#M56526</link>
      <description>&lt;P&gt;Is this right. (the right most column in the picture)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2020-09-24 at 2.32.20 PM.png" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/26972i6FBEDCB9ECF7B327/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2020-09-24 at 2.32.20 PM.png" alt="Screen Shot 2020-09-24 at 2.32.20 PM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 18:32:53 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312384#M56526</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2020-09-24T18:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312641#M56533</link>
      <description>&lt;P&gt;Yes, that is right.&lt;/P&gt;&lt;P&gt;But I want to maintain the original data table without excluding some rows to get this result.&lt;/P&gt;&lt;P&gt;Can this be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 21:22:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312641#M56533</guid>
      <dc:creator>bzanos</dc:creator>
      <dc:date>2020-09-24T21:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312704#M56537</link>
      <description>&lt;P&gt;Add this script to you data table and give it a spin&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt1=current data table();
//I want to get the max quantity by lot, so I'm adding a new column to change all the lots back to their non-decimal value.
dt1&amp;lt;&amp;lt; New Column("Lot Actual", formula(If( :Qty == Col Maximum( :Qty, Char( Round( Num( :Lot# ), 0 ) ) ),
	:Lot#,
	.
)));&lt;BR /&gt;//this next formula column excludes rows that aren't the max Qty by lot
dt1&amp;lt;&amp;lt; New Column( "Lot Values in One Formula",
		Formula( 
			If( :Lot Actual == "",
				Row State( row() ) = Excluded State( 1 );
				Excluded( Row State( row() ) );
				1;
			)
		)
	 );
dt1&amp;lt;&amp;lt;run formulas();
&lt;BR /&gt;//once all the formulas for tagging the data are added, we can use Tabulate to get a subset of just the important rows.&lt;BR /&gt;//There are lots of other ways to do this, this just happened to be the method of the day	
	
obj1=dt1 &amp;lt;&amp;lt; Tabulate(
	Add Table(
		Column Table( Analysis Columns( :Qty ) ),
		Row Table( Grouping Columns( :Lot# ) )
	)
);
dt2=obj1 &amp;lt;&amp;lt; Make Into Data Table;
column(dt2,2)&amp;lt;&amp;lt;set name("Lot Max");&lt;BR /&gt;&lt;BR /&gt;//Finally here is the rolling sum of the last three lots&lt;BR /&gt;//there is moving average function we could have used, it would have looked simpler but more abstract 
dt2&amp;lt;&amp;lt;new column("Rolling Sum of 3",formula(If( Row() == 1,
	:Name( "Lot Max" ),
	If( Row() == 2,
		Lag( :Name( "Lot Max" ), 1 ) + :Name( "Lot Max" ),
		:Name( "Lot Max" ) + Lag( :Name( "Lot Max" ), 1 )
		+Lag( :Name( "Lot Max" ), 2 )
	)
)));
&lt;BR /&gt;//&lt;BR /&gt;//This part joins the values from the separate table back to the original table&lt;BR /&gt;
dt1 &amp;lt;&amp;lt; Update(
	With( dt2 ),
	Match Columns( :Lot# = :Lot# )
);
close(dt2, nosave);
obj1&amp;lt;&amp;lt;close window;
dt1&amp;lt;&amp;lt; Delete Columns("Lot Actual", "Lot Max", "Lot Values in One Formula");

///&lt;BR /&gt;//I feel like this step is cheating a little. This step just repeats the missing values down to the next non-missing&lt;BR /&gt;//row. Its some code I got from Brady Brady a ways back. Different iterations of come in handy from time to time.
&lt;BR /&gt;dtnr=current data table();

dtcollist=dtnr&amp;lt;&amp;lt;Get Column Names;
dtMat = (dtnr &amp;lt;&amp;lt; Get All Columns As Matrix);
selection={}; insert into (selection, column(4)); //these columns get acted on
For( i = 1, i &amp;lt;= N Items( selection ), i++,
colNum = Contains( dtColList, Name Expr( selection[i] ) );
vMat = dtMat[0, colNum];
mis = Loc( Is Missing( vMat ) );
If( N Row( mis ),
col = selection[i];
If( mis[1] == 1,
col[1] = col[(Loc( vMat ))[1]]
);

For( j = if(mis[1]==1, 2, 1), j &amp;lt;= N Row( mis ), j++,
col[mis[j]] = col[mis[j] - 1]
);
);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Sep 2020 15:51:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312704#M56537</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2020-09-30T15:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312950#M56560</link>
      <description>&lt;P&gt;Thanks Byron.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 12:06:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/312950#M56560</guid>
      <dc:creator>Lino</dc:creator>
      <dc:date>2020-09-25T12:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Sum of Previous Row Values with Condition</title>
      <link>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/314994#M56710</link>
      <description>&lt;P&gt;Thanks for posting your question. That was a fun, but twisty problem to work out. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added some annotation to the JSL, hopefully is makes sense and doesn't just look like a giant magic formula.&lt;/P&gt;
&lt;P&gt;Honestly, at first I thought it would be nearly impossible to sort out, but after bigger cup of coffee,.. not so bad.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 15:54:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rolling-Sum-of-Previous-Row-Values-with-Condition/m-p/314994#M56710</guid>
      <dc:creator>Byron_JMP</dc:creator>
      <dc:date>2020-09-30T15:54:46Z</dc:date>
    </item>
  </channel>
</rss>

