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
Midway
Helper I
Helper I

12 Previous Months Sum for each month in the past from current month

Hi there,

 

Been trying to get the DAX for this but no luck so far. Need some help on the following.

 

So for each month, I'm trying to Sum the previous 12 months of data. Let's say for September 2019, I need to go back 12 months and sum the number of sales (Oct/2018 to Sept/2019). The same goes for the rest of the months in the past, for Aug/2019 should be  Sept/2018 to Aug/2019 and so on for the rest of the months in the past.

 

Thanks for your help!

 

Midway

 

 

  

1 ACCEPTED SOLUTION

I added some months to make it easier for me to debug the measure

past_12_months.pnghere is the measure:

Prev 12 Months Sales =
VAR start_date = max('Sales'[Sales Date])
var start_date_for_sales_month = datevalue(month(start_date) & "/1/" & year(start_date))
var end_date_for_month = edate(start_date_for_sales_month,-12)
var min_date = calculate(min(Sales[Sales Date]) ,All())
var min_date_month = datevalue(month(min_date) & "/1/" & year(min_date))
var show_amount = min_date_month <= end_date_for_month
var amount = calculate(sum(sales[sales amount]),sales[sales date] <= start_date_for_sales_month, sales[sales date] <= start_date)
return if(show_amount,amount)
There might be easier ways. 
I started by getting the first day of the month for the row.
Then i used edate to get a date 12 months in the past
I calculated the minimum date in the data set
then i checked to see if the month 12 months in the past was greater than the minimum date in the dataset and less than the month in the current row
if it was, then i used calculate to add up the sales for the month.
DAX has some great functions to simplify calculating periods, and dates in periods, but i could not figure out how to use them when the month was represented by "any date in the month"
Hope this helps
Ken Tyler




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


View solution in original post

7 REPLIES 7
kentyler
Solution Sage
Solution Sage

here's some sample code i found online

Sales12M := CALCULATE (
    [Sales],
    DATESBETWEEN (
        Calendar[FullDate],
        NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( Calendar[FullDate] ) ) ),
        LASTDATE ( Calendar[FullDate] )
    )
)




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Thanks for the reply there, but don't think this is giving me what I need.  What would be the Calendar FullDate? 

I do have a calendar table tho. 

That would be the name of the field in your calendar table that holds the date.





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


That's what I thought, but still not showing the right numbers. This is suming the last 12 months from current month (October 2019 to October 2018) but this is just repeating the same numbers for the other dates. Like for example Sep-2019 should sum from Sept-2019 to Sept 2018 and so forth.

Can you make a sample pofwer bi file available ?





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


This is an example of what I'm trying to accomplish. The previous 12 months sum column should sum from current month 12 months in the pat. For the previous month, it should sum from Septeber 2019 to past 12 months and so on for the rest of the months in the past. Let me know if I'm making any sense here please.

 

Sale DateSalesPrevious 12 months sum
Jun-1820 
Jul-1818 
Aug-1830 
Sep-1820 
Oct-1822 
Nov-1840 
Dec-1860 
Jan-1921 
Feb-1919 
Mar-1920 
Apr-1956 
May-1920346
Jun-1978404
Jul-1912398
Aug-1944412
Sep-1911403
Oct-1960441

I added some months to make it easier for me to debug the measure

past_12_months.pnghere is the measure:

Prev 12 Months Sales =
VAR start_date = max('Sales'[Sales Date])
var start_date_for_sales_month = datevalue(month(start_date) & "/1/" & year(start_date))
var end_date_for_month = edate(start_date_for_sales_month,-12)
var min_date = calculate(min(Sales[Sales Date]) ,All())
var min_date_month = datevalue(month(min_date) & "/1/" & year(min_date))
var show_amount = min_date_month <= end_date_for_month
var amount = calculate(sum(sales[sales amount]),sales[sales date] <= start_date_for_sales_month, sales[sales date] <= start_date)
return if(show_amount,amount)
There might be easier ways. 
I started by getting the first day of the month for the row.
Then i used edate to get a date 12 months in the past
I calculated the minimum date in the data set
then i checked to see if the month 12 months in the past was greater than the minimum date in the dataset and less than the month in the current row
if it was, then i used calculate to add up the sales for the month.
DAX has some great functions to simplify calculating periods, and dates in periods, but i could not figure out how to use them when the month was represented by "any date in the month"
Hope this helps
Ken Tyler




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


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.

Top Solution Authors