Determining Quarter?

nvermanverma Member Posts: 396
Hello yall,

I have been asked to create this report and I am not sure how to get it started.

I have to write some code that will figure out what Quarter we are in based on the current date.

Our first quarter starts from April1, 2012.

I was thinking maybe it can be done using "case" statements.

Any suggestions???

Answers

  • nvermanverma Member Posts: 396
    I think this should be it:
    Date := Today;
    Month := DATE2DMY(Date,2);
    CASE Month OF
      1..3:
        Message('You are in Quarter 4');
      4..6:
        Message('You are in Quarter 1');
      7..9:
        Message('You are in Quarter 2');
     ELSE
        Message('You are in Quarter 3');
    END;
    
  • DenSterDenSter Member Posts: 8,305
    From object designer, click the 'Form' button, hit F3, which should start the new form wizard. Under Table, type in 'Date', select 'tabular' and hit OK. Move all fields over and hit 'Finish'. Run the form, and see what shows up.

    This is the virtual table called 'Date', and it knows about quarters. Instead of programming your own date processing code, familiarize yourself with this virtual table.
  • nvermanverma Member Posts: 396
    I have two questions:-

    I have been able to detect which quarter we are in depending on the current date. Now I have created two new variables:- QuarterBegin (date) and QuarterEnd (date).

    So if the current date for instance is May 23. I want it to determine all the 'Job No.' that fall in that quarter (which would be QuarterBegin = 04/01 and QuarterEnd = 06/31).

    How can I do this without providing an explicit year (or does that even matter whether I enter a year or not). I want to make a my QuarterBegin and QuarterEnd generic. So that report would work in any year.

    Any suggestions?

    Also, right now it gives me an error that the type of conversion is not possible (date := decimal) which i dont understand since the QuarterBegin is off type date.
  • DenSterDenSter Member Posts: 8,305
    I'm not sure what you are referring to when you ask about not specifying the year. A date always includes a year. A date without a year is not a date.

    If you know the quarter, you also know the quarter's start and end dates. Use those dates to set filters on the other table, so you can figure out the jobs between those dates.

    About that error, that's a type conversion error. Somewhere you are trying to put a decimal value into a date type variable or vice versa (hard to tell without the actual error message). You need to make sure that you take care of aligning data types.

    That's what programming is all about, you have to figure out the puzzle.
  • nvermanverma Member Posts: 396
    This is the Code, I have so far. If I get rid of line QuarterBegin and QuarterEnd. I dont get the error no more.

    This is the exact Error: - "The type of conversion is not possible because 1 of the operators contains an invalid type. Date := Decimal".

    CurrentDate := TODAY;
    Month := DATE2DMY(CurrentDate,2);
    CASE Month OF
      1..3:
        MESSAGE('You are in Quarter 4');
      4..6:
        QuarterBegin := 01/04/12;
        QuarterEnd := 30/06/12;
        MESSAGE('You are in Quarter 1');
      7..9:
        MESSAGE('You are in Quarter 2');
      ELSE
        MESSAGE('You are in Quarter 3');
    END;
    

    I am specifying QuarterBegin and End year to be 2012. Wouldnt that create a problem next year since my QuarterBegin and end would be in year 2013. Thats why I wanted to how to bypass this problem and somehow create a generic date, so year wont create an issue, no matter what year we are in.
  • kinekine Member Posts: 12,562
    01/04/12 is not date, it is 01 / 04 / 12 = 0,0208333333333333

    if you want to enter date, enter it as 040112D (or is it 010412D??? :whistle: )
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • nvermanverma Member Posts: 396
    Thank you so much. That worked like a charm.

    Just one last question.

    How can I Make QuarterBegin and QuarterEnd generic.
        QuarterBegin := 010412D;
        QuarterEnd := 300612D;
    

    Right now, it would only work, if the year is 2012. But I want it to work for any year.

    Any suggestions?
  • kinekine Member Posts: 12,562
    Like DMY2DATE(1,4,DATE2DMY(TODAY,3))?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • nvermanverma Member Posts: 396
    That makes so much sense. Thanks!!!
Sign In or Register to comment.