Forum Discussion
CycleWyze
Sep 08, 2024Copper Contributor
I cant seem to figure out this formula
Im trying to get the average time between customer orders. I was thinking of using a combination of AVERAGE, DATEDIF and the next formula is where im struggling. I have tried XLOOKUP but this on...
Patrick2788
Sep 08, 2024Silver Contributor
If you have access to the newly released GROUPBY function, it can make short work of this request.
=LET(
avg_days, LAMBDA(arr,
LET(
k, ROWS(arr),
sorted, SORT(arr),
IF(k = 1, @arr, AVERAGE(DROP(sorted, 1) - DROP(sorted, -1)))
)
),
GROUPBY(Table1[Name], Table1[Date], avg_days)
)First, 'avg_days' is created to be used with GROUPBY. This function will take the date(s) being returned by GROUPBY and do some subtraction. If only 1 date is found for a person, then the date is returned instead of the average. Sorting is done to ensure the dates being subtracted are in ascending order.
GROUPBY obtains a list of unique names from the table and takes in the date as the value -- the avg. days Lambda is applied to each return.