The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
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.173 | 22,62 | 202408 |
PT | 1.458.816 | 1.439.754 | 64.514 | 63.671 | 22,61 | 202409 |
PT | 1.757.429 | 1.732.260 | 77.784 | 76.670 | 22,59 | 202410 |
PT | 1.954.116 | 1.918.556 | 84.681 | 83.140 | 23,08 | 202411 |
PT | 2.569.072 | 2.532.680 | 111.612 | 110.031 | 23,02 | 202412 |
PT | 1.710.249 | 1.679.592 | 72.633 | 71.331 | 23,55 | 202501 |
PT | 1.495.076 | 1.477.836 | 62.441 | 61.721 | 23,94 | 202502 |
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,
Solved! Go to Solution.
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 ,
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.
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.
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.
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.
Proud to be a Super User!
User | Count |
---|---|
15 | |
8 | |
6 | |
6 | |
6 |
User | Count |
---|---|
23 | |
14 | |
13 | |
8 | |
8 |