Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to count distinct ignoring a single dimension?

I have a table which looks like this:

Claim        State     Payment_Type     Amount

  1              PA          Expert                   10

   1             PA          Loss                    15

  2              PA          Expert                   5

   2             PA          Loss                    25

    2            PA          Legal                    20

I have a stacked bar chart with state on the X-axis and payment_type stacked on the y axis.  I want my expression to be the average amount per claim.  So, for Payment_Type = Legal it should show (20 + 0) / 2 = 10 since there are 2 claims in PA.

I tried sum(Amount) / Count( distinct Claim) but the denominator only counts rows with a value, so for Legal this would yield (20) / 1 = 20.

I need to somehow ignore the Payment_Type dimension.

1 Solution

Accepted Solutions
Not applicable
Author

I see what's going on... my table doesn't include a record for each claim and payment type. So if there is no legal amount for claim #1 then there is no record for it. I edited my original post to remove that record from the sample data table.

View solution in original post

8 Replies
sunny_talwar

I see 10 for legal:

Capture.PNG

maxgro
MVP
MVP

try this

Avg(alt(Amount,0))

or

sum(Amount) / Count(Claim)

Not applicable
Author

Hmmm... perhaps my amount for record #3 is null and yours is zero? 

sunny_talwar

This is my script:

Table:

LOAD * Inline [

Claim, State, Payment_Type, Amount

1,      PA,     Expert,         10

1,      PA,     Loss,           15

1,      PA,     Legal,         

2,      PA,     Expert,         5

2,      PA,     Loss,           25

2,      PA,     Legal,          20

];

So mine is actually null. Also we are doing a Distinct Count of Claim which is not null.

Not applicable
Author

Avg(alt(Amount,0)) did not give me the desired answer either. I didn't mention this in the original post, but I do have a few other fields such as injury type which may also be limited. 

maxgro
MVP
MVP

and could you post some data with other fields?

1.png

jonas_rezende
Specialist
Specialist

Hi, Justin Bartoszek.

The result was reproduced as its expression. See image below.

SomaBarra.JPG

Note:The expression count (xxx) acts on the Claim field, so does not interfere with null results. And Sum(Amount) equal null, will be considered zero.

If you want to transform null to zero, make as follows.

Table1:

LOAD Claim, State, Payment_Type, if(isnum(Amount),Amount, 0) as Amount Inline [

Claim, State, Payment_Type, Amount

1, PA, Expert, 10

1, PA, Loss, 15

1, PA, Legal, -

2, PA, Expert, 5

2, PA, Loss, 25

2, PA, Legal, 20

];

I hope this helps!

Not applicable
Author

I see what's going on... my table doesn't include a record for each claim and payment type. So if there is no legal amount for claim #1 then there is no record for it. I edited my original post to remove that record from the sample data table.