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
CourtneyLeah
Frequent Visitor

Calculate sum of the past 3 months

Hi guys,

 

I'm fairly new to Power BI and my DAX knowledge is fairly limited, so bear with me.

 

I have a tabel with the following information:

 

Month Start, Company, # Safety Briefings for month

1/01/2016, Company A, 5

1/01/2016, Company B, 10

1/01/2016, Company C, 15

 

1/02/2016, Company A, 6

1/02/2016, Company B, 4

1/02/2016, Company C, 6

etc

 

What I want to do is calculate the total number of safety briefings completed over the past 3 months.

I have tried creating a new measure using the following:

 

Safety Briefings last 3 months = CALCULATE(sum(Table1[Safety Briefings]), DATESINPERIOD(Table1[Start Date], LASTDATE(Table1[Start Date]), -3, MONTH))

 

However, this only gives me the total for the month, NOT the past 3 months.

 

Any help would be greatly appreciated.

 

Cheers

 

1 ACCEPTED SOLUTION
GilbertQ
Super User
Super User

Hi @CourtneyLeah, it would be best to have a Date Table and link your Month Start to the date table.

 

If that is done you could then create the following calculated measure which should work for you.

 

Safety Briefings last 3 months =
CALCULATE (
    SUM ( Table1[Safety Briefings] ),
    DATEADD ( 'Date'[Calendar Date], -3, MONTH )
)

Formatted using DAX Formatter





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

Proud to be a Super User!







Power BI Blog

View solution in original post

12 REPLIES 12
pandakaka
New Member

How do you do the calculate sum of the past 3 months for each company?

I created a measure using your dax below, but it doesn't work if I put in a filter for each company

 

Cheers

Hi @pandakaka

 

What you could do is to keep the measure and then put each company onto the Rows?





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

Proud to be a Super User!







Power BI Blog

GilbertQ
Super User
Super User

Hi @CourtneyLeah, it would be best to have a Date Table and link your Month Start to the date table.

 

If that is done you could then create the following calculated measure which should work for you.

 

Safety Briefings last 3 months =
CALCULATE (
    SUM ( Table1[Safety Briefings] ),
    DATEADD ( 'Date'[Calendar Date], -3, MONTH )
)

Formatted using DAX Formatter





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

Proud to be a Super User!







Power BI Blog

Anonymous
Not applicable

I am a little confused by this solution. From what I understand this won't summarize over all three months, it will just summarize what happened three months ago. I.e. if I put "Safety Briefings last 3 months" in a table grouped per month and look at the April-row, then I will see the sum of January and not the sum of Januari-March, right!?

Hi there, the proposed solution will go back 3 months and then SUM all the data starting 3 months prior.

So if you had to put this measure into a table and drag in the Month column from your date table you should then see the previous 3 months




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

Proud to be a Super User!







Power BI Blog

Anonymous
Not applicable

Hmmm... do you mean like this? To me this doesn't look like the sum of the last three months but I might misunderstand you?

 

briefing.png

Anonymous
Not applicable

For anyone else stumbling into this thread these solutions might be of interest (2 more ways of doing almost the same thing are commented out). They could all be useful depending on challenge...

 

briefing.png

Great formula! Thanks! 

In case you want to show the total of jan + Feb (ie 210) in Feb line instead of Mar line, then this works: 

DATESINPERIOD('Dim Calendar'[Date], LASTDATE(DATEADD('Dim Calendar'[Date], 0, MONTH)), -3, MONTH)
Anonymous
Not applicable

That is great! So simple example with the datesbetween function!

 

Thank you very much!

Thanks @GilbertQ. The date table has helped enormously.

 

Cheers

Hi @CourtneyLeah,

 

Please mark the corresponding reply as answer, which will help more people find solution clearly.

 

Best Regards,
Angelia

Hi @CourtneyLeah, glad that it helped out Smiley Happy





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

Proud to be a Super User!







Power BI Blog

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.