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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
BunnyV
Frequent Visitor

DAX - Calculate sum of last 12 months from each month

Hello All,

 

I am new to powerbi and DAX.

 

I have a sample data set and it is as below table.

MONTHYEAR            Sales

Jan-181194
Feb-181103
Mar-181313
Apr-181289
May-181363
Jun-181317
Jul-181518
Aug-181560
Sep-181474
Oct-181420
Nov-181487
Dec-181526
Jan-191437
Feb-191459
Mar-191650
Apr-191470
May-191699
Jun-191421
Jul-191591
Aug-191549
Sep-191517
Oct-191275
Nov-191417
Dec-191428
Jan-20283

 

I would like to create a calculated column (NOT MEASURE) which gives me sum of last 12 months from each month.

The expecting output is something like,

MONTHYEAR |                       Sales |                        Sales of Last 12 Months from Each month

Jan-1811941194
Feb-1811032297
Mar-1813133610
Apr-1812894899
May-1813636262
Jun-1813177579
Jul-1815189097
Aug-18156010657
Sep-18147412131
Oct-18142013551
Nov-18148715038
Dec-18152616564
Jan-19143716807
Feb-19145917163
Mar-19165017500
Apr-19147017681
May-19169918017
Jun-19142118121
Jul-19159118194
Aug-19154918183
Sep-19151718226
Oct-19127518081
Nov-19141718011
Dec-19142817913
Jan-2028316759

 

Example:- 

For Jan-20 - Sum of (Feb-19 to Jan-20) = 16759

For Dec-19 - Sum of (Jan-19 to Dec-19) = 17913

 

 Can anyone please help me.

 

Thanks in advance.

BunnyV.

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

Hi @BunnyV ,

 

First you should create a calendar table using “calendar()” function which is as shown below:

 

111.png

 

Then create a calculated column using following dax expression:

YTDtotal = 
VAR a='sample data'[MONTHYEAR]
var lastyear= YEAR('sample data'[MONTHYEAR])-1
var lastmonth=IF(MONTH('sample data'[MONTHYEAR])>=12,MONTH('sample data'[MONTHYEAR])-12,MONTH('sample data'[MONTHYEAR])+1)
var lastday=DAY('sample data'[MONTHYEAR])
var last=DATE(lastyear,lastmonth,lastday)
Return
CALCULATE(SUM('sample data'[  Sales]),DATESBETWEEN('sample data'[MONTHYEAR],last,'sample data'[MONTHYEAR]))

Finally,you will see:

112.png

For the related .pbix file,pls click here.

 

Hope this would help.

 

Best Regards,

Kelly

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hello @v-kelly-msft ,

Thank you for this!

Just a quick one:

How to calculate the same if the Dates are the end of each month? I tried above DAX but not getting the ytd total as expected (works in the measure but when date filter is applied, the measure goes blank)

Sharing the sample data for reference:

 

Date

IDRevenueRevenue YTD (Column)Revenue_Ytd (Measure)

31/07/2018

324665495.425495.42 
31/08/2018324662815.072815.07 
30/09/2018324662878.242878.24 
31/10/2018324662795.922795.92 
30/11/2018324662818.152818.15 
31/12/2018324662664.912664.91 
31/01/2019324662445.282445.28 
28/02/2019324662262.112262.11 
31/03/2019324662067.892067.89 
30/04/2019324662314.282314.2828557.27
31/05/2019324662261.892261.8930819.16
30/06/2019324662342.662342.6633161.82
31/07/2019324662260.472260.4729926.87
31/08/2019324662281.322281.3229393.12
30/09/2019324662305.822305.8228820.7
31/10/2019324662260.632260.6328285.41
30/11/2019324662333.882333.8827801.14
31/12/2019324662290.432290.4327426.66
31/01/2020324662354.212354.2127335.59
28/02/2020324662370.572370.5727444.05
31/03/2020324662186.492186.4927562.65
30/04/2020324662143.562143.5627391.93
31/05/2020324661987.851987.8527117.89
30/06/2020324662092.422092.4226867.65
31/07/2020324662036.242036.2426643.42
31/08/2020324662070.132070.1326432.23
30/09/2020324662090.242090.2426216.65
31/10/2020324661992.351992.3525948.37
30/11/2020324662046.592046.5925661.08
31/12/2020324662018.552018.5525389.2
31/01/2021324662076.072076.0725111.06
28/02/2021324662089.052089.0524829.54
31/03/2021324661870.881870.8824513.93
1/04/2021324662077.572077.5724447.94

 

The DAX I used for measure is:

Revenue_Ytd (Measure) =
VAR LastNotBlankDate =
CALCULATE (
LASTDATE ( 'Client Details'[Date] ),
FILTER (
ALLSELECTED ('Client Details'),
LASTNONBLANK ( 'Client Details'[Date], 'Client Details'[Revenue.1] )))
VAR MinDate =
CALCULATE ( FIRSTDATE ( 'Client Details'[Date] ), ALLSELECTED ( 'Client Details' ) )
VAR Revenue_of_last_year =
IF (
MAX ( 'Client Details'[Date] ) <= LastNotBlankDate,
CALCULATE (
SUM ( 'Client Details'[Revenue.1] ),
DATESINPERIOD ('Client Details'[Date], LASTDATE ('Client Details'[Date]), -1, YEAR )
),
CALCULATE (
SUM ( 'Client Details'[Revenue.1] ),
DATESINPERIOD ('Client Details'[Date], LastNotBlankDate, -1, YEAR )
),
VAR CurrentRowDate =
MAX ( 'Client Details'[Date])
VAR Revenue_of_last_year_2 =
IF (
DATEDIFF ( MinDate, CurrentRowDate, MONTH ) + 1 < 12,
BLANK (),
Revenue_of_last_year)
RETURN
IF (
DATEDIFF ( MinDate, MAX ('Client Details'[Date] ), MONTH ) + 1 < 12,
BLANK (),
Revenue_of_last_year
)
 
Any help would be greatly appreciated!
Thanks!

Hi  @Anonymous ,

 

Pls create a new thread with your requirment which will be given priority to follow.🙂

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

v-kelly-msft
Community Support
Community Support

Hi @BunnyV ,

 

First you should create a calendar table using “calendar()” function which is as shown below:

 

111.png

 

Then create a calculated column using following dax expression:

YTDtotal = 
VAR a='sample data'[MONTHYEAR]
var lastyear= YEAR('sample data'[MONTHYEAR])-1
var lastmonth=IF(MONTH('sample data'[MONTHYEAR])>=12,MONTH('sample data'[MONTHYEAR])-12,MONTH('sample data'[MONTHYEAR])+1)
var lastday=DAY('sample data'[MONTHYEAR])
var last=DATE(lastyear,lastmonth,lastday)
Return
CALCULATE(SUM('sample data'[  Sales]),DATESBETWEEN('sample data'[MONTHYEAR],last,'sample data'[MONTHYEAR]))

Finally,you will see:

112.png

For the related .pbix file,pls click here.

 

Hope this would help.

 

Best Regards,

Kelly

Hi, Kelly. Should the calculated column be added to the calendar table created in the previous step?

amitchandak
Super User
Super User

Not Sure if you have a date column. In case you create a date column based on Month Name and join it with calendar, it can be done pretty easily in the column too. I tried same for MTD and it worked.

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

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s.

Refer
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions

 

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/Winner-Topper-on-Map-How-to-Color-States-on-a-Map-wi...
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

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

thanks for the reply @amitchandak .

I have created date column using MonthYear column by parsing.

Now based on that i did created calender table and created relation with my data table.

But as you mentioend when i tried to mark it as a date table it is not letting me to do it, because the dates are having gaps in between.

 

But still i tried what you have suggest the dax formula.

I see that its not working.

 

Capture1.PNG

 

I even tried the measure, but both giving the same result.

 

Any help.

How you have created your calendar table. Hope not user Auto Calendar. You used calendar, the way it has been given in link.

If possible please share a sample pbix file after removing sensitive information.

My Recent Blog -

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.