Wednesday, August 5, 2020

How to find and get quarter / quarter name (month / month name) from a given date in Power BI

Let's imagine that we have General ledger attributes table that contains Date field

You would like to extract Years, Quarters, Months values from the Date field. For this purpose you can create new columns (Month, Quarter and Year) and use the following DAX expressions:

Month = MONTH('General ledger attributes'[Date])
Quarter = QUARTER('General ledger attributes'[Date])
Year = Year('General ledger attributes'[Date])

The issue you might experience is that Month column will return numeric values instead of text (like 1,2,3,4,....12). Quarter column will return numeric values also (like 1,2,3,4).

What if you want to have text values. For Quarters, for instance, to have Q1, Q2, Q3 and Q4. For Months, to have January, February etc.

You may try to use the following expression:
FORMAT('General ledger attributes'[Date], "MMM")

If it doesn't work as it was in my case, because of the following error message
Function 'FORMAT' is not allowed as part of calculated column DAX expressions on DirectQuery models.
you can create additional columns and use the following expressions:

MonthName =
SWITCH('General ledger attributes'[Month], 1,"January", 2,"February",3,"March",4,"April",
5,"May",6,"June",7,"Jul",8,"August",9,"September",10,"October",11,"November",12,"December")

QuarterName = SWITCH('General ledger attributes'[Quarter], 1,"Q1", 02,"Q2",3,"Q3",4,"Q4")

The structure of the columns will be the following:

Now you can build visualizations using new created columns.

No comments:

Post a Comment