Results 1 to 8 of 8

Thread: [RESOLVED] DateDiff - First Record

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Resolved [RESOLVED] DateDiff - First Record

    Hi,
    I am using a datediff formula to tell the number of days between records. The problem I am running into is that on the first row of each group, it looks at the last row of the previous group to tally the difference. I am trying to set the field equal to zero for the first row of each group. Does anyone know how to tell if the row is the first one in the group?
    thanks!

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: DateDiff - First Record

    You could Insert a Running Total field that Counts the records and Reset on change of Group

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: DateDiff - First Record

    Assuming Crystal Reports...

    Use the Previous function to see if the previous record and the current record are in the same group.

    Code:
    //very first record in the report should always return 0
    If PreviousIsNull ({Customers.CustomerID}) Then
      0
    Else
      //only calculate days between orders after the first record in the CustomerId Group
     //formula returns 0 if the following is false (could also use an Else clause).
      If Previous ({Customers.CustomerID}) = {Customers.CustomerID} Then
         DateDiff ("d", Previous ({Orders.OrderDate}) ,{Orders.OrderDate})

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: DateDiff - First Record

    jggtz, can you explain this a little more. Would I have one variable (Variable1) and set it := 0 then have another (Variable2) and set it := Variable1 +1 ????

    Quote Originally Posted by jggtz
    You could Insert a Running Total field that Counts the records and Reset on change of Group

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: DateDiff - First Record

    This works great Brucevde! Thanks! I worked on that crude for two hours yesterday! Thanks again!

    Quote Originally Posted by brucevde
    Assuming Crystal Reports...

    Use the Previous function to see if the previous record and the current record are in the same group.

    Code:
    //very first record in the report should always return 0
    If PreviousIsNull ({Customers.CustomerID}) Then
      0
    Else
      //only calculate days between orders after the first record in the CustomerId Group
     //formula returns 0 if the following is false (could also use an Else clause).
      If Previous ({Customers.CustomerID}) = {Customers.CustomerID} Then
         DateDiff ("d", Previous ({Orders.OrderDate}) ,{Orders.OrderDate})

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: DateDiff - First Record

    I do seem to have another problem now though. If you can't answer it I will just re-post under a new title. I just figured I would try it here first. It figures up my datediff for my records. Now I need to sum all the datediffs but when I try Sum({DateDiffFormula}) it says that "this field cannot be summarized", how can I get the total number of days in the group?

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: DateDiff - First Record

    It looks like you cannot use Sum(@formula) if the formula uses any of the PrintState functions. I could not create a RunningTotal field on that formula either.

    The only way I could generate a sum was by using a Global Variable.

    Create 3 additional formulas

    1) ResetTotalDays - place this formula in the Group Header and suppress it.
    WhilePrintingRecords;
    Global numberVar TotalDays:=0;
    TotalDays;

    2) SetTotalDays - place in the Details section also suppressed.
    WhilePrintingRecords;
    Global numberVar TotalDays;
    TotalDays:= TotalDays+{@DaysBetweenOrders};

    3)DisplayTotalDays - place in the Group Footer section.
    WhilePrintingRecords;
    Global numberVar TotalDays;
    TotalDays;

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: DateDiff - First Record

    Worked great bruce! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width