Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
qw_jakub
Partner - Contributor III
Partner - Contributor III

Actual average date interval based on user selection

Hello,

suppose I have a table with a Start and End date.

I have a calendar table interval-matched to these two dates.

The user chooses a month -> all intervals which intersect with this month are filtered in.

I need to calculate the average interval length on these simple rules:

  • if the End of the interval is smaller than the biggest date selected by the user (the end of the month) - take the actual day difference: End - Start
  • if the interval did not yet come to the end (last day of the month selected by the user < End) - take the difference between the maximal selected day  and the Start : Last Day of the selected Month - Start

I can't seem to figure it out how to compare the End of each row with the Maximum selected day.

For example this:

=avg(if(max(Date) >= End, End - Start, max(Date) - Start))

does not work.

Bellow is an example for how it should work. The same example is in the attached files.

Thank is advance for any helpful comments.

StartEndUser Selects January 2013
1.1.201315.2.201330
10.1.201315.2.201321
1.1.201315.1.201314
1.1.201328.2.201330
1.2.201315.2.2013
1.1.201315.1.201314
15.1.201331.1.201316
15.2.201328.2.2013
15.1.201315.2.201316
25.1.201315.2.20136
Avg =18,375
1 Solution

Accepted Solutions
er_mohit
Master II
Master II

Try this

avg(if(max(TOTAL Date)>=End,(End-Start),max(TOTAL Date) - Start))

View solution in original post

3 Replies
tresesco
MVP
MVP

May be like attached sample?

Update:

For total average work properly, tweak the expression a bit, put avg() around the existing expression like:

=Avg(Aggr(avg(if(max(total Date) >= End,  End - Start, max(total Date) - Start)),Dimension) )

er_mohit
Master II
Master II

Try this

avg(if(max(TOTAL Date)>=End,(End-Start),max(TOTAL Date) - Start))

qw_jakub
Partner - Contributor III
Partner - Contributor III
Author

Thank you both for helpful answers!

I have based my solution on Mohit's solution. The TOTAL keyword was the key missing in my previous attempts.

I had to solve one more problem: because I did the average difference on two date fields which I also used in an intervalmatch without generating a synthetic key, my expression was calculated only on these distinct combination of dates from the interval table.

I have solved it by generating copies of the two date fields in the main table, which I used for the  calculations.