用于查找一段时间内平均持续时间的 QlikSense 表达式

QlikSense expression to find average duration over a period of time

我想找出不同时间段的总持续时间(以小时为单位)。 IE。一旦我添加了诸如 'October' 之类的过滤器,它应该会显示该月的总小时数。我想将多个与会者的重复课程计为 1 节课。 IE。教授该科目所花费的时间。

 Date        Duration  Subject   Attendee
 1/10/2019     2:00    Math       Joe Bloggs
 1/10/2019     2:00    Math       John Doe
 2/10/2019     3:00    English    Jane Doe
 6/11/2019     1:00    Geog       Jane Roe
 17/12/2019    0:30    History    Joe Coggs

我想要花在这些科目上的总时数。这意味着上面的总时长应该加起来 6:30,因为两节数学课应该只算作 1 节课(2 小时)。我如何编写一个表达式来生成我们整体学习的 KPI,然后还允许我深入到月份和日期。提前致谢

可以建议您创建另一个包含不同值的 table(我假设唯一组合是 Date <-> Subject

下面的脚本将创建 OverallDuration table 将包含组合 Date <-> Subject 的不同持续时间值。这样您将拥有一个可用于 KPI 的附加字段 OverallDuration

OverallDuration table 链接到 RawData table(它本身链接到 Calendar table)这意味着OverallDuration 计算将遵循 SubjectLessonYearLessonMonth 等的选择(请查看下面的 Math 选择图片)

RawData:
Load
  *,
  // Create a key field with the combination of Date and Subject
  Date & '_' & Subject as DateSubject_Key
;
Load * Inline [
Date,          Duration, Subject,  Attendee
1/10/2019,     2:00,     Math,     Joe Bloggs
1/10/2019,     2:00,     Math,     John Doe
2/10/2019,     3:00,     English,  Jane Doe
6/11/2019,     1:00,     Geog,     Jane Roe
17/12/2019,    0:30,     History,  Joe Coggs
];

// Load distinct DateSubject_Key and the Duration
// converting the duraion to time.
// This table will link to RawData on the key field
OverallDuration:
Load
  Distinct
  DateSubject_Key,
  time(Duration)          as OverallDuration
Resident
  RawData
;

// Creating calendar table from the dates (distinct) 
// from RawData and creating two additional fields - Month and Year
// This table will link to RawData on Date field
Calendar:
Load
  Distinct
  Date,
  Month(Date)             as LessonMonth,
  Year(Date)              as LessonYear
Resident
  RawData
;  

重新加载上面的脚本后,您的表达式将只是 sum( OverallDuration ) 您可以在下面的数据透视表 table 中看到结果:

总持续时间为 06:30 小时,Math02:00 小时:

您的数据模型将如下所示:

我喜欢将我的日历数据单独保存 table,但如果需要,您可以将月份和年份字段添加到主 table