I am having problems with finding the number of sundays between two dates. I tried to use the intck () function to count number of weeks between two dates and since week in SAS starts on a Sunday I thought that was the right way. However now I am not so sure.
My dates go from 2007QI to 2013QII. I want to know the number of sundays in each quarter, so I have to look at the number of sundays between current quarter and previous (that is this date and lag (date)). When I use the intck() function I get 13 as the number of weeks in each quarter (except for one where I get 14).. i am not sure whether it is correct to assume that there are 13 sundays in each quarter?
intck is the correct function, but I would use weekday as the time interval. Ordinarily this assumes a 5 day working week, with Saturday and Sunday as weekends, but you can tweak it to use a 1 day working week, with Monday - Saturday as weekends (i.e. just count the number of Sundays).
data _null_;
format a b date9.;
a='01 nov 2016'd;
b='18 nov 2016'd;
Sundays = intck('weekday234567w',a,b);
put _all_;
run;
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.
Power Bi gives you the option to look at data by Year, Quarter, Month, and Day. I want the ability to look at data by 7 day periods that start on a specific date (not necessarily Monday or Sunday). How is the best way to accomplish this? I am guessing it will be with a measure but I can't quite figure out what the measure should look like?
Here I know I can assign a day of the week to each row and then use Week Day on my date axis. My problem is I need to be able to put "Tuesday" in the second parameter instead of either 1. Sunday or 2. Monday.
Week Number = WEEKNUM(Sheet1[Date],2)
Thank you in advance!
IIUC, the following might work:
Week number = WEEKNUM(DATEADD([Date],1,DAY),2)
I have a line graph in PowerBI and in my date dimension I have the Week Number for every date (note that this is a custom week number with the week starting on Friday).
Whenever I put it on a the x-axis, PowerBI groups all the weeks together, regarless of year... so Week 1 of year 2015 will be grouped together with Week 1 of 2016...
I think to myself: "Ok, no problem, I'll just add the Year after every week number so I'll have 1-2016, 2-2016, and so on."
Well PowerBI sees this concatenation as a string value so when I put that on the graph, it goes
1-2016, 1-2017, 2-2016, 2-2017, 3-2016, 4-2016, and so on....
I've tried sorting the new column by the old week number column, but it does the same thing. Any suggestions on how to accomplish this?
You're on the right track. I recommend a separate (hidden) sort column that sorts alphabetically (i.e. year first, then 2 digit week). In other words, 1-2016 = 201601.
This way, all the weeks for 2016 sort before the weeks for 2017, and the weeks sort in the right order too. (A 1 digit week would mean 20161 will be followed by 201610, which you don't want either.)
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.
I would like to determine the next date that has a day of the week value equal to something I specify.
For example, today is 6/21/2010 and the day of week value is 1 b/c today is a Monday. How do I find the next date with let's say a day of week value of 3.
I would like to consider all cases that come close to the end of something including month and year.
Is there any easy way to do this or do I have to manually check to see if it's close to the end of the month or year and make adjustments accordingly? I feel there is an easily and better solution than doing that.
You can stuff your values into a struct tm, and then normalize them with a call to mktime. This will ignore the day of week and day of year on input, and normalize the other values. For example, you can feed it an input of February 31st, and it'll convert that to March 3rd (or March 2nd for a leap year).
To use this, the normal sequence would be to call time to get the current time as a time_t. Then convert that to a struct tm with a call to localtime. Adjust the time in the struct tm as needed (e.g., if it says today is Tuesday, and you want Friday, add three to the day of the month). Then call mktime to normalize the values, in case you've (for example) overflowed from one week/month/year to the next.