首页 > 解决方案 > 按工作日划分的月平均新用户数

问题描述

我的数据由fully_enrolled_at维度和new_users度量组成。因此,这不需要数据集,因为您可以sum()进行任何日期和测量。

我创建了以下 2 个计算:

这是该月的日历天数

days_in_month = DATEDIFF('day',MIN(DATETRUNC('month',[fully_enrolled_at])),MAX(DATETRUNC('month',DATEADD('month',1,[fully_enrolled_at]))))

这会将当前月份的日历天数更改为经过的天数:

days_in_all_months = IF MIN(DATETRUNC('month',today()))=MAX(DATETRUNC('month',[fully_enrolled_at])) THEN 1+DATEDIFF('day',DATETRUNC('month',today()),today()) ELSE [days_in_month] END

然后我绘制:discrete month(fully_enrolled_at) as dimension, and sum([new_users])/[days_in_all_months]作为度量。

这给了我想要的东西,但是我想计算每个月的工作日,而不是日历月。

如何删除周末和国定假日days_in_all_months(而本月应该有 # 个工作日)?假期名单:https ://gist.github.com/jameslin101/cf2860eba52a56281427#file-csv-of-us-stock-market-holidays-through-2020

你能帮忙调整一下吗?谢谢!

标签: tableau-api

解决方案


您可以设置当前DayWorkingDaysTotal。然后包括一个IF语句。

如果 Day = 周六或周日什么都不做 WorkingDaysTotal = WorkingDaysTotal + 1

这在理论上会给你工作日的数量。

您还需要:

如果该月的第一天,则 WorkingDaysTotal = 0

最后,再次检查该月的第一天是周六还是周日。

国定假日会更加棘手,因为不同的国家、宗教等有不同的假期。因此,您必须事先设置这些日期。

编辑

这是您想要的工作示例,不包括假期和周末。

=AVERAGE(IF(WEEKDAY(C7:I7,2)<6,IF(ISERROR(MATCH(C7:I7,Holidays,FALSE)),C8:I8)))

where C7:I7 has the dates, C8:I8 has the data, and Holidays is a named range of holiday dates. If you further want to exclude 0 values, then use this array formula

=AVERAGE(IF(WEEKDAY(C7:I7,2)<6,IF(ISERROR(MATCH(C7:I7,Holidays,FALSE)),IF(C8:I8>0,C8:I8))))

推荐阅读