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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
forbi
Frequent Visitor

calculation of the cumulative total of the previous month

I have a metric that calculates cumulative purchases over 30 rolling days:

Cumulative purchases to date =
CALCULATE(
SUM(tab[nb_visits]),
FILTER(
ALLSELECTED(tab),
tab[ref_date] >= MAX(MyCalendar[Date]) - 30
&& tab[ref_date] <= MAX(MyCalendar[Date])
)
)
I want to create a DAX measure called cumulative purchases at m-1 which will calculate the cumulative for the previous month over 30 rolling days

Desired result:
date - cumulative purchases - cumulative purchases at m-1
02/25/2020 - 35 - 18
02/24/2020 - 30 - 16
02/23/2020 - 25 - 14
02/22/2020 - 20 - 12
02/21/2020 - 15 - 10
02/20/2020 - 10 - 8

knowing that the data for the month of December are:
date - cumulative purchases
01/25/2020 - 18
01/24/2020 - 16
01/23/2020 - 14
01/22/2020 - 12
01/21/2020 - 10
01/20/2020 - 8

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @forbi ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

vtangjiemsft_0-1707115326105.png

vtangjiemsft_1-1707115334837.png

(2) We can create a measure.

cumulative purchases at m-1 = 
var _date=MAX('MyCalendar'[Date])-31
RETURN CALCULATE([cumulative purchases],FILTER(ALLSELECTED('tab'),'tab'[date]=_date))

(3) Then the result is as follows.

vtangjiemsft_2-1707115381612.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

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

forbi
Frequent Visitor

Hello,
I would like to share the solution that I found and which meets my needs,
Regards
___________________________________________________
cumulative purchases = 

  VAR __Date = LASTDATE(date[Date])
  VAR __PrvEndDate = __Date
  VAR __PrvStartDate = 
    CALCULATE(
        MIN(tab[date]),
        FILTER(
            ALLSELECTED(tab),
            [purshases - M-1] <> BLANK()
        )
    )
RETURN
CALCULATE(
    SUM(tab[purchases]),
    FILTER(
       ALLSELECTED(tab),
        tab[date] >= __PrvStartDate
        && tab[date] <= __PrvEndDate
    )
)
purshases - M-1 = VAR __Date = MAX(date[Date])
  VAR __PrvEndDate = __Date - 30
  VAR __PrvStartDate = __Date - 60
RETURN
CALCULATE(
    SUM(tab[purshases]),
    FILTER(
        ALLSELECTED(tab),
        tab[date] >= __PrvStartDate
        && tab[date] <= __PrvEndDate
    )
)

 

View solution in original post

4 REPLIES 4
forbi
Frequent Visitor

Hello,
I would like to share the solution that I found and which meets my needs,
Regards
___________________________________________________
cumulative purchases = 

  VAR __Date = LASTDATE(date[Date])
  VAR __PrvEndDate = __Date
  VAR __PrvStartDate = 
    CALCULATE(
        MIN(tab[date]),
        FILTER(
            ALLSELECTED(tab),
            [purshases - M-1] <> BLANK()
        )
    )
RETURN
CALCULATE(
    SUM(tab[purchases]),
    FILTER(
       ALLSELECTED(tab),
        tab[date] >= __PrvStartDate
        && tab[date] <= __PrvEndDate
    )
)
purshases - M-1 = VAR __Date = MAX(date[Date])
  VAR __PrvEndDate = __Date - 30
  VAR __PrvStartDate = __Date - 60
RETURN
CALCULATE(
    SUM(tab[purshases]),
    FILTER(
        ALLSELECTED(tab),
        tab[date] >= __PrvStartDate
        && tab[date] <= __PrvEndDate
    )
)

 

Anonymous
Not applicable

Hi @forbi ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

vtangjiemsft_0-1707115326105.png

vtangjiemsft_1-1707115334837.png

(2) We can create a measure.

cumulative purchases at m-1 = 
var _date=MAX('MyCalendar'[Date])-31
RETURN CALCULATE([cumulative purchases],FILTER(ALLSELECTED('tab'),'tab'[date]=_date))

(3) Then the result is as follows.

vtangjiemsft_2-1707115381612.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Greg_Deckler
Community Champion
Community Champion

@forbi So like this?

Cumulative purchases previous 30 days =
  VAR __Date = MAX(MyCalendar[Date])
  VAR __PrvEndDate = __Date - 31
  VAR __PrvStartDate = __Date - 61
  VAR __Table = FILTER(ALLSELECTED('tab'), [Date] >= __PrvStartDate && [Date] <= __PrvEndDate)
  VAR __Result = SUMX( __Table, [nb_visits] )
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thank you but it does not work 😕 
when I create a table like this, column cumulative purchases at m-1 remains empty
date - cumulative purchases - cumulative purchases at m-1
02/25/2020 - 35 - 
02/24/2020 - 30 - 
02/23/2020 - 25 - 
02/22/2020 - 20 - 
02/21/2020 - 15 - 
02/20/2020 - 10 - 
I want a result like that :
date - cumulative purchases - cumulative purchases at m-1
02/25/2020 - 35 - 18
02/24/2020 - 30 - 16
02/23/2020 - 25 - 14
02/22/2020 - 20 - 12
02/21/2020 - 15 - 10
02/20/2020 - 10 - 8

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.