【问题标题】:Qlik Sense - Displaying data by time series aggregated by a given time periodQlik Sense - 按给定时间段聚合的时间序列显示数据
【发布时间】:2016-10-25 06:32:54
【问题描述】:

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

日期客户标志 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

背后的逻辑:我需要按周计算在该周(包括前一周)具有标志 = 1 的 DISTINCT 客户。因此,第 40 周将显示第 39 周和第 40 周的不同计数。类似地,第 41 周将显示第 40 周和第 41 周的不同计数,依此类推。

如果有人可以为上述内容建议 Qlik Sense 表达式,我将不胜感激?

【问题讨论】:

    标签: qliksense


    【解决方案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();
    

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

    而Dates 表将包含以下数据:

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

    所以后面的表达就很简单了:

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

    结果是:

    附:屏幕截图来自 QlikView,但相同的加载代码和表达式也可以在 QS 中使用

    【讨论】:

    • 谢谢斯特凡!我正在查看结果并将尽快更新线程。
    • Stefan - 最终结果似乎是错误的,并且不符合所需的逻辑。如果您查看第 41 周,应该有 3 个不同的计数,因为它还包括第 40 周。
    • 对不起,我的错。 Week(Date - 7) as Week 实际上应该是 Week(Date + 7) as Week(+ 而不是 -)。我会更新答案
    【解决方案2】:

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

    //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 与, 分隔

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 2018-02-28
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      相关资源
      最近更新 更多