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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
haputhanthree
Frequent Visitor

Averagex over multiple dates

Hi,

I have an unrelated calendar table and the below measures in the sales table. 


InTransit =

 CALCULATE(

    COUNT(Sales[ID])

    ,FILTER(

        'Sales'

        ,Sales[DeliveryDate] < MAX('Dim Calendar'[Date])

           && Sales[ArrivalDate] > MAX('Dim Calendar'[Date])

    )

)

 

InTransit - The number of orders that have been delivered by the end of the month but have not been delivered yet. 

I want to calculate the average for the last 2 months as (# of last month's in transit + this moth transit)/2 

But the below measure is not working as expected. Look forward to your support. 

Average Lst 2 Months =

AVERAGEX(

    VALUES('Dim Calendar'[Month])

    ,[InTransit]

)
Current result 

haputhanthree_0-1669588117464.png


Expected Result
 Screenshot 2022-11-27 at 23.34.00.png
Link to PBIX file 

1 ACCEPTED SOLUTION
haputhanthree
Frequent Visitor

The below measue worked for me. 

 Average Lst 2 Months =
VAR NumOfMonths = 2
VAR LastCurrentDate =
    MAX ( 'Dim Calendar'[Date] )
VAR Period =
    DATESINPERIOD ( 'Dim Calendar'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR result =
    AVERAGEX (
        SUMMARIZE (
            CALCULATETABLE ( 'Dim Calendar', Period ),
            'Dim Calendar'[Month],
            "InTransit", [InTransit]
        ),
        [InTransit]
    )
RETURN
    result

View solution in original post

3 REPLIES 3
haputhanthree
Frequent Visitor

The below measue worked for me. 

 Average Lst 2 Months =
VAR NumOfMonths = 2
VAR LastCurrentDate =
    MAX ( 'Dim Calendar'[Date] )
VAR Period =
    DATESINPERIOD ( 'Dim Calendar'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR result =
    AVERAGEX (
        SUMMARIZE (
            CALCULATETABLE ( 'Dim Calendar', Period ),
            'Dim Calendar'[Month],
            "InTransit", [InTransit]
        ),
        [InTransit]
    )
RETURN
    result

haputhanthree
Frequent Visitor

@Jihwan_Kim  Thank you!

If I want to calculate average of last 12 moths must define 12 variables. Is there any optimization that you could think off to handle that scenario?

Jihwan_Kim
Super User
Super User

Hi,

Please check the attached pbix file.

 

Jihwan_Kim_0-1669610987381.png

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.