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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
MCacc
Helper IV
Helper IV

cumulative sum for the last two years

I want to create a cumulative sum in Power BI for the last two years, based on a filter selection. The fact table has a 'Year' column linked to the 'Year' field in the calendar table, which is also used for filtering. 

 

Cumulative Sales Last 2 Years =
VAR MaxYear = CALCULATE(
MAX(Calendar[Year]),
ALLSELECTED(Calendar)
)
VAR MinYear = MaxYear - 1

RETURN
CALCULATE(
[Total Sales],
FILTER(
ALL(Calendar),
Calendar[Year] >= MinYear &&
Calendar[Year] <= MaxYear &&
Calendar[Date] <= MAX(Calendar[Date])
)
)

 

However, it doesn't cumulate my sum.

 

Any idea how to change/improve my measure? Thank you

2 ACCEPTED SOLUTIONS
mdaatifraza5556
Super User
Super User

Hi @MCacc 

Can you please try the below DAX ?

Cumulative Sales Last 2 Years =
VAR MaxYear = CALCULATE(
    MAX(dim_date[Year]),
    ALLSELECTED(dim_date)
)
VAR MinYear = MaxYear - 1

RETURN
CALCULATE(
    [Total Sales],
    FILTER(
        ALL(dim_date),
        dim_date[Year] >= MinYear &&
        dim_date[Year] <= MaxYear &&
        dim_date[Date] <= MAX(dim_date[Date])
    )
)

If this ansewers your questions, kindly accept it as a solution and give kudos

View solution in original post

danextian
Super User
Super User

Hi @MCacc 

 

I'm wondering why your fact table is linked to calendar on the year and not the date column and if they're linked on year, what is the use of adding Calendar[Date] <= MAX(Calendar[Date]) to your measure?

That aside, you can instead use DATESINPERIOD

Last 2 years = 
CALCULATE (
    SUM ( factTable[Value] ),
    DATESINPERIOD ( CalendarTable[Date], MAX ( CalendarTable[Date] ), -2, YEAR ),
    REMOVEFILTERS ( CalendarTable )
)

danextian_0-1745931301271.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @MCacc 

I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.
Thank you.

 

Anonymous
Not applicable

Hi @MCacc 
I wanted to check if you had the opportunity to review the information provided by @danextian and @mdaatifraza5556 . Please feel free to contact us if you have any further questions. If their response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.

Thank you.

Anonymous
Not applicable

Hi @MCacc 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

Thank you.

danextian
Super User
Super User

Hi @MCacc 

 

I'm wondering why your fact table is linked to calendar on the year and not the date column and if they're linked on year, what is the use of adding Calendar[Date] <= MAX(Calendar[Date]) to your measure?

That aside, you can instead use DATESINPERIOD

Last 2 years = 
CALCULATE (
    SUM ( factTable[Value] ),
    DATESINPERIOD ( CalendarTable[Date], MAX ( CalendarTable[Date] ), -2, YEAR ),
    REMOVEFILTERS ( CalendarTable )
)

danextian_0-1745931301271.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
mdaatifraza5556
Super User
Super User

Hi @MCacc 

Can you please try the below DAX ?

Cumulative Sales Last 2 Years =
VAR MaxYear = CALCULATE(
    MAX(dim_date[Year]),
    ALLSELECTED(dim_date)
)
VAR MinYear = MaxYear - 1

RETURN
CALCULATE(
    [Total Sales],
    FILTER(
        ALL(dim_date),
        dim_date[Year] >= MinYear &&
        dim_date[Year] <= MaxYear &&
        dim_date[Date] <= MAX(dim_date[Date])
    )
)

If this ansewers your questions, kindly accept it as a solution and give kudos

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.