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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
SLima
Helper I
Helper I

Average

 

Good afternoon.

How do I calculate the average purchase of the past months to customer, starting from the selected month?

example.

I selected june/2021 he would make the average of may, april, march, february and january of that same year.

 

Thank you in advance.

 

4 REPLIES 4
OwenAuger
Super User
Super User

Hi @SLima 

Assuming you have a Date table set up with a YearMonth column (or similar), something along these lines should do the trick:

 

 

Average Purchases over 5 preceding months = 
CALCULATE ( 
    AVERAGEX ( 
        VALUES ( 'Date'[YearMonth] ),
        [Purchases]
    ),
    DATESINPERIOD (
        'Date'[Date],
        EOMONTH ( MAX ( 'Date'[Date] ), -1 ),
        -5,
        MONTH
    )
)

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

it would not be the previous 5 months, it would be from the selected month/year

Yes, in the code above, the maximum visible date is used as the reference point for determining the date filter for the "previous" 5 months.

 

You can change MAX ( 'Date'[Date] ) to TODAY() for the date filter to be determined relative to the current date (when the query is generated from the report page):

 

 

Average Purchases over 5 preceding months =
CALCULATE (
    AVERAGEX (
        VALUES ( 'Date'[YearMonth] ),
        [Purchases]
    ),
    DATESINPERIOD (
        'Date'[Date],
        EOMONTH ( TODAY (), -1 ),
        -5,
        MONTH
    )
)

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

I did it that way

Sales Average last months = 
CALCULATE ( 
    AVERAGEX ( 
        VALUES ( dCalendario[MonthYear] ),
        [Sales]
    ),
    DATESINPERIOD (
        ( dCalendario[date]),
        EOMONTH ( MAX ( dCalendario[date] ), -1 ),
        SWITCH(VALUES(dCalendario[MonthNum]),
        12,-11,
        11,-10,
        10,-9,
        9,-8,
        8,-7,
        7,-6,
        6,-5,
        5,-4,
        4,-3,
        3,-2,
        2,-1),
        MONTH
    )

Helpful resources

Announcements
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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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