Hitachi Vantara Pentaho Community Forums
Results 1 to 8 of 8

Thread: Count the weeks within the month? help.

  1. #1

    Question Count the weeks within the month? help.

    Hey everybody, okay??

    I need to count the weeks of the month within the year and I am having the following problem.

    I'm using the function:

    getDayNumber (start_date, "wm");

    The problem is that this counting function of 7 in 7 days.

    Example: In January 2010 until 1 week had only two days since the year began on Friday that it counts only up to 08 days to overturn the 2nd week.


    has some way to tell it correctly?

  2. #2
    Join Date
    Nov 2008
    Posts
    777

    Default

    If you are working with a database then MySQL has date functions that would work. If not, then my advice is to use java.util.Calendar.

    http://download.oracle.com/javase/6/.../Calendar.html
    pdi-ce-4.4.0-stable
    Java 1.7 (64 bit)
    MySQL 5.6 (64 bit)
    Windows 7 (64 bit)

  3. #3
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    I never heard of counting the weeks of the months within a year.
    Clearly tell us what you are trying to accomplish first, then we'll advise.

  4. #4

    Default

    ok.

    I must send to the database (PostgreSQL) the following information:

    Start Date
    Final Date
    Total days between two dates
    Total hours between two dates
    Weekdays (1, 2, 3, 4, 5, 6 or 7)
    Days of the year (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......, 365)
    Weeks of the month (1, 2, 3, 4 or 5)
    Weeks of the year (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......, 52)

    My only problem is I can not count the number of weeks within the month.

    Thank you.

  5. #5
    Join Date
    Sep 2009
    Posts
    810

    Default

    To get all of this you should probably use the java calendar, since it gives you most control over how weeks are calculated. Week calculations vary between cultures and organizations. Some people's weeks start on sunday, other people's weeks start on monday. Likewise it is a matter of cultural preference as to the weeks during the transition to a new year. There's ISO http://en.wikipedia.org/wiki/ISO_8601 and there's local preferences that vary. Same difficulties apply when counting weeks in months.

    For this reason I'd like to suggest you use the java.util.Calendar class

    If you need help working with it I suggest you check out this article. It explains how to use java classes from within kettle's JS step.
    http://type-exit.org/adventures-with...entaho-kettle/

    Here's a short sample on how it can be used in a JS step, supposing year, month, and day are coming in as fields:

    Code:
    var Calendar = Packages.java.util.Calendar;
    
    var quarter = Math.floor((month-1) / 3) +1;
    var quarter_label = 'Q'+quarter;
    
    var cal = Calendar.getInstance();
    cal.set(year, month-1, day);
    
    var days_in_year = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    var is_leap_year = (days_in_year == 366)?1:0;
    
    var week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
    Remember that you can (and probably should) specify a locale when calling Calendar.getInstance() to unambiguously specify the way weeks are handled.

    Cheers

    Slawo

  6. #6
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Pretty impressive Slawo! I wouldn't have known where to start.
    A new question comes to mind though... Santi, what kind of use does the number of weeks in a month have? What can you do with it?

  7. #7

    Default

    Ok guys, I'll try to adapt.


    MattCasters: In my case I need to provide bank information to a manager and so I work with virtually all types of information.

    Sorry for my English, I'm using Google translator lol



    Thanks

  8. #8
    Join Date
    Feb 2010
    Posts
    2

    Default

    Quote Originally Posted by MattCasters View Post
    Pretty impressive Slawo! I wouldn't have known where to start.
    A new question comes to mind though... Santi, what kind of use does the number of weeks in a month have? What can you do with it?
    Its quite usefull. For example, in retail industry, most customer will earn their salary in 1st week of the month, so comparing sales figure in monthly base or even weekly based, is not reflect the actual trend. The best way is compare 1st week on current month with several pass month at the same week of month number.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.