Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Calculate Average - only if not null

Please see the file attached.

I am trying to calculate average (see text object) number of days between Date1 and Date2 but only for items that have both dates

Not sure how to do it   (set analysis, if statement???)

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Maybe

=Avg(Date2-Date1)

If one of the dates is not a number, the date calculation returns NULL, and Avg() does not consider NULL

View solution in original post

6 Replies
Anil_Babu_Samineni

Seems working, Why you want to do in set analysis?

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
sunny_talwar

May be this

='Average' & chr(13) & num(sum(Date2-Date1)/Count({<Date1 = {"=Len(Trim(Date1)) > 0"}, Date2 = {"=Len(Trim(Date2)) > 0"}>}Item),'0.0') & ' days'

or if they are true nulls, then this

='Average' & chr(13) & num(sum(Date2-Date1)/Count({<Date1 = {'*'}, Date2 = {'*'}>}Item),'0.0') & ' days'

swuehl
MVP
MVP

Maybe

=Avg(Date2-Date1)

If one of the dates is not a number, the date calculation returns NULL, and Avg() does not consider NULL

Anonymous
Not applicable
Author

Hi,

May be like this

='Average' & chr(13) &

num(sum({<Date2-={''},Date1-={''}>}Date2-Date1)/Count({<Date2-={''},Date1-={''}>}Item),'0.0') & ' days'

swuehl
MVP
MVP

Also note that Item 17 appears two times in your sample data set.

It won't show up in your bar chart, because your expression

=Date2-Date1

does not return an unambiguous answer for this item. The suggested average calculations will consider this item though, if you want to exclude it, you can also use

=Avg(Aggr( Date2-Date1, Item))

Not applicable
Author

Thank you Stefan. The simplest solution is often the most accurate.