Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

If Statement in Script Counting dup values

I have the following IF Statement in my script

First 100.

IF(Reason = 'Go',1,0) as [GoReason_Count]

In my data I have a duplicate line and when I sum I'm getting 101 as my total. Is there a way to add something in my IF statement to change to only add a 1 in a distinct ID value?

ID 1 has a duplicate line. I realize I could load only distinct values into my query but trying to align with my chart expression

count({<Reason={[Go]}>}distinct ID)

GoReason_CountID
Total4
21
12
13

Would like to see...

GoReason_CountID
Total3
11
12
13
1 Solution

Accepted Solutions
Mark_Little
Luminary
Luminary

HI,

Make sure when you load you table by ID,

Then something like

  IF(RowNo =1,

          IF(Reason='GO',1,0),

          IF(ID = PREVIOUS(ID),

               0,

               IF(Reason='GO',1,0)

             )

     )  as [GoReason_Count]

Mark      

View solution in original post

5 Replies
swuehl
MVP
MVP

Could you explain your data model a bit more detailed?

You can try to create the GoReason_Count flag in the dimension table with distinct ID values, if that is feasible, but without knowing your data model, it's hard to give any specific advice.

Anonymous
Not applicable
Author


One table with ID being the primary key. This table has dup primary keys and is throwing my sum values off.

swuehl
MVP
MVP

Well, then ID is not a primary key, right?

You may achieve a script that creates a GoReason_Count flag only once for a single ID, but since the flag value is not unambiguos per ID then, you might run into trouble when you are filtering records (removing the flag though you would need to consider it in your sum).

Anonymous
Not applicable
Author

True.
How would one go about creating a flag for a single ID in the script?

Mark_Little
Luminary
Luminary

HI,

Make sure when you load you table by ID,

Then something like

  IF(RowNo =1,

          IF(Reason='GO',1,0),

          IF(ID = PREVIOUS(ID),

               0,

               IF(Reason='GO',1,0)

             )

     )  as [GoReason_Count]

Mark