You might have wondered that there is no DAX Quarter function in Power BI. Here in this short blog post, I explain some easy methods for calculating quarter. If you like to learn more about Power BI, read Power BI book from Rookie to Rock Star.
Sample Data
To start this example, you can open a new Power BI file, create a new calculated table, with below expression that gives you a date table with one column:
Date = CALENDAR( DATE(2019,1,1), DATE(2020,1,1) )
If you like to know more about calendar() function in DAX, read this article.
Using the Format Function
One easy way to calculate the quarter from a date field is the Format function with a format string of “q”. this will give you the Quarter number but as a text format.
FORMAT('Date'[Date],"q")
the output is:
You can then use a function such as INT if you want to achieve the number:
INT(FORMAT('Date'[Date],"q"))
the output is:
Or if you want to build a text value for it, such as QTR 1, you can do this:
FORMAT('Date'[Date],"\QTR q")
The \ in the format string above is an escape character, that means this Q after the \ will be an actual Q letter rather than the q that will be replaced by the quarter number.
the output is:
Calculating it from Month
Another way to calculate the quarter is to derive it from the month calculation. mainly a divide by three and the round it. Here are some options:
Ceiling
You can use the Ceiling function to the get the round-up number of the divide by three from the month value as below;
CEILING( MONTH('Date'[Date])/3 ,1)
the output is:
RoundUp
In this scenario, you can also use the roundup function to achieve the same thing with the same approach:
ROUNDUP( MONTH('Date'[Date])/3 ,0)
the output is the same:
Round
It is not my preference in this scenario to use Round, but if you want just to see another method to calculate it, here it is:
ROUND( (MONTH('Date'[Date])+1)/3 ,0)
For this you need one extra step because round might end up with zero for some values, you have to first add one to the month value, and then do the divide and rounding. The output is the same.
Other Quater Related Functions
There are other functions that can be helpful for working with Quarters, such as StartOfQuarter
STARTOFQUARTER('Date'[Date])
and EndOfQuarter:
ENDOFQUARTER('Date'[Date])
Here is the output:
There are also two other functions: PreviousQuarter and the NextQuarter. However, these two functions don’t return one value, they return a range of dates, which can be useful for filtering other functions.
Summary
This was a very quick blog post about some of the ways you can calculate the quarter in DAX for Power BI. Most of the methods are the same anyways. If you like to learn more about Time Intelligence functions in DAX read more here.
I realized there’s no quarter() function last friday 🙂
thanks Reza
Great roundup of functions to compute quarter. Interesting that DAX does it though formats.
Thanks Cody. Yes, Format is very useful function 🙂