Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
_Dee_
New Member

How to group date into 12, 24, 36 months period selection?

Hello,
As per users request, we would like to show dates grouped into the following periods: last 12months, last 24months, last 36months etc and use this as a slicer replacing a standard date slicer where year+month had to be clicked to select the required period.

How to achieve this?

The following DAX is wrong, it only returns if e.g. "Last 24 Months" is chosen the dates between 12 and 24months and not 24months to date. Perhaps there is an easy solution?

Thank you

Date_group =
VAR TotalMonths = DATEDIFF('Table'[Date], TODAY(), MONTH)
RETURN
    SWITCH(
        TRUE(),
        TotalMonths <= 12, "Last 12 Months",
        TotalMonths <= 24, "Last 24 Months",
        TotalMonths <= 36, "Last 36 Months",
        "More than 36 Months"
)

 

2 REPLIES 2
_Dee_
New Member

Thank you @v-nuoc-msft , indeed the preference would have been to have a calculated column so that it could be used for slicer (easier for users). However that would mean that depending on the context e.g. 01/03/2024 could be part of last 12months or last 24months or last 36months. But I understand in this case this is not possible since each date needs to have a single allocation. 
Nonetheless, thank you for your help.

v-nuoc-msft
Community Support
Community Support

Hi @_Dee_ 

 

For your question, here is the method I provided:

 

Here's some dummy data

 

"Table"

 

vnuocmsft_0-1715130955411.png

 

If you are creating a measure, here are the changes I made to your code.

 

Date_group = 
var TotalMonths = DATEDIFF(SELECTEDVALUE('Table'[Date]), TODAY(), MONTH)
RETURN 
SWITCH(
    TRUE(),
    TotalMonths <= 12, "Last 12 Months",
    TotalMonths <= 24, "Last 24 Months",
    TotalMonths <= 36, "Last 36 Months",
    "More than 36 Months"
)

 

vnuocmsft_1-1715131037927.png

Please note that measure cannot be used in slicer. If you want to filter based on measure, please consider using it in Filters.

 

vnuocmsft_2-1715131303883.png

 

Alternatively, you can create a new column. This way, you can use that column in slicer.

 

vnuocmsft_3-1715131428767.png

 

Dategroup = 
var TotalMonths = DATEDIFF('Table'[Date], TODAY(), MONTH)
RETURN 
SWITCH(
    TRUE(),
    TotalMonths <= 12, "Last 12 Months",
    TotalMonths <= 24, "Last 24 Months",
    TotalMonths <= 36, "Last 36 Months",
    "More than 36 Months"
)

 

Regards,

Nono Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors