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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Fragmaticx
Frequent Visitor

Calculate avg for the previous 3 month and apply

Hi 

I'm working on a model. And I want to calculate Average Cost Per Parcel (for the preivous 3 month), with data.

And Apply that to Parcels Sales (EST)

And it seems like it working, or atleast i calculate a avg. But it does not apply to the 2 most recent months:

Market Lastmile Amount Cost  Estimated Lastmile Cost  Parcels Cost  Parcels Sales (EST) Avg Cost Per Parcel (Last 3 Months)ShipPeriod
PT                                  1.375.489                                      1.360.924                   60.817                                60.17322,62202408
PT                                  1.458.816                                      1.439.754                   64.514                                63.67122,61202409
PT                                  1.757.429                                      1.732.260                   77.784                                76.67022,59202410
PT                                  1.954.116                                      1.918.556                   84.681                                83.14023,08202411
PT                                  2.569.072                                      2.532.680                 111.612                              110.03123,02202412
PT                                  1.710.249                                      1.679.592                   72.633                                71.33123,55202501
PT                                  1.495.076                                      1.477.836                   62.441                                61.72123,94202502
PT                                   60.341 202503
PT                                         182 202504

 

 

Avg Cost Per Parcel (Last 3 Months) = 
VAR AvailableMonths =
FILTER(
    VALUES('DateTable'[ShipPeriod]),
    NOT ISBLANK([Avg Cost Per Parcel]) && [Avg Cost Per Parcel] <> 0
)

VAR LastThreeMonthsWithData =
    TOPN(3, AvailableMonths, 'DateTable'[ShipPeriod], DESC)  -- Get the latest 3 months with data

VAR Result =
    CALCULATE(
        AVERAGEX(LastThreeMonthsWithData, [Avg Cost Per Parcel]),  -- Average only non-blank months
        'DateTable'[ShipPeriod] IN LastThreeMonthsWithData
    )

RETURN
    Result




Most of this is done by chatgpt, since i'm not that good at dax. So if there is something obvious im missing, please help me 🙂 


Thanks in advance,



1 ACCEPTED SOLUTION
v-tsaipranay
Community Support
Community Support

Hi @Fragmaticx ,

Thanks for reaching out to the Microsoft Fabric Community, and we appreciate @some_bih  valuable observation he is correctly highlighted that the issue may stem from how your DateTable is related to the fact table.

 

If the DateTable is not properly related using a valid date column (as opposed to ShipPeriod), time-based calculations like TOPN may not evaluate as expected. Additionally, your current DAX logic retrieves the last three months globally, not relative to each row context. This can result in the calculation not applying to recent months those months are excluded from the filtered data.

 

To address this, first ensure that the DateTable is correctly marked as a date table and is connected to the fact table using a proper date-type column. Next, verify that the [Avg Cost Per Parcel] measure is returning valid values for the months you are analyzing, particularly the most recent ones. Finally, consider updating your DAX logic to calculate a rolling 3-month average relative to each row’s period, rather than using a fixed set of the last three months globally.

 

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

 

Thankyou.

 

 

View solution in original post

5 REPLIES 5
v-tsaipranay
Community Support
Community Support

Hi @Fragmaticx ,

 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

 

Thank you.

v-tsaipranay
Community Support
Community Support

Hi @Fragmaticx ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


Thank you.

v-tsaipranay
Community Support
Community Support

Hi @Fragmaticx ,

Thanks for reaching out to the Microsoft Fabric Community, and we appreciate @some_bih  valuable observation he is correctly highlighted that the issue may stem from how your DateTable is related to the fact table.

 

If the DateTable is not properly related using a valid date column (as opposed to ShipPeriod), time-based calculations like TOPN may not evaluate as expected. Additionally, your current DAX logic retrieves the last three months globally, not relative to each row context. This can result in the calculation not applying to recent months those months are excluded from the filtered data.

 

To address this, first ensure that the DateTable is correctly marked as a date table and is connected to the fact table using a proper date-type column. Next, verify that the [Avg Cost Per Parcel] measure is returning valid values for the months you are analyzing, particularly the most recent ones. Finally, consider updating your DAX logic to calculate a rolling 3-month average relative to each row’s period, rather than using a fixed set of the last three months globally.

 

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

 

Thankyou.

 

 

Hi @Fragmaticx ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


Thank you.

some_bih
Super User
Super User

Hi @Fragmaticx your date table is connected with some date in fact table which is not 'DateTable'[ShipPeriod]?

Check relationship your date table date column with other tables.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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