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
Rygar
Helper II
Helper II

TotalYTD until last existing Date

Dear Community,

I would like to calculate the sum of spendigs for every year until the last existing month in my datetable to compare the spendings.

Example:
Last date in my Datetable is the 5th of Feb, so I need the Spendings until Feb for every Year existing in my Datetable.

This works fine:

 

Spend latest Month = 
VAR LM=TOTALYTD(
    SUM(Spend[GrossSpend]),
    dDate[Date],
    MONTH(dDate[Date])<=2
)
RETURN LM

 

PROBLEM: I have to manually enter the <=2 (Feb) in this Expression.

Something like this does not bring the correct results:

 

MONTH(dDate[Date])<=MONTH(MAX(dDate[Date]))

 

Any Idea?

Thanks and regards,
Michael

1 ACCEPTED SOLUTION

Ah, you need to remove the local filter context when computing the last month.

Spend latest Month =
VAR _LastMonth = CALCULATE ( MONTH ( MAX ( dDate[Date] ) ), ALL ( dDate ) )
VAR LM =
    CALCULATE (
        SUM ( Spend[GrossSpend] ),
        KEEPFILTERS ( MONTH ( dDate[Date] ) <= _LastMonth )
    )
RETURN
    LM

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

Since you aren't using TOTALYTD quite how it's designed, you probably don't want that exact function.

 

See if something like this works for you:

Spend latest Month =
VAR _LastMonth = MONTH ( MAX ( dDate[Date] ) )
VAR LM =
    CALCULATE (
        SUM ( Spend[GrossSpend] ),
        KEEPFILTERS ( MONTH ( dDate[Date] ) <= _LastMonth )
    )
RETURN
    LM

Note: I'm assuming you already have the year in your filter context. 

Thank you very much! The Expression works fine, when I set _LastMont to a fixed value (March):

KEEPFILTERS ( MONTH ( dDate[Date] ) <= 3 )

But when I use a dynamic Value _LastMonth:

KEEPFILTERS ( MONTH ( dDate[Date] ) <= _LastMonth )

the Result is completely different and for me wrong. I don't understand this.

Example File here.

 

 

Ah, you need to remove the local filter context when computing the last month.

Spend latest Month =
VAR _LastMonth = CALCULATE ( MONTH ( MAX ( dDate[Date] ) ), ALL ( dDate ) )
VAR LM =
    CALCULATE (
        SUM ( Spend[GrossSpend] ),
        KEEPFILTERS ( MONTH ( dDate[Date] ) <= _LastMonth )
    )
RETURN
    LM

Now I learned again! Thanks a lot for this solution! 

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.