Qlik Sense - 按给定时间段汇总的时间序列显示数据

时间:2016-10-25 06:32:55

标签: qliksense

我在Qlik Sense中有以下格式的数据:

Date       Customer Flag
2016-10-01   A       1
2016-10-01   B       0
2016-10-02   A       1
2016-10-02   C       1
2016-10-03   A       1
2016-10-03   B       1
2016-10-03   C       1
2016-10-05   C       1
2016-10-10   A       0
2016-10-10   B       1
2016-10-11   C       0

我想以下列格式在Qlik Sense的表格中显示这些数据:

#Week     Count Distinct Customer Where Flag is 1
39           2
40           3
41           3

背后的逻辑:我需要周计数的DISTINCT客户,其中该周的Flag = 1,包括前一周。因此,此处第40周将显示第39周和第40周的不同计数。同样,第41周将显示第40周和第41周的不同计数。依此类推。

如果有人能为上述建议使用Qlik Sense表达式,我将不胜感激?

2 个答案:

答案 0 :(得分:1)

在我看来,这应该在脚本级别解决,而不是试图使表达式/ GUI复杂化(如果可能的话)

下面的脚本会创建另外的表Dates,其中包含Date - > Week链接。 Week可以用作正常维度。

RawData:
Load * Inline [
    Date      , Customer, Flag
    2016-10-01, A       , 1
    2016-10-01, B       , 0
    2016-10-02, A       , 1
    2016-10-02, C       , 1
    2016-10-03, A       , 1
    2016-10-03, B       , 1
    2016-10-03, C       , 1
    2016-10-05, C       , 1
    2016-10-10, A       , 0
    2016-10-10, B       , 1
    2016-10-11, C       , 0
];

// Generate week number from the existing dates
Dates_Temp:
Load 
    Date, 
    Week(Date)  as Week
Resident
    RawData
;   

Concatenate

// Generate previous week number from the existing dates
Load 
    Date, 
    Week(Date + 7)  as Week // Generate the previous week number
Resident
    RawData
;   

// The following code will remove week 42
// If week 38 need to be visible just ignore/delete 
// the script below --->

// Find the max week from the generated weeks
MaxWeek:
Load
    max(Week) as maxWeek
Resident
    Dates_Temp
;   

// Load the min week in vMaxnWeek variable
let vMaxWeek = peek('maxWeek');

// This table is not needed anymore
Drop Table MaxWeek;

NoConcatenate

// The new Dates table will contain all weeks apart from week 42
Dates:
Load
    *
Resident
    Dates_Temp
Where
    Week <> $(vMaxWeek)
;

Drop Table Dates_Temp;

let vMaxWeek = null();

执行脚本后,数据结构如下所示:

enter image description here

Dates表将包含以下数据:

enter image description here

正如您所看到的那样,每个日期都有两周的数字。 (只有第39周是单一记录,因为从数据中删除了第38周。不确定是否需要)

所以在此之后表达非常简单:

= count( {< Flag = {1} >} distinct Customer)

结果是:

enter image description here

P.S。屏幕截图来自QlikView,但同样的加载代码和表达式也可以在QS中使用

答案 1 :(得分:0)

作为Stefan回答的替代方案,Qlik Sense在日期方面有一些很棒的功能。以下内容可以插入到您的脚本中,并在选择器屏幕上生成可以使用的其他字段。

CurrencyDatePicker

//AutoCalendar
[Calendar]: 
  DECLARE FIELD DEFINITION Tagged ('$date')
FIELDS
  Dual(Year($1), YearStart($1))                                         AS [Year] Tagged ('$axis', '$year')
  ,Dual('Q'&Num(Ceil(Num(Month($1))/3)),Num(Ceil(NUM(Month($1))/3),00)) AS [Quarter] Tagged ('$quarter')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3)),QuarterStart($1))     AS [YearQuarter] Tagged ('$axis', '$yearquarter')
  ,Month($1)                                                            AS [Month] Tagged ('$month')
  ,Dual(Year($1)&'-'&Month($1), monthstart($1))                         AS [YearMonth] Tagged ('$axis', '$yearmonth')
  ,Dual('W'&Num(Week($1),00), Num(Week($1),00))                         AS [Week] Tagged ('$weeknumber')
  ,Date(Floor($1))                                                      AS [Date] Tagged ('$date')
  /*User added date components*/
  ,Dual(Year($1), if(Year($1)=Year(today()),YearStart($1),null))        AS [ThisYear] Tagged ('$axis', '$thisyear')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3)), if(Year($1)=Year(today()),QuarterStart($1),null)) 
                                                                        AS [ThisYearQuarter] Tagged ('$axis', '$thisyearquarter')
  ,Dual(Year($1)&'-'&Month($1)
        , if(Year($1)=Year(today()), monthstart($1),null))              AS [ThisYearMonth] Tagged ('$axis', '$thisyearmonth')
  ,Dual(Year($1), if(Year($1)=(Year(today())-1),YearStart($1),null))    AS [LastYear] Tagged ('$axis', '$lastyear')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3))
        , if(Year($1)=(Year(today())-1),QuarterStart($1),null))         AS [LastYearQuarter] Tagged ('$axis', '$lastyearquarter')
  ,Dual(Year($1)&'-'&Month($1)
        , if(Year($1)=(Year(today())-1), monthstart($1),null))          AS [LastYearMonth] Tagged ('$axis', '$lastyearmonth')
  ,Dual(date(MonthStart($1),'MMM-YYYY')
        , if(Monthstart($1)=Monthstart(today()),Monthstart($1),null))   AS [ThisMonth] Tagged ('$axis', '$thismonth')
  ,Dual(date(MonthStart($1),'MMM-YYYY')
        , if(Monthstart($1)=Monthstart(addmonths(today(),-1)),Monthstart($1),null)) 
                                                                        AS [LastMonth] Tagged ('$axis', '$lastmonth')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3))
        ,if(QuarterStart($1)=QuarterStart(Today()),QuarterStart($1),null))  
                                                                        AS [ThisQuarter] Tagged ('$axis', '$thisquarter')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3))
        ,if(QuarterStart($1)=QuarterStart(addmonths(Today(),-3)),QuarterStart($1),null)) 
                                                                        AS [LastQuarter] Tagged ('$axis', '$lastquarter')
  ,Dual(date(MonthStart($1),'MMM-YYYY')
        ,if(QuarterStart($1)=QuarterStart(Today()),MonthStart($1),null)) AS [ThisQuarterMonth] Tagged ('$axis', '$thisquartermonths');

DERIVE FIELDS FROM FIELDS [CurrencyDate]
USING [Calendar] ;

这将使用与Stefan相同的表达式。

= count( {< Flag = {1} >} distinct Customer)

这样做的好处

  • 字段隐藏在下拉列表中,因此不要混乱字段选择
  • 可以在同一堆栈中处理多个日期,只需使用derive from fields
  • 添加到,分隔符