Forum Discussion
Categorical Rolling Average
Hello,
I have a table of the form:
Name | Category | Date | Sales
-It should be noted that there is a N:1 relationship between a Name and a Category (The same Name will have the same Category, but multiple Name's can share the same Category)
-There can be duplicate Dates for the same Name.
| Name | Category | Date | Sales |
| Bob | A | 6/19/2020 | 10 |
| Alice | B | 6/19/2020 | 13 |
| Bob | A | 6/19/2020 | 12 |
| Joe | B | 6/19/2020 | 15 |
| Bob | A | 6/20/2020 | 14 |
Is there a way to:
- Get the rolling 7 day average of Sales for each Name (let's call it NameRollAvg)
- Now for each Category, get the average of SalesRollAvg for all Names belonging to that category (let's call it CategoryRollAvg). However, I do not want simply the average of sales for each category.
Ultimately I would like to use these two numbers to compare a Name to the other Name's in its Category:
This is not representing the sample data above.
t-dahen ,
ah missed that summarization of the sales per day needs to be done first. Adjusted! output is now as expected:Adjusted code:
_2_Measure_RunningAverageNameCategorySales = var _CTfilter = DATESINPERIOD('Date'[Date]; MIN('Date'[Date]);-7;DAY) var _CT = CALCULATETABLE(SUMMARIZE('Table';'Table'[Name];'Date'[Date];"sales"; CALCULATE(SUM('Table'[Sales]))) ;_CTfilter;ALLEXCEPT('Table';'Table'[Name];'Table'[Category])) return IF(NOT(ISBLANK(MIN('Table'[Sales]))); AVERAGEX(_CT;[sales]); BLANK())_1_Measure_AverageOfAverageRunningSalesinCat = var _sumtbl = CALCULATETABLE(SUMMARIZE('Table';'Table'[Name];'Date'[Date];'Table'[Category]; "runningsales"; [_2_Measure_RunningAverageNameCategorySales]);ALLEXCEPT('Table';'Date';'Table'[Category])) return AVERAGEX(_sumtbl; [runningsales])New file available here.
Hope we got it now?
Kind regards, Steve.
18 Replies
- lbendlin
Super User
NameRollAvg = var n = SELECTEDVALUE('Table'[Name]) var d = SELECTEDVALUE('Table'[Date]) return calculate ( avg('Table'[Sales]) ,'Table'[Date] >= d-7 ,'Table'[Name] = n )"However, I do not want simply the average of sales for each category."
Why not? That would be the most "fair" calculation.
- t-dahen
Microsoft Employee
Thank you for the response,
I want to compare these two numbers NameRollAvg and CategoryRollAvg. Basically to see how did this Name compare to others in it's Category. Take for example, Bob's Sales for this week is 1 and Alice's Sales for this week is 14 and they are both Category A. If we did "average of sales per category" the CategoryRollAvg would be (1 + 14) / 7 = 2.14... However, what I would like to see is the average of all the NameRollAvg. [(1/7) + (14/7)] / 2 = 1.07...
- lbendlin
Super User
Yeah, no, still not buying it. You already know that 14 is more than 1. Fudging the average in that way is not constructive IMHO.