计算特定字段之前出现的次数然后重置

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

在数据加载编辑器或 Qlik Sense 表格中的度量中,我尝试在结果 =“Cat”出现之前计算每个用户 ID 的不同结果的数量。

来源表

用户ID 日期 结果
A 2/17/22 山羊
A 2/18/22 山羊
A 2/19/22
A 2/20/22
A 2/21/22
A 2/22/22
B 2/19/22
B 2/20/22

预期结果表

用户ID 猫之前的#结果
A 1.5
B 1

说明 UserID = A 将为 1.5,因为它是 2 个不同结果的平均值(山羊和狗在猫之前),然后又出现 1 个狗在猫之前 UserID = B 将为 1,因为 Cat 之前只有一个不同的结果

qliksense qlik-expression
1个回答
0
投票

看看下面带注释的脚本。

运行后输出将是:

// Load directly from SO
WebData:
Load
    // create increment per UserID
    if(UserID <> Peek(UserID), 1, peek(Increment) + 1) as Increment,
    RecNo() as Record,
    UserID,
    Date, 
    Result
From
    [https://stackoverflow.com/questions/78189164/count-number-of-occurrences-before-specific-field-then-reset]
    (html, utf8, UserAgent is 'Mozilla/5.0', embedded labels, table is @1)
;

// per UserID find whish is the min Increment
// number for Cat records
MinIncrement:
Load
    min(Increment) as MinIncrement,
    UserID
Resident
    WebData
Where
    Result = 'Cat'
Group By
    UserID
;

// join back to the WebData
join (WebData) Load * Resident MinIncrement;


// find how many distinct Result values
// exists before the min Cat record
Temp1:
Load
    Count(distinct Result) as ResultCount,
    UserID
Where
    IncludeFlag = 1
Group By UserID
;
Load
    if(Increment < MinIncrement, 1, 0) as IncludeFlag,
    UserID,
    Result
Resident
    WebData
;

// join the MinIncrement value
// so we can have the record number
// of the Cat record (per UserID)
join

Load
    UserID,
    MinIncrement
Resident
    MinIncrement
;

// we dont need this table anymore
Drop Tables MinIncrement;

// calculate the final result
FinalResult:
Load
    UserID,
    (MinIncrement - 1) / ResultCount as FinalResult
Resident
    Temp1
;

// we dont need the following tables and fields anymore
Drop Table Temp1;
Drop Fields Increment, MinIncrement From WebData;
© www.soinside.com 2019 - 2024. All rights reserved.