Forum Discussion

ccolletti's avatar
ccolletti
Copper Contributor
Aug 24, 2023

Finding the difference between more than 2 dates with a matching name from a different column

Hi All,

 

I've been searching for a way to calculate the average difference between order dates for each of my customers. In my data I have one sheet with all my customers and their orders.

 

Currently I have a second sheet that I have done the UNIQUE function to to filter through my sales sheet and give me each customers name. I want to use this individual name to build a formula that will only pull the dates for the specific customer and give me the difference between the order dates, so I have the average amount of time between orders. 

 

I've attempted IF's, VLOOKUP's along with the DATEDIF function, however it looks like it's only good for 2 dates. 

 

Is there a way for me to get the difference with more than 2 dates based off if the customer is as an example Customer 1?

 

Example data:

AccountPurchase Date
Customer 11/2/2023
Customer 21/15/2023
Customer 31/29/2023
Customer 24/2/2023
Customer 44/3/2023
Customer 14/6/2023
Customer 34/17/2023
Customer 46/20/2023
Customer 56/30/2023
Customer 27/12/2023
Customer 17/20/2023
Customer 47/30/2023
Customer 38/20/2023

 

Thank you all for your help!

  • ccolletti 

    To obtain for "Customer 1":

     

    =LET(
        dates, FILTER(Purchase, Account = "Customer 1"),
        diff, IFERROR(DROP(dates, 1) - dates, ""),
        r, ROWS(diff),
        IF(r = 1, "-", AVERAGE(diff))
    )

     

    To do all 5 in one shot:

    =LET(
        uAccount, SORT(UNIQUE(Account)),
        AvgDays, LAMBDA(a, v,
            LET(
                dates, FILTER(Purchase, Account = v),
                diff, IFERROR(DROP(dates, 1) - dates, ""),
                r, ROWS(diff),
                result, IF(r = 1, " - ", AVERAGE(diff)),
                VSTACK(a, HSTACK(v, result))
            )
        ),
        REDUCE({"Account", "Avg days"}, uAccount, AvgDays)
    )
  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    ccolletti 

    To obtain for "Customer 1":

     

    =LET(
        dates, FILTER(Purchase, Account = "Customer 1"),
        diff, IFERROR(DROP(dates, 1) - dates, ""),
        r, ROWS(diff),
        IF(r = 1, "-", AVERAGE(diff))
    )

     

    To do all 5 in one shot:

    =LET(
        uAccount, SORT(UNIQUE(Account)),
        AvgDays, LAMBDA(a, v,
            LET(
                dates, FILTER(Purchase, Account = v),
                diff, IFERROR(DROP(dates, 1) - dates, ""),
                r, ROWS(diff),
                result, IF(r = 1, " - ", AVERAGE(diff)),
                VSTACK(a, HSTACK(v, result))
            )
        ),
        REDUCE({"Account", "Avg days"}, uAccount, AvgDays)
    )
    • ccolletti's avatar
      ccolletti
      Copper Contributor
      Thank you so much Patrick2788! That worked for me!

      I used the first one to get the average for each customer individually.

Resources