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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Sofinobi
Helper IV
Helper IV

average sales quantity last 3 months

hello community,

i have a meseare to calculate the sales quantity,

i need to calculate the average sales of last 3 months
eg; we are on january, i need the sum of last 3 months /3 (october, november and december)

and next months, it calculates (november, december and january)

i copied a measure from SQLBI, but it doesn't works as i want

 
Sales R3M =
VAR NumOfMonths = 3
VAR LastCurrentDate =
    MAX ( 'Date'[Date] )
VAR Period =
    DATESINPERIOD ( 'Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        SUMX(
            VALUES ( 'Date'[Month Year] ),
            [Sales Quantity]
        )/3,
        Period
    )
VAR FirstDateInPeriod = MINX ( Period, 'Date'[Date] )
VAR LastDateWithSales = MAX ( COM_DocumentDetail[CreationDate] )
RETURN
    IF ( FirstDateInPeriod <= LastDateWithSales, Result )
 
Capture1.PNG
 
thank you all
1 ACCEPTED SOLUTION
johnt75
Super User
Super User

That version is including the current month in the calculation, you want to exclude that so I think you just need to tweak the LastCurrentDate

Sales R3M =
VAR NumOfMonths = 3
VAR LastCurrentDate =
    MIN ( 'Date'[Date] ) - 1
VAR Period =
    DATESINPERIOD ( 'Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        SUMX ( VALUES ( 'Date'[Month Year] ), [Sales Quantity] ) / 3,
        Period
    )
VAR FirstDateInPeriod =
    MINX ( Period, 'Date'[Date] )
VAR LastDateWithSales =
    MAX ( COM_DocumentDetail[CreationDate] )
RETURN
    IF ( FirstDateInPeriod <= LastDateWithSales, Result )

View solution in original post

4 REPLIES 4
v-zhangti
Community Support
Community Support

Hi, @Sofinobi 

 

You can try the following methods.
Measure:

Month Quantity = CALCULATE(SUM('Table'[Ouantity Now]),ALLEXCEPT('Date','Date'[Month]))
Cumulative 3 month = 
Var N1=SUMMARIZE(FILTER(ALL('Date'),[Month]<=SELECTEDVALUE('Date'[Month])),[Month],"Sum",[Month Quantity])
Var N2=TOPN(3,N1,[Month],DESC)
Return
SUMX(N2,[Sum])

vzhangti_0-1674028657834.png

Always calculate the sum of the last 3 months. Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

 

thank you for your support, the answer from Johnt75 is exactely what i'm looking for.

thanks for you again

johnt75
Super User
Super User

That version is including the current month in the calculation, you want to exclude that so I think you just need to tweak the LastCurrentDate

Sales R3M =
VAR NumOfMonths = 3
VAR LastCurrentDate =
    MIN ( 'Date'[Date] ) - 1
VAR Period =
    DATESINPERIOD ( 'Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        SUMX ( VALUES ( 'Date'[Month Year] ), [Sales Quantity] ) / 3,
        Period
    )
VAR FirstDateInPeriod =
    MINX ( Period, 'Date'[Date] )
VAR LastDateWithSales =
    MAX ( COM_DocumentDetail[CreationDate] )
RETURN
    IF ( FirstDateInPeriod <= LastDateWithSales, Result )

thank you @johnt75 , thats perfect

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.