turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Calculate Moving Average Using Last 3 Time Periods

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 26, 2017 9:11 AM
(1463 views)

I have a set of data with 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.

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.

Any suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 26, 2017 11:00 AM
(2806 views)

Solution

Here is your formula

```
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 << get rows where( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 )
]
);
```

Jim

8 REPLIES

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 26, 2017 11:00 AM
(2807 views)

Here is your formula

```
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 << get rows where( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 )
]
);
```

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 26, 2017 1:40 PM
(1397 views)

Thank you very much for your reply.

I'm very new to scripting. Is there a way for the mean to be output in a new column, by row? So as the script determines the mean over the last 3 timeperiods it will display that for each row in a new column?

Additionally, is there an issue with that script at the beginning of the data set? When there's no lagtp1 or lagtp2 for the very first date?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 26, 2017 1:46 PM
(1394 views)

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.

In the data table, go to the pull down menu

Cols==>New Column

Name the column whatever you want

In the lower left hand corner, click on

Column Properties

Select Formula

Double click on the blue box that says "No Formula"

In the window that opens, paste in the formula

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 27, 2017 5:50 AM
(1367 views)

Jim - Thank you for your patience.

I added that formula into a new column, as you guided, but I get the same repeating value down the whole column.

I would like the formula to just give me the mean for the last 3 time periods **of that row**.

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 average consisting of the last 3 time periods from May 2017 (201705, 201704, 201703).

Thoughts?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 27, 2017 6:13 AM
(1365 views)

The formula that I gave you takes a look at the year/month value for a given row. It then finds what the value 1 month prior is, and the value 2 months prior would be. 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.

Therefore, any row that has a year/month value of, let's say, 201705, would find the average quanity of all of the rows in the data table that are found to have the value of 201705, 201704 or 201703. My limited test case worked for this. Please note, the physical order of the rows in the data table are not of importance. The formula finds all of the rows in the entire data table that match the 3 time periods for a given row.

If you could provide me with a sample data table, I can look further into this. Or, maybe my interpretation of what you are asking is not correct.

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 27, 2017 8:10 AM
(1357 views)

You were correct! It must have been user error on my end :-)

If I want the means separated by customer, let's say 'Customer' is in its own column to reference, is that possible?

So then the output column will only calculate the mean for the last 3 timeperiods for the customer in that row.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 27, 2017 9:41 AM
(1348 views)

Here is the extension of the formula with the Customer added in

```
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 << get rows where( ( :timeperiod == tp | :timeperiod == lagtp1 | :timeperiod == lagtp2 ) &
:Customer == cus)
]
);
```

Jim

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 27, 2017 9:46 AM
(1346 views)

Thank you again for all your help!

That's exactly what I was looking for.

-Matt