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

Average of interval of days

Hi,

I have a data set like this.

CustomerSales DateAmount
Cust101/01/2017100
Cust101/15/2017200
Cust102/20/2017100
Cust103/11/2017450
Cust202/20/2017120
Cust203/12/2017450
Cust204/11/2017560
Cust204/29/2017780
Cust205/20/2017120

What i want is to find the average of interval of sales dates for each customer.

like for Cust1 the interval of days between sales dates are

01/01 - 01/15 = 15

01/15 - 02/20 = 25

02/20 - 03/11 = 21

so the average would be

15 + 25 + 21 / 3 = 20.33

Is it possible?

Thanks

Jean

4 Replies
Nicole-Smith

FYI - The intervals you have above are not correct...there are 36 days between 1/15 and 2/20, and 19 between 2/20 and 3/11.

I would calculate the difference in the load script so you can just usage avg() on your charts:

Data:

LOAD *, IF(Customer = Previous(Customer), [Sales Date] - Previous([Sales Date]), 0) AS [Sales Date Diff];

LOAD * INLINE [

    Customer, Sales Date, Amount

    Cust1, 01/01/2017, 100

    Cust1, 01/15/2017, 200

    Cust1, 02/20/2017, 100

    Cust1, 03/11/2017, 450

    Cust2, 02/20/2017, 120

    Cust2, 03/12/2017, 450

    Cust2, 04/11/2017, 560

    Cust2, 04/29/2017, 780

    Cust2, 05/20/2017, 120

];

This gives us:

Customer Sales Date Amount Sales Date Diff
Cust101/01/20171000
Cust101/15/201720014
Cust102/20/201710036
Cust103/11/201745019
Cust202/20/20171200
Cust203/12/201745020
Cust204/11/201756030
Cust204/29/201778018
Cust205/20/201712021

And then you can just use avg([Sales Date Diff]) on a chart.

I've attached an example file doing the same.

jduluc12
Creator
Creator
Author

The problem is that i do not have access to the load script.

But thanks anyway to help me out here.

Nicole-Smith

I think this expression should do the trick:

avg(aggr([Sales Date]-above([Sales Date]), Customer, [Sales Date]))

Example file is attached.

jduluc12
Creator
Creator
Author

I am trying it out.