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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
NatK
Helper I
Helper I

YTD for previous month to date

I'm trying to come up with a calculation for the year to date sales of the previous month-to-date

 

For example, my current date is 11/18/2021. My previous month to date is 10/18/2021. I am looking for the YTD amount of the previous month-to-date.  I would like to return sales from 1/1/2021 through 10/18/2021.

 

Current date = 11/18/2021

Current MTD = 11/1/2021 - 11/18/2021

Previous Month MTD = 10/1/2021 - 10/18/2021

YTD of Previous Month MTD = 1/1/2021 - 10/18/2021 (This is what I'm looking for)

 

Any suggestions??

 

Thanks!!

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

Hi @NatK ,

I created a sample pbix file(see attachment), please check whether that is what you want. You can create a measure as below to get the YTD for previous month to date:

YTD for previous month to date = 
VAR _seldate = SELECTEDVALUE ( 'Date'[Date] )
VAR _year =  YEAR ( _seldate )
VAR _month = MONTH ( _seldate )
VAR _day =   DAY ( _seldate )
VAR _presmonth =  IF ( _month = 1, DATE ( _year - 1, 1, 1 ), DATE ( _year, 1, 1 ) )
VAR _premaxday =  EOMONTH ( DATE ( _year, _month - 1, 1 ), 0 )
VAR _preemonth =
    IF (
        _month = 1,
        DATE ( _year - 1, 12, _day ),
        IF ( _day > DAY ( _premaxday ), _premaxday, DATE ( _year, _month - 1, _day ) )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[Sales] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] >= _presmonth
                && 'Table'[Date] <= _preemonth
        )
    )

yingyinr_0-1638265136770.png

If the above one is not what you want, please provide some sample data(exclude sensitive data) and your expected result with calculation logic and special examples. Thank you.

Best Regards

Community Support Team _ Rena
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

5 REPLIES 5
v-yiruan-msft
Community Support
Community Support

Hi @NatK ,

I created a sample pbix file(see attachment), please check whether that is what you want. You can create a measure as below to get the YTD for previous month to date:

YTD for previous month to date = 
VAR _seldate = SELECTEDVALUE ( 'Date'[Date] )
VAR _year =  YEAR ( _seldate )
VAR _month = MONTH ( _seldate )
VAR _day =   DAY ( _seldate )
VAR _presmonth =  IF ( _month = 1, DATE ( _year - 1, 1, 1 ), DATE ( _year, 1, 1 ) )
VAR _premaxday =  EOMONTH ( DATE ( _year, _month - 1, 1 ), 0 )
VAR _preemonth =
    IF (
        _month = 1,
        DATE ( _year - 1, 12, _day ),
        IF ( _day > DAY ( _premaxday ), _premaxday, DATE ( _year, _month - 1, _day ) )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[Sales] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] >= _presmonth
                && 'Table'[Date] <= _preemonth
        )
    )

yingyinr_0-1638265136770.png

If the above one is not what you want, please provide some sample data(exclude sensitive data) and your expected result with calculation logic and special examples. Thank you.

Best Regards

Community Support Team _ Rena
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

@NatK , With help from Time Intelligence and date table, Example

 

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 month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))
next month Sales = CALCULATE(SUM(Sales[Sales Amount]),nextmonth('Date'[Date]))

 

 

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

 

 

Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
https://www.youtube.com/watch?v=km41KfM_0uA

 

Power BI — Month on Month with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
https://www.youtube.com/watch?v=6LUBbvcxtKA

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 @amitchandak. I am familiar with the time intelligence functions. My problem, however, is less straightforward. I am looking for YTD of Previous Month MTD. My report also has a 'month year' slicer. I came up with a solution, although it is quite long. I'm sure there is a shorter version out there, but here is my solution in case someone else may have the same question (If anyone comes up with a shorter solution, please share!):

 

Purchase Order Count YTD 1 Month Prior =

VAR LASTDAY = LASTDATE('Date'[TheDate])
VAR LASTDAYnMOPRIOR = DATE(YEAR(LASTDAY), MONTH(LASTDAY)-1, DAY(LASTDAY))

VAR CURRMO_TOTAL = --CALC FOR CURRENT MONTH
CALCULATE([Purchase Order Count], FILTER(ALL('Date'),
AND(
'Date'[TheDate] <= LASTDAYnMOPRIOR,
'Date'[TheYear] = YEAR(LASTDAYnMOPRIOR)
)
))

VAR nMOPRIOR_TOTAL = --CALC FOR PREVIOUSMONTHS
CALCULATE (
[Purchase Order Count],
DATESYTD ( 'Date'[TheDate] ),
MONTH ( 'Date'[TheDate] ) < MONTH ( LASTDAY )
)

RETURN IF(MONTH (MAX('Date'[TheDate]) ) = MONTH(MAX('Last Dataset Refresh'[Last Dataset Refresh (PBI Service)])), CURRMO_TOTAL, nMOPRIOR_TOTAL)
 

 

@NatK , DO you need YTD till last date


YTD Sales =
var _1 = maxx(allselected('Date'),'Date'[Date])
var _max = date(year(_1) , month(_1) -1 , day(_1))
return
CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"), 'Date'[Date]<= _max)

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

Yes, I need YTD to last date. I tried the measure you provided. It works for the current mtd. If I select a prior month (i.e. October 2021), then the date range it returns is 1/1/2021- 10/1/2021. The range should be 1/1/2021-9/30/2021.

Helpful resources

Announcements
Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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