Forum Discussion
Calculate Days between OrderDate per Customer where Order date is in the same Column???
- Anonymous9 years ago
Sorry Matt,
Got it working not sure if it was me having the .Date on the date column and i selected CustomerID for the Values bit, but got rid of the .Date and ID so its like the columns below and done manual calculations to verify the data its outputing and its working now
So this is what i ended up with;
First Order = CALCULATE(FIRSTDATE(TableName[Date]),VALUES(Tablename[Date]))
Last Order = CALCULATE(LASTDATE(TableName[Date]),VALUES(Tablename[Date]))
Number of Orders = DISTINCTCOUNT(TableName[OrderID])
First to Last Date period = ((TableName[Last Order] - TableName[First Order])*1)
Today = Today()
Days Since Last Order = TableName[Today].[Date] - TableName[Order Date].[Date] * 1
Avg Order Period = divide(TableName[First to Last Date period],TableName[Number of Orders])
&
Difference = CALCULATE(MIN([Days Since Last Order]) - [Avg Order Period])
Then when Difference gets to 0 its time to call,
Seeing it like the above makes it so clear, Thanks Matt, Great Work !!!
Thanks,
J
I realise this seems easy, but it is not so easy - hope that makes you feel any better :-). Power Pivot is not really built to handle relative comparison between rows in a table. You can do it, but it is not super easy. But as I think about your problem in a different way, I think there may be an easier way to solve the problem that might meet your needs.
Break the problem into pieces
Using Adventure Works as an example, you could write measures like this. If you put customer name on a visual (say table) with these measures, I think it will give you what you need.
Last Order=CALCULATE(LASTDATE(Sales[OrderDate]),VALUES(Customers[CustomerKey]))
First Order=CALCULATE(FIRSTDATE(Sales[OrderDate]),VALUES(Customers[CustomerKey]))
Total Orders=DISTINCTCOUNT(Sales[SalesOrderNumber])
First to Last Date period=[Last Order] - [First Order]
Edit (sorry, previous formula was wrong): Avg Order Period=divide([First to Last Date period],[Total Orders] -1 )
Dont get me wrong though for our longest standing customers its working well,
Just how to make it see "Their" first order and then drop the -1 on the Average?
Thanks though this is excellent if it will work!
Was almost going to pull it into excel manually do the averages and then have a relationship between two data tables to show it in one graph visual.
Thanks,
Josh
- MattAllington9 years ago
Community Champion
You have lots of replies and thoughts here and I am not really sure the current status of open issues. Can you do a single reply explaining what open issues you have?
- Anonymous9 years agoNot applicable
Sorry Matt,
Got it working not sure if it was me having the .Date on the date column and i selected CustomerID for the Values bit, but got rid of the .Date and ID so its like the columns below and done manual calculations to verify the data its outputing and its working now
So this is what i ended up with;
First Order = CALCULATE(FIRSTDATE(TableName[Date]),VALUES(Tablename[Date]))
Last Order = CALCULATE(LASTDATE(TableName[Date]),VALUES(Tablename[Date]))
Number of Orders = DISTINCTCOUNT(TableName[OrderID])
First to Last Date period = ((TableName[Last Order] - TableName[First Order])*1)
Today = Today()
Days Since Last Order = TableName[Today].[Date] - TableName[Order Date].[Date] * 1
Avg Order Period = divide(TableName[First to Last Date period],TableName[Number of Orders])
&
Difference = CALCULATE(MIN([Days Since Last Order]) - [Avg Order Period])
Then when Difference gets to 0 its time to call,
Seeing it like the above makes it so clear, Thanks Matt, Great Work !!!
Thanks,
J
- MAAbdullah_475 years ago
Helper V
Wrong Solution MattAllington