In either the Data Load editor or a measure in a table in Qlik Sense I am trying to count the number of distinct results for each UserID before the Result = "Cat" occurs.
Source Table
| UserID | Date | Result |
|---|---|---|
| A | 2/17/22 | Goat |
| A | 2/18/22 | Goat |
| A | 2/19/22 | Dog |
| A | 2/20/22 | Cat |
| A | 2/21/22 | Dog |
| A | 2/22/22 | Cat |
| B | 2/19/22 | Dog |
| B | 2/20/22 | Cat |
Expected Result Table
| UserID | # Results Before Cat |
|---|---|
| A | 1.5 |
| B | 1 |
Explanation UserID = A would be 1.5 because it is an average of 2 distinct results (Goat & Dog before Cat) then another occurrence of 1 Dog before Cat UserID = B would be 1 since there was only one distinct Result before Cat
Have a look at the annotated script below.
Once ran the output will be: