excel - Moving average excluding weekends and holidays
问题描述
I have a table within PowerPivot currently that tracks a count of customers through our sales pipeline. From (by sales location) first interaction to charged sale. So far, I’ve creates a moving 5-day average that averages each task. Below is the DAX formula I’ve created thus far and an example table.
=
CALCULATE (
SUM ( [Daily Count] ),
DATESINPERIOD ( Table1[Date], LASTDATE ( Table1[Date] ), -7, DAY ),
ALLEXCEPT ( Table1, Table1[Sales Location], Table1[Group] )
)
/ 5
Where I’m struggling is being able to come up with a way to exclude weekends and company observed holidays. Additionally, if a holiday falls on a weekday I would like to remove that from the average and go back an additional day (to smooth the trend).
For example, on 11/26/18 (the Monday after Thanksgiving and Black Friday) I would like to average the five business days previous (11/26/18, 11/21-11/19, and 11/16). In the example above, the moving total and average for the previous 5 days should be Intake = 41 (total) 8.2 (average), Appointment = 30 (total) 6 (average), and Sale = 13 (total) and 2.6 (average).
Based on the formula currently, each of these numbers is inaccurate. Is there an easy way to exclude these days?
Side note: I’ve created an ancillary table with all holidays that is related to the sales data that I have.
Thank you for the help!
解决方案
为此,我建议使用与Table1
该Date
列相关的日历表,该表也有一个列IsWorkday
,1
如果那一天是工作日,0
否则。
设置完成后,您可以编写如下度量:
Moving Avg =
VAR Last5Workdays =
SELECTCOLUMNS (
TOPN (
5,
FILTER (
DateTable,
DateTable[Date] <= EARLIER ( Table1[Date] )
&& DateTable[IsWorkday] = 1
),
DateTable[Date], DESC
),
"Workday", DateTable[Date]
)
RETURN
CALCULATE (
SUM ( Table1[Daily Count] ),
Table1[Date] IN Last5Workdays
ALLEXCEPT ( Table1, Table1[Sales Location], Table1[Group] ),
)
/ 5
此处的TOPN
函数返回前 5 行,DateTable
其中每行必须是小于或等于当前Table1
行中日期的工作日(该EARLIER
函数引用定义当前行的较早行上下文)。
然后,我SELECTCOLUMNS
通过选择单个列(我已将其命名为Workday
)来将此表转换为列表。从那里开始,基本上你的日期过滤器改变了一点。
推荐阅读
- c# - 如何让 Parent 只使用它的游戏对象而不被子类访问?
- c++ - 分配给 std::vector 元素会导致内存损坏
- scala - 无法在 Scala 中使用多个未来的布尔类型
- azure-ad-b2c - 如何在AD B2C中预先创建“商业客户”
- java - 无法将 Comparable 转换为实现它的类
- javascript - 如何从nodejs服务器端的表单标签记录数据?
- android - 当从 Fragment B 到 A 的 popbackstack 时保存 Fragment View(如完成活动)
- c++ - Initialization of a local static variable throught multiple function calls
- angular - 在我的 Angular 项目中,如果没有映射或过滤方法,则无法通过“id”获取数据
- php - Laravel 检查 eloquent 的返回是单个集合还是项目集合