Calculate average interval between customer orders

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I am using Power Query and have a table containing customers along with corresponding order dates. I would like to calculate the average interval (in months) between orders for each given customer. For instance, is the average time span 1, 2, 3 months etc.

The dates will only represent the fiscal period so will be things like 5/1/2016, 7/1/2016, 10/1/2016 and so forth.

What would be the best way to do that?

This will be used on a table with about 1 million rows so as efficient a solution as possible is preferred.

Thanks
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,

Source Table:

Excel 2010
ABC
1CustomerOrder Date
2A01/01/2001
3B01/01/2001
4C02/01/2001
5A02/01/2001
6B01/02/2001
7A01/03/2001
8B01/03/2001
9C01/04/2001
10A01/04/2001
11B01/04/2001
12
Sheet1



Results:

Excel 2010
EFG
1CustomerOrder DateAvgDuration
2A[Table]30
3B[Table]30
4C[Table]89
5
Sheet1




Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"Order Date", each _, type table}}),
    AvgFunc = (CustTable as table) as duration =>
        let
            SortDate = Table.Sort(CustTable, {{"Order Date", Order.Ascending}}),
            AddIndex = Table.AddIndexColumn(SortDate, "Index", 0, 1),
            DifCol = Table.AddColumn(AddIndex, "Duration", each try AddIndex[Order Date]{[Index]}-AddIndex[Order Date]{[Index]-1} otherwise 0),
            RemoveZero = Table.SelectRows(DifCol, each ([Duration] <> 0)),
            TypeDuration = Table.TransformColumnTypes(RemoveZero,{{"Duration", type duration}}),
            CalcAvg = List.Average(TypeDuration[Duration])
        in
            CalcAvg,
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "AvgDuration", each AvgFunc([Order Date])),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"AvgDuration", type duration}}),
    #"Extracted Days" = Table.TransformColumns(#"Changed Type1",{{"AvgDuration", Duration.Days, Int64.Type}})
in
    #"Extracted Days"

I've left all the columns in so you can delete and format as required.

Also the result is in days so you can divide by 30/31 etc.... but I've left that up to you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top