6
9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 1/6 How to calculate the number of days in a month in WebIntelligence March 12, 2012 — weldblog A quick method of determining the number of days in a month in WebIntelligence is through the use of the LastDayOfMonth function. For a specified date, the LastDayOfMonth will give you the last day in that month. =LastDayOfMonth(CurrentDate()) To extract the number of days, we want to use the first two digits of this result. The following formula will acheive that: =Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2) We’ve added two functions here. The first is FormatDate. LastDayOfMonth returns a value held as a date field. We ultimately want the first two digits of this field. However, using the Left() function means we have to have a string input, not a date input. We have to convert the input into a string format first. This requires two steps. The first is to convert the LastDayOfMonth value into a string. We do this using the FormatDate function: =FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”) We now have a string. We then extract the first two digits to give us the number of days in a month: =Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2) If we want to subsequently use this in a calculation, we need to further manipulate this value and convert it to a number. This is achieved using the ToNumber function: Business Objects Blog

How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

Embed Size (px)

DESCRIPTION

This report will give the calculate the number of days in webi

Citation preview

Page 1: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 1/6

How to calculate the number of days in a monthin WebIntelligence

March 12, 2012 — weldblogA quick method of determining the number of days in a month in WebIntelligence is through theuse of the LastDayOfMonth function.

For a specified date, the LastDayOfMonth will give you the last day in that month.

=LastDayOfMonth(CurrentDate())

To extract the number of days, we want to use the first two digits of this result. The followingformula will acheive that:

=Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2)

We’ve added two functions here. The first is FormatDate. LastDayOfMonth returns a value heldas a date field. We ultimately want the first two digits of this field. However, using the Left()function means we have to have a string input, not a date input. We have to convert the input intoa string format first.

This requires two steps.

The first is to convert the LastDayOfMonth value into a string. We do this using the FormatDatefunction:

=FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”)

We now have a string.

We then extract the first two digits to give us the number of days in a month:

=Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2)

If we want to subsequently use this in a calculation, we need to further manipulate this value andconvert it to a number. This is achieved using the ToNumber function:

Business Objects Blog

Page 2: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 2/6

number ToNumber(string number_string)

This takes a string value and converts it to a number.

In our case, we create the following formula:

=ToNumber(Left(FormatDate(LastDayOfMonth(CurrentDate());”dd/MM/yyyy”);2))

We now have the number of days for a particular month that we can use in our calculations.

Missing FirstDayOfMonth() function…

Sadly, there isn’t a function to determine the first day of the month. However, we have a methodof extracting the last day of the month using LastDayOfMonth() and using the formulasdescribed above, we can work out the first day:

=RelativeDate(LastDayOfMonth(CurrentDate());-ToNumber(Left(FormatDate(LastDayOfMonth(CurrentDate()) ;”dd/MM/yyyy”);2))+1)

Despite looking complex, we are subtracting the number of days in the month (plus one) from thelast day of the month. If we subtracted the total number of days in the month, we would end upwith the last day of the previous month! We don’t want this. So we add a 1 to give us the first day.

Also note that in order to subtract days, we have prefixed the ToNumber function with a minussymbol.

Update:

Hat tip to JB for pointing out that we can avoid the string conversion by using theDayNumberOfMonth function.

Hence, to calculate the last day of the month:

=DayNumberOfMonth(LastDayOfMonth(CurrentDate()))

And the first day of the month:

=DayNumberOfMonth(RelativeDate(CurrentDate(); 1-DayNumberOfMonth(CurrentDate())))

Page 3: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 3/6

Posted in BusinessObjects, Web Intelligence, WebIntelligence. Tags: FormatDate,LastDayOfMonth, Left, ToNumber, WebIntelligence. 13 Comments »

13 Responses to “How to calculate the number of days in amonth in WebIntelligence”

jerimiahbaldwin Says: March 21, 2012 at 4:01 pmCouldn’t you also use the following to avoid converting to a string and parsing:NumberOfDaysInMonthOfObject=DayNumberOfMonth(LastDayOfMonth([Action Time]))orNumberOfDaysInMonthOfCurrentDate=DayNumberOfMonth(LastDayOfMonth(CurrentDate()))

Similarly, FirstDayOfMonth=RelativeDate([Action Time]; 1-DayNumberOfMonth([Action Time]))or=RelativeDate([Action Time]; 1-DayNumberOfMonth(CurrentDate()))

Replyweldblog Says: March 26, 2012 at 1:01 pmJermimiah

Thanks for your comment, and you are right! I completely overlooked theDayNumberOfMonth function.

This will make things a bit simpler for our readers.

About these ads

Page 4: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 4/6

I will update the post.

Replyjerimiahbaldwin Says: March 26, 2012 at 2:23 pmThis is a terrific blog!

weldblog Says: March 27, 2012 at 8:27 amThank you for your kind words.

midyx Says: April 23, 2012 at 9:33 amnice posting. i have a question. how i can get LastDayOfMonth from period example: MAR2012

Replyweldblog Says: April 23, 2012 at 10:36 amHamidy

You can use:

=LastDayOfMonth(ToDate(“MAR 2012″;”MMM yyyy”))

The last part tells Web Intelligence the date format of the string you are passing in.

Replypavani Says: September 23, 2013 at 5:57 pmhi how can we get firstday of month given ”MMM yyyy” format i.e MAR 2013 similarto LastDayof Month shown in posts

weldblog Says: October 1, 2013 at 11:55 amThere isn’t a FirstDayOfMonth function that I am aware of.

However, I did write an article which described a workaround:

hep Says: July 19, 2012 at 3:54 pmThis is great, i’ve also been hunting around for a calculation of ‘work days’ in a month? Also,is there a way to calculate the number of workdays between any two dates?

Replyweldblog Says: July 20, 2012 at 11:59 am

Hep

Page 5: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 5/6

HepThanks for your feedback.

Working days in a month is slightly more tricky as you may need to take into account localholidays, etc. This varies from country to country.

They may be a way around this by using the MOD function. I will try to work an exampleand include here.

Replyhep Says: July 20, 2012 at 3:44 pmThat’s a good point. I think “working days, not accounting for holidays” is probably allthat could be calculated without using calendars. Not sure how to hook in customcalendars to the calculation anyway.

Shashi Says: August 2, 2012 at 5:11 amHello,

Really a very nice post

I have requirement, where in need to calculated turn time between 2 dates i.e. Start Date – EndDate

The catch is if End Date is Sunday than the Time calcuation should be till Saturday.Eg.Start Date 12-JUL-2012 00:09:25End Date 15-JUL-2012 22:08:35

As 15-JUL-2012 is Sunday, while calculation I should take till 14-JUL-2012 24:00

I have used below formula to minus one day if the End Date is Sunday

=If(DayNumberOfWeek([End Date])=7) Then RelativeDate([End Date];-1)

However the date/time which is get is 14-JUL-2012 22:08:35

How can make this to 14-JUL-2012 24:00. I want it in date format to use it in calculation

ReplyJoel de Souza Says: December 13, 2013 at 4:41 pmGreat Post!!! Helped me a lot. Thanks,

Reply

Page 6: How to Calculate the Number of Days in a Month in WebIntelligence _ Business Objects Blog

9/8/2014 How to calculate the number of days in a month in WebIntelligence | Business Objects Blog

http://bobjblog.wordpress.com/2012/03/12/how-to-calculate-the-number-of-days-in-a-month-in-webintelligence/ 6/6

« How to convert a string prompt into a date in WebIntelligence

Handling formula errors in Web Intelligence »

Create a free website or blog at WordPress.com. | The Garland Theme.

Follow

Follow “Business Objects Blog”

Powered by WordPress.com