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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
micwed
New Member

How to calculate the sum of the last 6 month?

Hi,

 

I am very new to DAX and it it is realy hard to understand fom me. Maybe somebody can give me an example or link to the answer of the same question. 

 

I want to calculate the sum of quantity of the last 6 month

Usualy I would try as in any other language: Measure = CALCULATE([Sum of Quantity], 'Date'[Date]>=DATEADD(TODAY(),-6,month))

It should be a very simple formular, honestly it isn't.

How can I do this, without adding claculated columns just within a measure. Just in one measure.

 

Thanks in advance!

 

Mike

5 REPLIES 5
Greg_Deckler
Super User
Super User

@micwed Try this:

Last 6 Months Measure =
  VAR __Today = TODAY()
  VAR __SixMonthsAgo = EOMONTH(__Today,-7) + 1
  VAR __Table = FILTER('Table',[Date]>=__SixMonthsAgo && [Date]<=__Today)
  VAR __Result = SUMX(__Table,[Quantity])
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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

thank you very much for your answer!

 

After a wile I found a solution that fits to my needs.

I used

Sum of Quantity = SUM(Sales[Quantity])

Sold Quantity = CALCULATE([Sum of Quantity],DATESINPERIOD('Date'[Date],TODAY(),-6, MONTH))
Yes of course this is rolling month, but its okay in this case. Is there any advantagas or disadvantages on your way or my way?
 
Best regards,
Mike

I'm exceptionally new to DAX and it is realy hard to comprehend fom me. Perhaps someone can offer me a model or connection to the response of a similar inquiry.

@belanoor @micwed Can you provide sample data. By way of explanation in terms of the formula:

Last 6 Months Measure =
  VAR __Today = TODAY()
// Get today's date

  VAR __SixMonthsAgo = EOMONTH(__Today,-7) + 1 
// Goes back 7 months (end of month) + 1 is the first of the month 6 months ago

  VAR __Table = FILTER('Table',[Date]>=__SixMonthsAgo && [Date]<=__Today) 
// filter the table such that all dates between today's date and six months ago

  VAR __Result = SUMX(__Table,[Quantity])
// Sum up the Quantity column for all rows

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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

I just saw, that probably I can't use your solution, because I have my data stored in a star-schema and have no generic Date column in my Sales data, but you assume it, right?

 

micwed_2-1674050444989.png

Here just a small part of my model

 

 

Best,

 

Mike

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.