Forum Discussion
average over period
Hi,
I was unable to find a straight answer to my question. The goal is to determine the average monthly sales for each product over a specified period of time. It is up to the user to determine the time frame. I would like the average to consider months regardless of whether the product was sold in the months included in the range. I created a Date table with one column of running dates in the format dd/mm/yyyy.
for example, the table is :
| Product | Date | Sale price ($) |
| A | 1/1/2022 | 30 |
| A | 20/1/2022 | 10 |
| A | 1/3/2022 | 20 |
| B | 1/2/2022 | 50 |
| B | 1/5/2022 | 20 |
| B | 1/7/2022 | 10 |
And the selected range is 2022 , 8 months (considering now is August), therefore, the expected output should be:
| Product | Average |
| A | 7.5 |
| B | 10 |
what I actually get right now:
| Product | Average |
| A | (30+10+20)/3= 20 |
| B | (50+20+10)/3=26.6 |
thanks!!
Hi, OfirK1
You can try the following methods.
Table:
Date = CALENDAR(DATE(2022,1,1),TODAY())Column:
Year = YEAR([Date])Month = Month([Date])Measure:
Average = Var N1=CALCULATE(SUM('Table'[Sale price ($)]),ALLEXCEPT('Date','Date'[Month],'Date'[Year],'Table'[Product])) Var N2=CALCULATE(DISTINCTCOUNT('Date'[Month]),ALLEXCEPT('Date','Date'[Month],'Date'[Year])) return DIVIDE(N1,N2)Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
5 Replies
- jgeddesSuper User
There are likely other ways to do this but the following measure will work.
ProductAverage =var _salesSum =SUM(sales_table[Sale price ($)])var _selectedDate =SELECTEDVALUE(dimDate[Date].[MonthNo])var _numberOfMonths =((YEAR(TODAY()) - SELECTEDVALUE(dimDate[Date].[Year])) * 12) + _selectedDateReturnDIVIDE(_salesSum,_numberOfMonths,0) - jgeddesSuper User
The measure as it is currently written will only return a value if there is a date to evaluate against. If you want to return a default value it could be added to the measure with if statements.
- v-zhangtiCommunity Support
Hi, OfirK1
You can try the following methods.
Table:
Date = CALENDAR(DATE(2022,1,1),TODAY())Column:
Year = YEAR([Date])Month = Month([Date])Measure:
Average = Var N1=CALCULATE(SUM('Table'[Sale price ($)]),ALLEXCEPT('Date','Date'[Month],'Date'[Year],'Table'[Product])) Var N2=CALCULATE(DISTINCTCOUNT('Date'[Month]),ALLEXCEPT('Date','Date'[Month],'Date'[Year])) return DIVIDE(N1,N2)Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- OfirK1New Member
Hi, I get this error:
All arguments within an ALLEXCEPT function must be related to (or contained by) the table which is used as the first argument. Where a one-to-many relationship exists, the table which is used as the first argument must be on the 'many' side of that relationship.
in my case, its one-to-many relationship between Date and Table (many products can be sold in one date).