I engaged in creating some date expressions yesterday, these expressions are very common needed. So I bring them here to share.
Suppose your Date Field name is FillDt ,
1- number of days in the quarter of date field.
For example
If the fillDt is "2002-03-04" the count will be 63
If the fillDt is "2002-06-20" the count will be 81
expression:
DATEDIFF("DAY",(DT_DATE)(((DT_WSTR,4)YEAR(fillDt)) + "-" + ((DT_WSTR,2)(((DATEPART("QUARTER",fillDt) – 1) * 3) + 1)) + "-1"),fillDt)
2-
number of days in the year of date field.
For example
If the fillDt is "2002-03-04" the count will be 63
If the fillDt is "2002-06-20" the count will be 171
expression:
DATEDIFF("DAY",(DT_DATE)(((DT_WSTR,4)YEAR(fillDt)) + "-1-1"),fillDt)
3- Number of months of the quarter of date
For example:
If the fillDt is "2002-03-31" the count will be 3
If the fillDt is "2002-06-30" the count will be 3
If the fillDt is "2002-07-13" the count will be 1
expression:
DATEDIFF("MONTH",(DT_DATE)(((DT_WSTR,4)YEAR(fillDt)) + "-" + ((DT_WSTR,2)(((DATEPART("QUARTER",fillDt) – 1) * 3) + 1)) + "-1"),fillDt)
4- quarter number of date
For example
If the fillDt is "2002-03-04" the quarter count will be 1
If the fillDt is "2002-06-20" the quarter count will be 2
If the fillDt is "2002-10-06" the quarter count will be 4
expression:
(DT_WSTR,1)(DATEPART("QUARTER",fillDt))
5- quarter start date
For example
If the fillDt is "2002-03-04" the "quarterStartDt" will be "2002-01-01"
If the fillDt is "2002-06-20" the "quarterStartDt" will be "2002-04-01"
expression:
(DT_DATE)(((DT_WSTR,4)YEAR(fillDt)) + "-" + ((DT_WSTR,2)(((DATEPART("QUARTER",fillDt) – 1) * 3) + 1)) + "-1")
6- quarter end date
For example
If the fillDt is "2002-03-04" the "quarterStopDt" will be "2002-03-31"
If the fillDt is "2002-06-20" the "quarterStopDt" will be "2002-06-30"
expression:
DATEADD("DAY",-1,DATEADD("MONTH",3,(DT_DATE)(((DT_WSTR,4)YEAR(fillDt)) + "-" + ((DT_WSTR,2)(((DATEPART("QUARTER",fillDt) – 1) * 3) + 1)) + "-1")))
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.