cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Frequent Visitor

## DAX to calculate last 3 months sales dynamically

Hi,

I want to calculate the last 3 months average sales dynamically.

For examples, in July, it should be sales in (April + May + June)/3.

In August, it should automatically change to sales in (May + June + July)/3.
Kindly help me to wirte the DAX for this.

1 ACCEPTED SOLUTION
Super User

I have updated the measure as  per your need to dynamically determine the last three full months based on the current date and then calculate the average sales for those months

Last3MonthsAvgSales =
VAR CurrentDate = MAX('DateTable'[Date])
VAR EndOfLastMonth = EOMONTH(CurrentDate, -1)
VAR StartDate = EOMONTH(CurrentDate, -4) + 1
RETURN
CALCULATE(
AVERAGE('Sales'[Value]),
DATESBETWEEN('DateTable'[Date], StartDate, EndOfLastMonth)
)

 Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!

4 REPLIES 4
Super User

@inikhilpv , First make sure that you have one date table and create relation with sales table

Then write measure

Last3MonthsAvgSales =
VAR CurrentDate = MAX('DateTable'[Date])
VAR StartDate = EDATE(CurrentDate, -3) -- This gets the date 3 months before the current date
RETURN
CALCULATE(
AVERAGE('Sales'[Value]),
DATESINPERIOD('DateTable'[Date], CurrentDate, -3, MONTH)
)

 Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!

Frequent Visitor

But when we use this formula, it will calculate the avg sales from April 4 to July 3. I need to find the average sales in (April+May+June)/3 irrespective of any date in July. When the month turns to August, the avarage sales must change to (May+June+July)/3

Super User

I have updated the measure as  per your need to dynamically determine the last three full months based on the current date and then calculate the average sales for those months

Last3MonthsAvgSales =
VAR CurrentDate = MAX('DateTable'[Date])
VAR EndOfLastMonth = EOMONTH(CurrentDate, -1)
VAR StartDate = EOMONTH(CurrentDate, -4) + 1
RETURN
CALCULATE(
AVERAGE('Sales'[Value]),
DATESBETWEEN('DateTable'[Date], StartDate, EndOfLastMonth)
)

 Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!

Frequent Visitor

Thank you so much. It worked.

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.