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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
JellyFishBi
Helper I
Helper I

MTD / LY MTD

Thank you for reading. 

 

I have two measures:
MTD = TOTALMTD(SUM('4011-Visits'[GrossProd]),'Date'[Date])

MTD LY = CALCULATE([MTD],DATEADD('Date'[Date], -1,YEAR))
 
And the Result:
 
SnipMTD.JPG
How do I get MTD LY to toal correctly?  The value we are looking for is 594,233.
 
Many thanks,
 
Mike
 

 

1 ACCEPTED SOLUTION

To @parry2k point, if you don't want the 'Is Past' column you can get the last transaction date from your detail table and use it in a VAR like so.

 

MTD LY = 
VAR _LastDate = MAX ( '4011-Visits'[DateField] )
RETURN
CALCULATE( [MTD],
    CALCULATETABLE (
        SAMEPERIODLASTYEAR ( DATE[Date] ),
        DATE[Date] <= _LastDate
    )
)

 

View solution in original post

12 REPLIES 12
v-alq-msft
Community Support
Community Support

Hi, @JellyFishBi 

 

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

 

4011-Visits:
d1.png

 

You may create measures as below.

MTD = 
var _maxdate = MAX('4011-Visits'[Date])
var _year = YEAR(_maxdate)
var _month = MONTH(_maxdate)
var _day = DAY(_maxdate)
return
CALCULATE(
    SUM('4011-Visits'[GrossProd]),
    FILTER(
        ALLSELECTED('4011-Visits'),
        '4011-Visits'[Date]>=DATE(_year,_month,1)&&
        '4011-Visits'[Date]<=DATE(_year,_month,_day)
    )
)

MTD LY = 
var _maxdate = MAX('4011-Visits'[Date])
var _year = YEAR(_maxdate)
var _month = MONTH(_maxdate)
var _day = DAY(_maxdate)
return
CALCULATE(
    SUM('4011-Visits'[GrossProd]),
    FILTER(
        ALLSELECTED('4011-Visits'),
        '4011-Visits'[Date]>=DATE(_year-1,_month,1)&&
        '4011-Visits'[Date]<=DATE(_year-1,_month,_day)
    )
)

 

Result:

d2.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.

 

 

Thank you Allan.  Looks like your well thought out solution saved the day.

 

Might you be abel to do the same thing for week to date last year(WTD LY)?  Currently I have WTD as:

 

WTD =
VAR CurrentDate =
    LASTDATE ( 'Date'[Date] )
VAR DayNumberOfWeek =
    WEEKDAY ( LASTDATE ( 'Date'[Date] )3 )
RETURN
    CALCULATE (
        SUM ( '4011-Visits'[GrossProd] ),
        DATESBETWEEN (
            'Date'[Date],
            DATEADD ( CurrentDate-1 * DayNumberOfWeekDAY ),
            CurrentDate
        )
    )

 

 

 

@JellyFishBi did you tried the solution @jdbuchanan71 posted. You never posted any feedback on that solution. I think you should test and tell if it worked or not.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Ashish_Mathur
Super User
Super User

Hi,

Do these measures work?

MTD = SUM('4011-Visits'[GrossProd])

MTD LY = CALCULATE([MTD],SAMEPERIODLASTYEAR('Date'[Date]))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

So Very Odd.  I have used these measures in the past, and have worked fine in the past.  I run at least 10 different dimilar models with the logic you have specified and two days ago, the logic had failed.  It led me to wondering the integrity of the date table.  All seemed to fall into place when I set the ending date table value to Today().  @Ashish_Mathur In your travels have you experienced ever your measures all of a sudden failing?

 

Mike

 

Hi,

Has your question been solved?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
jdbuchanan71
Super User
Super User

@JellyFishBi 

Add a column to your 'Date' table named 'Is Past'

 

=DATE[Date] <= TODAY()

 

Then you can change your PY MTD 

 

MTD LY = 
CALCULATE( [MTD],
    CALCULATETABLE (
        SAMEPERIODLASTYEAR ( DATE[Date] ),
        DATE[Is Past] = TRUE
    )
)

 

This will stop the LY calc from going past the current date but in the prior year.  Pattern courtesy of SQLBI https://www.sqlbi.com/articles/hiding-future-dates-for-calculations-in-dax/

@jdbuchanan71 great idea but I would avoid adding un-necessary column but rather get the last transaction date and filter on that, what happens if you are not looking at the current year, you are coming 2019 vs 2018, this logic will not work. 

 

Although it is a cool solution.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k 

If you are looking at 2019 vs 2018 your not going to have a month with partial current results but full py reusults though.  You can change the check for 'Is Past' to look at say MAX(Sales[Invoice Date]) instead if you don't want to use TODAY().

To @parry2k point, if you don't want the 'Is Past' column you can get the last transaction date from your detail table and use it in a VAR like so.

 

MTD LY = 
VAR _LastDate = MAX ( '4011-Visits'[DateField] )
RETURN
CALCULATE( [MTD],
    CALCULATETABLE (
        SAMEPERIODLASTYEAR ( DATE[Date] ),
        DATE[Date] <= _LastDate
    )
)

 

Thanks for the help!  

parry2k
Super User
Super User

@JellyFishBi are you apply filter on date? Total is giving full month of PY.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.