I need to calculate the date of the first day in a calendar week, e.g.
Week 1 in 2009 -> Mon, 29.12.2008 (!)
Week 44 in 2009 -> Mon, 26.10.2009
I have some ugly code for this, but I would prefer a nice C++ lib.
Any suggestions?
what about boost::gregorian::date with its algorithms ?
Related
ISO months start and end at various dates. Not necessarily 1st/31st.
How to use the EOMONTH DAX to get the last date of iso month?
DAX can handle everything that follows a certain logic.
In your example Jan is week 1-4, Feb is 5-8 and Mar is probably 9-12. But April has to be 13-17 (5 weeks), which ends on Apr-30? After all 52 weeks divided by 4 is 13 not 12. So some "months" have to have 5 weeks instead of 4.
I'm afraid EOMONTH is not familiar with your calendar preferences as long as they not defined in any ISO regulation.
However, as mentioned above, it's possible to find the first Monday and last Sunday within a month using DAX, but not with the EOMONTH function.
I would like to create a perpetual calendar in Power BI (Power Query) with the holidays. The thing is, I live in Canada and Easter and Victoria Day are quite complicated to integrate in this kind of calendar.
For the perpetual calendar, I used this technique (you can find the code on this website), which is very effective. It's "simply" a function to which you tell that you want a calendar from this date to this date and it generates everything you want and probably more. However, the calendar doesn't know which day is a holiday. So you need to have another request in Power BI with all the holidays for that period. Then you tell the calendar to use this holiday request as a reference point and then it will know which day is a holiday.
This is where it gets very complicated. Some holidays are based on a specific date, some are based on a specific day and others vary a lot.
Based on a specific date : New Year's Day, January 1st ; Saint-Jean-Baptiste Day, June 24 ; Canada Day, July 1st ; National Day for Thruth and Reconciliation, September 30 ; Remembrance Day, November 11 ; Christmas Day, December 25 ; Boxing Day, December 26.
Based on a specific day : Labour day, first Monday of September ; Thanksgiving, second Monday of October.
Those that vary a lot : Good Friday, Friday before Easter ; Easter, first Sunday after the Paschal full moon ; Easter Monday, Monday after Easter ; Victoria Day, last Monday preceding May 25.
Finally, some holidays happen only once to commemorate specific events like the death of the Queen on September 19, 2022.
I would like to create a function to which I indicate that I want all holiday for a specific period and it generates everything by itself. Then I could tell my calendar function to use it as a reference to know which day is a holiday. It would also be great if I could add some new holiday here and there for specific events like the death of the Queen.
For holidays like New Year's Day, Canada Day and Thanksgiving, I used this tutorial, but it's incomplete. Also, it doesn’t explain how to integrate holidays like Easter or Victoria Day. So this is where I'm stuck right now.
I know it's complicated but if someone can help me, it would be awesome.
As Jon suggest, I think it will be a lot easier to use the canada-holidays.ca/api.
I'm having issues with this API though. If I want multiple years in a single request, I need to create a request for each year and then append them. I'm trying to find a way to only have a single request but I'm struggling with this as well.
As soon as I find a solution, I'll update this post.
EDIT
Someone help me with that on another post. You can find the solution right here!
I have managed to create another Hierarchy with year, month, day, and hour
but it does not work as I wanted
I created a table that has Days and hours and linked it to my table.
The "Go to the next level in the hierarchy" does not accumulate for that period
i mean in that case I should get January for all years but what I get is Jan 2017, Jan 2018, Jan 2019
it act exactly as "Expand all down" button
Is there any way I can simply add Hours to the Date hierarchy
Create a calculated "hour" column in the fact table, for example,
Hour = TIME( HOUR( [AVHandoverHour] ), 0, 0 )
And, add it to the visual axis.
How can I subtract a length-of-time from a boost gregorian date?
Let's say I construct a date as follows:
boost::gregorian::date Today(2012, 02, 13);
I would like to do Today - N weeks from Today - N Months from Today - N years and get a valid date after subtraction.
To get one week prior to today, just use
today - weeks(1)
To get the previous month you can do.
today - months(1)
But the same day of the previous month might not exist. For example, if today were March 30, there is no February 30th, but boost will "snap" to the end of the month in the case of March 30 - months(1) and give you Feb 28th (or Feb 29th in leap years).
The classes are all part of the boost::gregorian namespace.
I think you want the Date Iterators, described here: http://www.boost.org/doc/html/date_time/gregorian.html#date_time.gregorian.date_iterators
I am currently trying to solve some problems from the USACO training website in preparation for an unrelated C++ programming competition.
However, I am stuck on this problem:
Does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.
The number N is provided in an input file and the output is to be a file with seven numbers in it, each representing the number of 13th's falling on a particular day of the week.
I was wondering how you guys would approach this problem. I am not looking for code or anything since that would just defeat the purpose of me doing this, instead just a starting point or an algorithm would be helpful.
So far the only thing I could think of is using the Doomsday Algorithm, however I am unsure about how I would implement that in code.
Any help would be greatly appreciated.
As Denny says, N is so small that you can easily iterate through the months using a table of days-in-a-month and a simple is-a-leap-year predicate to handle February. Just find out what day the 13th of Jan was in 1900 and then add up the elapsed days until 13th Feb, then 13th March etc.. Use a % operator to wrap the # of elapsed days back into a day-of-week value.
N is less than 400? well you just need to go over 365.25*400=146100 days at max. sounds easy to enumerate all of them, convert dates into year/month/date (with your favorite date conversion routine), testing for day of week is trivial.
I would precalculate the table though.
Just use brute force. Like this pseudocode example:
from datetime import date
day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']
counts = [0] * 7
for year in range(1900, 2300):
for month in range(1, 13):
counts[date(year, month, 13).weekday()] += 1
for day, count in zip(day_names, counts):
print('%s: %d' % (day, count))
The "hard" part is calculating the day of the week a date falls on. In C(++), you can use the mktime and localtime library functions if you know that your platform handles a large enough date range.