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

问题描述 投票:0回答:1

我想找到不同时间段的总时长(以小时为单位)。即。一旦添加了“十月”之类的过滤器,它应该向我显示该月的总时间。我要将多个参与者的重复课程计为1课。即。教授课程的时间。

 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,然后还可以深入到月份和日期。在此先感谢

qlikview qliksense qlik-expression
1个回答
0
投票

可以建议您创建另一个包含不同值的表(假定唯一组合为Date <-> Subject

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

OverallDuration表链接到RawData表(它自身也链接到Calendar表),这意味着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 ),您可以在下面的数据透视表中看到结果:

Result Pivot Table

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

Math subject selected

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

Data Model

我希望将日历数据保存在单独的表中,但如果需要,您可以将月和年字段添加到主表中

© www.soinside.com 2019 - 2024. All rights reserved.