<?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: Calculate Moving Average Using Last 3 Time Periods in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38589#M22586</link>
    <description>&lt;P&gt;Here is the extension of the formula with the Customer added in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
tp = :timeperiod;
cus = :Customer;
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-1
);
lagtp1 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-2
);
lagtp2 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
Mean(
	:Quantity[dt &amp;lt;&amp;lt; get rows where( ( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 ) &amp;amp;
	:Customer == cus)
	]
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Apr 2017 16:41:31 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2017-04-27T16:41:31Z</dc:date>
    <item>
      <title>Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38514#M22546</link>
      <description>&lt;P&gt;I have a set of data with&amp;nbsp;three columns; Customer, TimePeriod, and Quantity. TimePeriod is in the format of yyyymm. I'm trying to determine a formula to calculate a "rolling average" over the last 3 time periods, across all customers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, In my row labeled 201704, I want a formula to return the average Quantity for all records from 201703, 201702, and 201701. The TimePeriod can occur multiple times in the data (by Customer), which makes it a little more complicated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 16:11:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38514#M22546</guid>
      <dc:creator>Risslemc</dc:creator>
      <dc:date>2017-04-26T16:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38525#M22553</link>
      <description>&lt;P&gt;Here is your formula&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
tp = :timeperiod;
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-1
);
lagtp1 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-2
);
lagtp2 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
Mean(
	:Quantity[dt &amp;lt;&amp;lt; get rows where( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 )
	]
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Apr 2017 18:00:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38525#M22553</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2017-04-26T18:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38529#M22556</link>
      <description>&lt;P&gt;Thank you very much for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm very new to scripting. Is there a way for the mean to be output in a new&amp;nbsp;column, by row? So as the script determines the mean over the last 3 timeperiods it will display that&amp;nbsp;for each row in a new column?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally, is there an issue with that&amp;nbsp;script at the beginning of the data set? When there's no&amp;nbsp;lagtp1 or lagtp2 for the very&amp;nbsp;first date?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:40:43 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38529#M22556</guid>
      <dc:creator>Risslemc</dc:creator>
      <dc:date>2017-04-26T20:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38530#M22557</link>
      <description>&lt;P&gt;What I gave you is a new formula that is intended to be used in a new column, that will create a new value for each row.&lt;/P&gt;
&lt;P&gt;In the data table, go to the pull down menu&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Cols==&amp;gt;New Column&lt;/P&gt;
&lt;P&gt;Name the column whatever you want&lt;/P&gt;
&lt;P&gt;In the lower left hand corner, click on&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Column Properties&lt;/P&gt;
&lt;P&gt;Select Formula&lt;/P&gt;
&lt;P&gt;Double click on the blue box that says "No Formula"&lt;/P&gt;
&lt;P&gt;In the window that opens, paste in the formula&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:46:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38530#M22557</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2017-04-26T20:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38559#M22574</link>
      <description>&lt;P&gt;Jim - Thank you for your patience.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I added that formula into a new column, as you guided, but I get the same repeating value down the whole column.&lt;/P&gt;&lt;P&gt;I would like the formula to just give me the mean for the last 3 time periods &lt;STRONG&gt;of that row&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, the Mean record from 201704 should only average the last 3 time periods from April 2017 (201704, 201703, 201702). The next row from 201705 should have a different&amp;nbsp;average consisting of the last 3 time periods from May 2017 (201705, 201704, 201703).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thoughts?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 12:50:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38559#M22574</guid>
      <dc:creator>Risslemc</dc:creator>
      <dc:date>2017-04-27T12:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38565#M22576</link>
      <description>&lt;P&gt;The formula that I gave you takes a look at the year/month value for a given row. &amp;nbsp;It then finds what the value 1 month prior is, and the value 2 months prior would be. &amp;nbsp;It then finds the mean for all rows in the data table that have the year/month value for any of the 3 calculated year/month values.&lt;/P&gt;
&lt;P&gt;Therefore, any row that has a year/month value of, let's say,&amp;nbsp;&lt;SPAN&gt;201705, would find the average quanity of all of the rows in the data table that are found to have the value of&amp;nbsp;201705, 201704 or 201703. &amp;nbsp;My limited test case worked for this. &amp;nbsp;Please note, the physical order of the rows in the data table are not of importance. &amp;nbsp;The formula finds all of the rows in the entire data table that match the 3 time periods for a given row.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you could provide me with a sample data table, I can look further into this. &amp;nbsp;Or, maybe my interpretation of what you are asking is not correct.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:13:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38565#M22576</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2017-04-27T13:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38584#M22584</link>
      <description>&lt;P&gt;You were correct! It must have been user error on my end :)&lt;/img&gt;&lt;/P&gt;&lt;P&gt;If I want the means&amp;nbsp;separated&amp;nbsp;by customer, let's say 'Customer' is in its own column to reference, is that possible?&lt;/P&gt;&lt;P&gt;So then the output column will only calculate the mean for the last 3 timeperiods for the customer in that row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:10:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38584#M22584</guid>
      <dc:creator>Risslemc</dc:creator>
      <dc:date>2017-04-27T15:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38589#M22586</link>
      <description>&lt;P&gt;Here is the extension of the formula with the Customer added in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
tp = :timeperiod;
cus = :Customer;
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-1
);
lagtp1 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
tmp = Date Increment(
	Date MDY( Num( Substr( :timeperiod, 5, 2 ) ), 1, Num( Substr( :timeperiod, 1, 4 ) ) ),
	"Month",
	-2
);
lagtp2 = Char( Year( tmp ) ) || Try( Substr( "0", Length( Char( Month( tmp ) ) ), 1 ), "" ) ||
Char( Month( tmp ) );
Mean(
	:Quantity[dt &amp;lt;&amp;lt; get rows where( ( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 ) &amp;amp;
	:Customer == cus)
	]
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:41:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38589#M22586</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2017-04-27T16:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Moving Average Using Last 3 Time Periods</title>
      <link>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38591#M22587</link>
      <description>&lt;P&gt;Thank you again for all your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That's exactly what I was looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Matt&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 16:46:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculate-Moving-Average-Using-Last-3-Time-Periods/m-p/38591#M22587</guid>
      <dc:creator>Risslemc</dc:creator>
      <dc:date>2017-04-27T16:46:51Z</dc:date>
    </item>
  </channel>
</rss>

