Focal Point
[SOLVED] Using the LAST Function

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/792102533

January 07, 2010, 03:54 PM
ColdWhiteMilk
[SOLVED] Using the LAST Function
I am trying to create a column of data that has the difference between numeric format field value on the current record to the one on the previous record with the code below.

The PRVDELTA define correcly returns the previous record's value. However, the PRVDELTA2 define does not return previous record's value that corresponds with PRVDELTA.

Am I using the LAST Function correctly?

TABLE FILE BASELINE2
SUM
CT_DAYS AS 'CT_DAYS' 

BY SRT_MONTH AS 'SRT_MONTH'

ON TABLE HOLD AS 'BASELINE3'
END 


DEFINE FILE BASELINE3
PRVDELTA/YYMD= IF SRT_MONTH EQ LAST SRT_MONTH THEN SRT_MONTH ELSE LAST SRT_MONTH;
PRVDELTA2/I8= IF SRT_MONTH EQ LAST SRT_MONTH THEN CT_DAYS ELSE LAST CT_DAYS;
END

TABLE FILE BASELINE3
PRINT
SRT_MONTH AS 'Month'
CT_DAYS AS 'Cycle Days' 
PRVDELTA AS 'Previous Month'
PRVDELTA2 AS 'Previous Month Cycle Days'

BY SRT_MONTH NOPRINT

ON TABLE SET STYLE *
TYPE=DATA, COLUMN=N2, BACKCOLOR=BLUE, WHEN=CT_DAYS LT 14,$
TYPE=DATA, COLUMN=N2, BACKCOLOR=LIME, WHEN=CT_DAYS LT 19,$
TYPE=DATA, COLUMN=N2, BACKCOLOR=YELLOW, WHEN=CT_DAYS LT 24,$
TYPE=DATA, COLUMN=N2, BACKCOLOR=RED, WHEN=CT_DAYS GT 25,$
ENDSTYLE

ON TABLE SET HTMLCSS ON
ON TABLE SET PAGE-NUM OFF
ON TABLE NOTOTAL
END
-EXIT

This message has been edited. Last edited by: Kerry,


Production - 7.6.4
Sandbox - 7.6.4
January 07, 2010, 04:05 PM
GinnyJakes
DEFINE FILE BASELINE3
PRVDELTA/YYMD= IF SRT_MONTH EQ LAST SRT_MONTH THEN PRVDELTA ELSE LAST SRT_MONTH;
PRVDELTA2/I8= IF SRT_MONTH EQ LAST SRT_MONTH THEN PRVDELTA2 ELSE LAST CT_DAYS;
END

Try this instead.


Ginny
---------------------------------
Prod: WF 7.7.01 Dev: WF 7.6.9-11
Admin, MRE,self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable, Google
January 07, 2010, 04:33 PM
ColdWhiteMilk
That returnsthe following, which is still out of sync (look at the right hand column).

Month      Cycle Days      Previous Month      Previous Month Cycle Days 
2009/01/31 23                                  0 
2009/02/28 25              2009/01/31          22 
2009/03/31 20              2009/02/28          25 
2009/04/30 22              2009/03/31          20 
2009/05/31 19              2009/04/30          21 
2009/06/30 20              2009/05/31          18 
2009/07/31 24              2009/06/30          19 
2009/08/31 16              2009/07/31          23 
2009/09/30 17              2009/08/31          16 
2009/10/31 23              2009/09/30          17 
2009/11/30 26              2009/10/31          22 
2009/12/31 22              2009/11/30          26 



Production - 7.6.4
Sandbox - 7.6.4
January 07, 2010, 04:55 PM
GinnyJakes
PRVDELTA/YYMD= IF SRT_MONTH NE LAST SRT_MONTH THEN LAST SRT_MONTH ELSE PRVDELTA;
PRVDELTA2/I8= IF SRT_MONTH NE LAST SRT_MONTH THEN LAST CT_DAYS ELSE PRVDELTA2;

Try this. I always do my NE logic first as it makes more sense to me to figure out what I want to do on the sort break.


Ginny
---------------------------------
Prod: WF 7.7.01 Dev: WF 7.6.9-11
Admin, MRE,self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable, Google
January 07, 2010, 05:01 PM
Prarie
Also try try throwing in a sort on CT_DAYS
January 07, 2010, 05:05 PM
GinnyJakes
The data is already sorted by month so you don't need to do that. I did a prototype and verified my define code before I posted it.


Ginny
---------------------------------
Prod: WF 7.7.01 Dev: WF 7.6.9-11
Admin, MRE,self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable, Google
January 07, 2010, 06:14 PM
Hua
The LAST function seems to pickup the previous cycle days, except sometime gives (previous-days - 1)???


Developer Studio 7.6.11
AS400 - V5R4
HTML,PDF,XLS
January 08, 2010, 04:47 AM
Alan B
I suspect that the data in BASELINE3 is not as you are expecting, reason unknown. Your use of LAST is fine if the data coming in is correct. Try
TABLE FILE BASELINE3
SUM
           SRT_MONTH               AS 'Month'
           CT_DAYS                 AS 'Cycle Days' 
COMPUTE 
PRVDELTA/YYMD = IF SRT_MONTH EQ LAST SRT_MONTH THEN SRT_MONTH ELSE LAST SRT_MONTH; 
                                   AS 'Previous Month'
COMPUTE 
PRVDELTA2/I8 = IF SRT_MONTH EQ LAST SRT_MONTH THEN CT_DAYS ELSE LAST CT_DAYS; 
                                   AS 'Previous Month Cycle Days'
BY         SRT_MONTH               NOPRINT
END

and see what happens.


Alan.
WF 7.705/8.007
January 08, 2010, 09:41 AM
ColdWhiteMilk
This gives me the same results. Does that mean that it is something wrong with the code that creates the BASELINE3 hold file?


Production - 7.6.4
Sandbox - 7.6.4
January 08, 2010, 09:49 AM
GinnyJakes
Did you use the defines in my last post. I tested those with your data and got the right answers.

It appears to me that your data is correct. It must be sorted by SRT_MONTH and there is only one row per month. That is what I determine from your code.


Ginny
---------------------------------
Prod: WF 7.7.01 Dev: WF 7.6.9-11
Admin, MRE,self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable, Google
January 08, 2010, 10:41 AM
Hua
I wonder what will this give you?
PRVDELTA/YYMD= LAST SRT_MONTH ;
PRVDELTA2/I8= LAST CT_DAYS;



Hua


Developer Studio 7.6.11
AS400 - V5R4
HTML,PDF,XLS
January 08, 2010, 11:09 AM
ColdWhiteMilk
Ginny -

I greatly appreciate your help. I used your defines. It appears to be working correctly for the previous month column, but not the cycle days column.

For example, on the results I got above, the February record correctly shows that the previous month is January, but on the February row, the Previous Month (January) cycle days do not match the january record from above.

What would cause LAST to work for one field, but not the other? Field format? Sorting?


Production - 7.6.4
Sandbox - 7.6.4
January 08, 2010, 11:41 AM
Tony A
CWM,

To be sure that you are using the correct LAST operation that matches your sort requirement, use COMPUTE within the TABLE request as per Alan above.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
January 08, 2010, 11:56 AM
ColdWhiteMilk
Even when I use the COMPUTE, I get this:

Month      Cycle Days Previous Month Previous Month Cycle Days 
2009/01/31 23                        0 
2009/02/28 25         2009/01/31     22 
2009/03/31 20         2009/02/28     25 
2009/04/30 22         2009/03/31     20 
2009/05/31 19         2009/04/30     21 


The January cycle days for row 1 & row 2 do not match, but should.

However, the February cycle days for row 2 & row 3 do? match.

Why does it only return the correct data sometimes?


Production - 7.6.4
Sandbox - 7.6.4
January 08, 2010, 12:47 PM
Tom Flynn
As Hua suggested; also, use SUM instead of PRINT:
COMPUTE 
PRVDELTA2/I8 = LAST CT_DAYS; AS 'Previous Month Cycle Days'



Tom Flynn
WebFOCUS 8.1.05 - PROD/QA
DB2 - AS400 - Mainframe
January 08, 2010, 01:30 PM
ColdWhiteMilk
Solved it!

The problem was not with the DEFINE itself, or the compute.

The problem was being caused by the PRVDELTA2 field formatting being I8, but the CT_DAYS was formatted as D8S. Once I made PRVDELTA2 to a D8S format, it fixed the problem.

Thank you all for your help with this.

-CWM


Production - 7.6.4
Sandbox - 7.6.4