Power Query - How to create a perpetual calendar with holidays - powerbi

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!

Related

How to graph by 7 day periods in Power BI

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)

Cumulative/Rolling Sum Blank Dates

I'm currently working on inventory reconciliation, and I've struggling to fill all days of the calendar with the cumulative sum of product we're currently storing:
Inventory level ($). = CALCULATE(SUM(ledger[cost]),FILTER(ALL(DimDate[Date]),DimDate[Date]<=MAX(ledger[Document Date])))
As you guys might notice it has at least 90% of all dates filled, however if we look closely to the graph, we can appreaciate March 5th of 2016 is missing just due to the fact there was no transaction during that day resulting on a blank value. However I'm trying to accomplish retrieving the previous day balance for those days with no transactions. e.g: for March 5th should have $17,038,462.32 (balance for the previous day March 4th).
I'm trying to work on another clause into the measure with functions such as EARLIER or LASTDATE, however I haven't been succesful.
Any insight or solutions works well thank you. Have a nice day.
You are using a wrong date field in your measure. Change it to the field from the Date table:
Inventory level. =
CALCULATE(
SUM(ledger[cost]),
FILTER(ALL(DimDate[Date]),DimDate[Date]<=MAX(DimDate[Date])))

Date_Part on SQL Athena - "Function date_part not registered"

I'm trying to find latest Sunday (just for the sake of the example)
In the link below I have found an explanation how to use a date function called date_part to extract the day of week (for example, for current timestamp) and other parts of the date which might be interesting.
https://docs.aws.amazon.com/redshift/latest/dg/r_DATE_PART_function.html
The gist of it:
for example to find day of week
select date_part(dow, starttime) as dow
from event
But when I try to run something similar on Athena I receive "function date_part not registered".
So how can I find the latest Sunday? Or any other day of the week for that matter.
You can combine current_date with day_of_week to get the last Sunday:
presto:default> SELECT date_add('DAY', -day_of_week(current_date), current_date);
_col0
------------
2019-12-15
(1 row)
Note: when it's run on Sunday, this returns previous Sunday. You can easily adjust this as needed with if.
Tested on Presto 326.
As Athena is currently based on Presto .172, this is where you can learn about all available functions: https://trino.io/docs/0.172/functions.html
AWS Athena uses Presto so you need to use the Presto date/time functions. (Redshift, on the other hand, seems to be loosely based on PostgreSQL.)
To get the latest Sunday, you should use day_of_week() to find Sundays, and you can restrict your query to dates in the last week to limit it to the most recent Sunday.
To find the latest sunday you can use:
select DATE_ADD('day', -(extract(dow from (datecolumn + interval '1'day))-1),cast(day as date))
Since athena considers first day of week as monday and last day of week as sunday, but in your case we want to consider first day of week as sunday, So, I have used interval '1' day to make sunday shift and consider as the first day of week.

Calendar table with changing start date

I work in an educational department where the start date of our academic calendar changes every year. Some years it might be week 9 of the year, some years it might be week 10. There is no formula used to figure out this start date, it's based a combination of factors and is picked by humans.
I want to be able to compare calls received in week 1 of the academic calendar 2017 to calls received in week 1 of the academic calendar 2018 and 2019.
I am using PowerBI desktop and trying to create a calendar table that includes a column "academic calendar week" or similar.
Does anyone have any suggestions on how to offset start date of the calendar by a different number of weeks for each year?

Finding the day of the thirteenth of every month over a period of years

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.