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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Count of the past five years

Hello!

 

First, I have a calendar table.

 

I would like something like the below, but that I can then filter by date (if I filter by January 2020, it will give me the count for January, and the sum of all the Januaries of the past 5 years, including this one). Then potentially I can do a 5 year average for that date period.

 

This YearPast five years
  

 

Thanks!

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

Calendar:

Calendar = CALENDARAUTO()

 a2.png

 

You may create measures as below.

Sum = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)
Count = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    COUNTROWS(
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)
Avg = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    CALCULATE(
        AVERAGE('Table'[Value]),
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)

 

Result:

a3.png

 

Best Regards

Allan

 

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

View solution in original post

2 REPLIES 2
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

Calendar:

Calendar = CALENDARAUTO()

 a2.png

 

You may create measures as below.

Sum = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)
Count = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    COUNTROWS(
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)
Avg = 
var y = SELECTEDVALUE('Calendar'[Year])
var m = SELECTEDVALUE('Calendar'[Month])
return
IF(
    HASONEVALUE('Calendar'[YearMonth]),
    CALCULATE(
        AVERAGE('Table'[Value]),
        FILTER(
            ALL('Table'),
            YEAR([Date])>=y-5&&
            YEAR([Date])<y&&
            MONTH([Date])=m
        )
    )
)

 

Result:

a3.png

 

Best Regards

Allan

 

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

amitchandak
Super User
Super User

@Anonymous , One of the way with Date table and time intelligence is

 

last 5 MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))+ CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
+ CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-24,MONTH))) + CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-36,MONTH)))
+ CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-48,MONTH)))+ CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-60,MONTH)))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.