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

Help calculating 12 month trailing sum in table

Hi all, i want to calculate a twelve month trailing sum of the volume column for each individual prod pkg code - how can i do this?

 

I've tried doing CALCULATE(SUM('Table' [Vol(CUR]), DATESINPERIOD('Table' [Date], Max([Date]), -12, MONTH)

 

but that isn't working.. any suggestions?

 

murphm6_0-1684774227312.png

 

10 REPLIES 10
Greg_Deckler
Super User
Super User

@murphm6 Try this, just use SUMX instead of AVERAGEX: Better Rolling Average - Microsoft Power BI Community

 

Also, it's not working because it's a single table data model which CALCULATE doesn't handle very well:


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I know the result for this is a measure - is there any way to get this value as a column? So for each row, the column would be a sum of the volume for the prior 12 months?

@murphm6 It's almost the exact same formula, you just have to drop the MAX aggregation:

 

Better Rolling Sum = 
    VAR __EndDate = 'Table'[Date]
    VAR __3MonthsAgo = EOMONTH(__EndDate, -12)
    VAR __StartDate = DATE(YEAR(__3MonthsAgo), MONTH(__3MonthsAgo), 1)
    VAR __Table = 
        SUMMARIZE(
            FILTER(ALL('Table'),[Date]>=__StartDate && [Date]<=__EndDate),
            'Table'[Month],
            "__Value",SUM('Table'[Value])
        )
RETURN
    SUMX(__Table,[__Value])

 

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

So i think this is on the right track but it's not accounting for each prod-pkg code, rather it's just summing the entire thing. What can I add to ensure it also considers the prod-pkg-code column?

 

murphm6_0-1684780225400.png

 

Thejeswar
Resident Rockstar
Resident Rockstar

Hi @murphm6 ,

I think, You might have to use  SUMX or AVERAGEX to get the rolling total

 

Regards,

 

@Thejeswar Did I miss them adding that function??


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler ,

Not sure on your ask. As @murphm6 was asking for getting the prior 12 months number, I thought RunningTotal() should be an appropriate thing for it. But I see you have suggested a different approach for this. If that works for Rolling Totals, I am good to know it for my future use.

 

In your response, what I was not sure is, Why do you have to use AVERAGEX over SUMX to get the Rolling Sum. 

 

And, BTW, in my response, I had earlier wrongly mentioned as Rolling Average in place of Rolling Sum

@Thejeswar I'm just not aware that a RUNNINGTOTAL function actually exists in DAX. If it does, it doesn't show up in my intellisense and there is no documentation on it anywhere that I find.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler ,

Sorry for creating a confusion here.😟

 

Yes you are right... There is no such function. Not sure why gave that reply, must be some confusion that made me put that non-existing function😥

 

I will correct it

@Thejeswar No worries! I was just like "Holy cow, did I miss them adding a new DAX function?" It's happened before that I find out after the fact! Would be a nice function to have!


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.