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
ashamsuzzoha
Helper III
Helper III

Same Period Last N Years

Is there a function that resembles SAMEPERIODLASTYEAR but that can be expanded to more than one year back? Like the average of a monthly value for the same month the last five years?

 

Thanks,

1 ACCEPTED SOLUTION

Hi @ashamsuzzoha ,

 

check this out.

PBIX

 

Regards,

Marcus

Dortmund - Germany
If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


View solution in original post

14 REPLIES 14
amitchandak
Super User
Super User

datemtd, datesqtd and datesytd , you can use . By changing the number in dateadd, you can go in past as much as you want

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last MTD Sales 5 year back = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-5,YEAR)))

last MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date Filer],-1,MONTH))))
last year MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date Filer],-12,MONTH))))

MTD (Year End) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFYEAR('Date'[Date Filer])))
MTD (Last Year End) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFYEAR(dateadd('Date'[Date Filer],-12,MONTH),"8/31")))


QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(('Date'[Date Filer])))

Last QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(dateadd('Date'[Date Filer],-1,QUARTER)))
Next QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(dateadd('Date'[Date Filer],1,QUARTER)))

Last year same QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(dateadd('Date'[Date Filer],-1,Year)))

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(('Date'[Date Filer])))

Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date Filer],-1,Year)))

Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-3,MONTH))  

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...
https://community.powerbi.com/t5/Community-Blog/Power-BI-Working-with-Non-Standard-Time-Periods/ba-p...
https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601

Hi @ashamsuzzoha ,

 

look at this.

https://radacad.com/dateadd-vs-parallelperiod-vs-sameperiodlastyear-dax-time-intelligence-question

Regards,

Marcus

Dortmund - Germany
If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Thanks for the reply, @mwegener.

I think I asked my question wrong. I would like the average of the previous 5 years, not just the value from 5 years ago. Is there a function that can do that?

Hi @ashamsuzzoha ,

is your problem solved?

If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Hi @mwegener ,

 

My problem is still not solved. I tried the code you gave to try out and this is what I got: 

Date	Number of Leaks	Number of Leaks Previous Year	Average Number of Leak Previous 5 Years
1/1/2010 0:00	118		118
2/1/2010 0:00	56		56
3/1/2010 0:00	95		95
4/1/2010 0:00	123		123
5/1/2010 0:00	257		257
6/1/2010 0:00	185		185
7/1/2010 0:00	64		64
8/1/2010 0:00	37		37
9/1/2010 0:00	26		26
10/1/2010 0:00	0		0
11/1/2010 0:00	43		43
12/1/2010 0:00	14		14
1/1/2011 0:00	52	118	52
2/1/2011 0:00	102	56	102
3/1/2011 0:00	21	95	21
4/1/2011 0:00	71	123	71
5/1/2011 0:00	126	257	126
6/1/2011 0:00	143	185	143
7/1/2011 0:00	38	64	38
8/1/2011 0:00	104	37	104
9/1/2011 0:00	176	26	176
10/1/2011 0:00	286	0	286
11/1/2011 0:00	155	43	155
12/1/2011 0:00	246	14	246
1/1/2012 0:00	176	52	176
2/1/2012 0:00	194	102	194
3/1/2012 0:00	114	21	114
4/1/2012 0:00	86	71	86
5/1/2012 0:00	122	126	122
6/1/2012 0:00	104	143	104
7/1/2012 0:00	116	38	116
8/1/2012 0:00	68	104	68
9/1/2012 0:00	51	176	51
10/1/2012 0:00	232	286	232
11/1/2012 0:00	191	155	191
12/1/2012 0:00	162	246	162
1/1/2013 0:00	120	176	120
2/1/2013 0:00	60	194	60
3/1/2013 0:00	59	114	59
4/1/2013 0:00	50	86	50
5/1/2013 0:00	168	122	168
6/1/2013 0:00	241	104	241
7/1/2013 0:00	511	116	511
8/1/2013 0:00	225	68	225
9/1/2013 0:00	110	51	110
10/1/2013 0:00	111	232	111
11/1/2013 0:00	116	191	116
12/1/2013 0:00	130	162	130
1/1/2014 0:00	445	120	445
2/1/2014 0:00	335	60	335
3/1/2014 0:00	191	59	191
4/1/2014 0:00	184	50	184
5/1/2014 0:00	91	168	91
6/1/2014 0:00	27	241	27
7/1/2014 0:00	89	511	89
8/1/2014 0:00	105	225	105
9/1/2014 0:00	80	110	80
10/1/2014 0:00	267	111	267
11/1/2014 0:00	135	116	135
12/1/2014 0:00	0	130	0
1/1/2015 0:00	148	445	148
2/1/2015 0:00	125	335	125
3/1/2015 0:00	69	191	69
4/1/2015 0:00	100	184	100
5/1/2015 0:00	175	91	175
6/1/2015 0:00	183	27	183
7/1/2015 0:00	162	89	162
8/1/2015 0:00	173	105	173
9/1/2015 0:00	46	80	46
10/1/2015 0:00	178	267	178
11/1/2015 0:00	127	135	127
12/1/2015 0:00	2	0	2
1/1/2016 0:00	135	148	
2/1/2016 0:00	162	125	
3/1/2016 0:00	111	69	
4/1/2016 0:00	64	100	
5/1/2016 0:00	114	175	
6/1/2016 0:00	129	183	
7/1/2016 0:00	117	162	
8/1/2016 0:00	102	173	
9/1/2016 0:00	90	46	
10/1/2016 0:00	133	178	
11/1/2016 0:00	36	127	
12/1/2016 0:00	28	2	
1/1/2017 0:00	144	135	
2/1/2017 0:00	76	162	
3/1/2017 0:00	41	111	
4/1/2017 0:00	41	64	
5/1/2017 0:00	35	114	
6/1/2017 0:00	28	129	
7/1/2017 0:00	61	117	
8/1/2017 0:00	43	102	
9/1/2017 0:00	68	90	
10/1/2017 0:00	84	133	
11/1/2017 0:00	106	36	
12/1/2017 0:00	1	28	
1/1/2018 0:00	84	144	
2/1/2018 0:00	68	76	
3/1/2018 0:00	68	41	
4/1/2018 0:00	50	41	
5/1/2018 0:00	29	35	
6/1/2018 0:00	69	28	
7/1/2018 0:00	91	61	
8/1/2018 0:00	126	43	
9/1/2018 0:00	95	68	
10/1/2018 0:00	31	84	
11/1/2018 0:00	0	106	
12/1/2018 0:00	0	1	
1/1/2019 0:00	0	84	
2/1/2019 0:00	11	68	
3/1/2019 0:00	46	68	
4/1/2019 0:00	40	50	
5/1/2019 0:00	11	29	
6/1/2019 0:00	1	69	
7/1/2019 0:00	6	91	
8/1/2019 0:00	8	126	

Hi @ashamsuzzoha ,

 

You may download my PBIX file from here.
Hope this helps.

 

Regards,

Marcus

Dortmund - Germany
If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


@v-lid-msft  @mwegener  @amitchandak Thanks for the help. That is not exactly what I'm looking for.  I am wanting the monthly average of the previous 5 years. Since the data starts in 2010, obviously this measure cannot start until 2015. So for Jan 2015, I want the average of Jan 2010, Jan 2011, Jan 2012, Jan 2013 and Jan 2014. Here is a sample output of what I want.

 

DateNumber of LeaksNumber of Leaks Previous YearAverage Number of Leak Previous 5 Years
1/1/2010 0:00118  
2/1/2010 0:0056  
3/1/2010 0:0095  
4/1/2010 0:00123  
5/1/2010 0:00257  
6/1/2010 0:00185  
7/1/2010 0:0064  
8/1/2010 0:0037  
9/1/2010 0:0026  
10/1/2010 0:000  
11/1/2010 0:0043  
12/1/2010 0:0014  
1/1/2011 0:0052118 
2/1/2011 0:0010256 
3/1/2011 0:002195 
4/1/2011 0:0071123 
5/1/2011 0:00126257 
6/1/2011 0:00143185 
7/1/2011 0:003864 
8/1/2011 0:0010437 
9/1/2011 0:0017626 
10/1/2011 0:002860 
11/1/2011 0:0015543 
12/1/2011 0:0024614 
1/1/2012 0:0017652 
2/1/2012 0:00194102 
3/1/2012 0:0011421 
4/1/2012 0:008671 
5/1/2012 0:00122126 
6/1/2012 0:00104143 
7/1/2012 0:0011638 
8/1/2012 0:0068104 
9/1/2012 0:0051176 
10/1/2012 0:00232286 
11/1/2012 0:00191155 
12/1/2012 0:00162246 
1/1/2013 0:00120176 
2/1/2013 0:0060194 
3/1/2013 0:0059114 
4/1/2013 0:005086 
5/1/2013 0:00168122 
6/1/2013 0:00241104 
7/1/2013 0:00511116 
8/1/2013 0:0022568 
9/1/2013 0:0011051 
10/1/2013 0:00111232 
11/1/2013 0:00116191 
12/1/2013 0:00130162 
1/1/2014 0:00445120 
2/1/2014 0:0033560 
3/1/2014 0:0019159 
4/1/2014 0:0018450 
5/1/2014 0:0091168 
6/1/2014 0:0027241 
7/1/2014 0:0089511 
8/1/2014 0:00105225 
9/1/2014 0:0080110 
10/1/2014 0:00267111 
11/1/2014 0:00135116 
12/1/2014 0:000130 
1/1/2015 0:00148445182
2/1/2015 0:00125335149
3/1/2015 0:006919196
4/1/2015 0:00100184103
5/1/2015 0:0017591153
6/1/2015 0:0018327140
7/1/2015 0:0016289164
8/1/2015 0:00173105108
9/1/2015 0:00468089
10/1/2015 0:00178267179
11/1/2015 0:00127135128
12/1/2015 0:0020110
1/1/2016 0:00135148188
2/1/2016 0:00162125163
3/1/2016 0:001116991
4/1/2016 0:006410098
5/1/2016 0:00114175136
6/1/2016 0:00129183140
7/1/2016 0:00117162183
8/1/2016 0:00102173135
9/1/2016 0:00904693
10/1/2016 0:00133178215
11/1/2016 0:0036127145
12/1/2016 0:00282108
1/1/2017 0:00144135205
2/1/2017 0:0076162175
3/1/2017 0:0041111109
4/1/2017 0:00416497
5/1/2017 0:0035114134
6/1/2017 0:0028129137
7/1/2017 0:0061117199
8/1/2017 0:0043102135
9/1/2017 0:00689075
10/1/2017 0:0084133184
11/1/2017 0:0010636121
12/1/2017 0:0012864
1/1/2018 0:0084144198
2/1/2018 0:006876152
3/1/2018 0:00684194
4/1/2018 0:00504188
5/1/2018 0:002935117
6/1/2018 0:006928122
7/1/2018 0:009161188
8/1/2018 0:0012643130
9/1/2018 0:00956879
10/1/2018 0:003184155
11/1/2018 0:000106104
12/1/2018 0:000132
1/1/2019 0:00084191
2/1/2019 0:001168153
3/1/2019 0:00466896
4/1/2019 0:00405088
5/1/2019 0:00112989
6/1/2019 0:0016987
7/1/2019 0:00691104
8/1/2019 0:008126110

 

Hi @ashamsuzzoha ,

 

How about the result after you follow the suggestions mentioned in my original post?Could you please provide more details about it If it doesn't meet your requirement?


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @ashamsuzzoha ,

 

check this out.

PBIX

 

Regards,

Marcus

Dortmund - Germany
If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Hi @mwegener , this appears to be what I need. Thanks!

 The quick one I can think of is

last 5 MTD Sales =  ( CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,Year)))
+CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-2,Year)))
+CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-3,Year)))
+CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-4,Year)))
+CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-5,Year))) 
)/5

 

Few Adjustments need to be done to take care of no of year. So instead of divide by 5 . We can take count distinct of years and divide.

Hi @ashamsuzzoha ,

 

We can create a measure using following dax to meet your requirement:

 

Number of Leank Previous N Years =
VAR LastNYearTable =
    GENERATESERIES ( 1, [Last N Year Value], 1 )
RETURN
    IF (
        COUNTROWS ( DATEADD ( 'Table'[Date], - [Last N Year Value], YEAR ) ) = 0,
        BLANK (),
        AVERAGEX (
            LastNYearTable,
            VAR y = [Value]
            VAR d =
                DATEADD ( 'Table'[Date], - y, YEAR )
            RETURN
                CALCULATE ( SUM ( 'Table'[Number of Leaks] ), ALLSELECTED ( 'Table' ), d )
        )
    )

 

3.jpg

 


Best regards,

 

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @ashamsuzzoha ,

 

After create a What-If Parameter, we can use a measure to meet your requirement:

 

8.jpg

 

 

 

Number of Leank Previous N Years = CALCULATE(SUM('Table'[Number of Leaks]),DATEADD(SAMEPERIODLASTYEAR('Table'[Date]),1-[Last N Year Value],YEAR))

 

 

 

9.jpg10.jpg

 


If it doesn't meet your requirement, Please show the exact expected result based on the Tables that you have shared.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @ashamsuzzoha 

 

try this

 

Measure =
CALCULATE (
    AVERAGE ( 'Table'[Value] ),
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
            && DATEADD ( 'Calendar'[Date], 5, YEAR ) >= MAX ( 'Calendar'[Date] )
    )
)

Regards,

Marcus

Dortmund - Germany
If I answered your question, please mark my post as solution, this will also help others.
Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


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.