Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
MAAbdullah_47
Helper V
Helper V

Knowing The Number Of Days Between 1st or Nth Orders for different customers

Dear Experts

 

  I need to know the Average Number of days Between Order (n) and Order (n+1) for all customers not for Particuler Customer here is an example:

Assuming  we have (175) customers they have a range of orders requested BTW (1 - n) orders, we need only Up to (10) Orders Maximum to calculate the average No of Days BTW (1-10) as the following:
From Ordder No                     To  Order No                  No Of Customers              Average No Of Days BTW Orders
     1                                                 2                                      100                                     22.3
     2                                                 3                                        70                                      33.3
     3                                                 4                                        65                                     10.7
,

,

,

   9                                                  10                                       40                                        15.3 

I attached PBix Sample with this thread.

https://drive.google.com/file/d/1PRJZRipQ5n_RYGUwFRe1zMpZqri-7wdV/view?usp=sharing 

1 ACCEPTED SOLUTION
daxer-almighty
Solution Sage
Solution Sage

@MAAbdullah_47 , please make sure you update the measure on your end with the latest changes. Otherwise, it'll not work correctly if there are more dimensions than the ones we've been using.

View solution in original post

28 REPLIES 28
daxer-almighty
Solution Sage
Solution Sage

@MAAbdullah_47 , please make sure you update the measure on your end with the latest changes. Otherwise, it'll not work correctly if there are more dimensions than the ones we've been using.

daxer-almighty
Solution Sage
Solution Sage

I've found an issue with the measure (which manifests itself when there are more dimensions than the ones we've used so far). I've updated the measure in the same file, so you can use the same link as above. You have to create a column that stores exactly what I've said, not what you invent, @MAAbdullah_47. Once again: for each customer you have to number orders in their chronological order starting from 1. Do anything else and I can't guarantee the correctness of the measure.

Thank you @daxer-almighty  I'm really appreciated your help, Ok Let me check and get back to you but I think your solution is correct by at least 90%.

daxer-almighty
Solution Sage
Solution Sage

It's best calculated in Power Query, to give you a hint...

Hi @daxer-almighty  again

I created custom column In Power query consist of OrderDate+OrderNo+CustomerNo

In one string how I can do it based on event sequencing?

daxer-almighty
Solution Sage
Solution Sage

Here's a link to a PBI report that implements a solution. You should very carefully examine the file and the formulas to understand how it all works. Be especially careful to understand relative and absolute Order Sequence Numbers.

 

https://1drv.ms/u/s!ApyQEauTSLtOgY9D2FS2kAShjQvcww?e=lV4Vee

Hi @daxer-almighty  I think this post can help me in brigning the Sequence No : https://www.sqlbi.com/articles/numbering-sequence-of-events-in-dax/ 

 

I'll chack and get back to you.

 

Hi @daxer-almighty  I think the solution is good but the problem how to get the (

AbsoluteOrderSeqNo) this is the only trick , how I can calculate it?

Hi @daxer-almighty  I'm appreciating your help let me examin the solution carefully and get back to you as soon as I understand the complete picture , thank you my friend.

 

Hi @daxer-almighty  One question How you bring the (AbsoluteOrderSeqNo) to Orders I mean this column is it calculated colomn or what is it exactly ? 

Well, this column starts with 1 for each customer and numbers orders in their chronological order. Calculate it in whatever way you want.

Ok I'll try to find a way for that, Thank you , I'll get back to you.

 

 

Fowmy
Super User
Super User

@MAAbdullah_47 

Please read my comments. You need to provide an example using the data that is in the attachment you shared.

________________________

If my answer was helpful, please click Accept it as the solution to help other members find it useful

Click on the Thumbs-Up icon if you like this reply 🙂


Website YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Hi @Fowmy 

 

Please check the measure below posted by @Anonymous  , the problem I don't know how to call it, did you get the point?

 

// Assuming that there's:
// 1. a Customers dimension,
// 3. a Dates dimension (for the order dates),
// 4. an Orders fact table.
// The Orders fact (*-side) joins to Dates[Date]
// dimension (1-side) on Orders[OrderDate] and to
// Customers[CustomerID] on Orders[CustomerID].
// Orders must also have a column [Order No]
// which will number orders (within each customer)
// from 1 to N. All the ID columns in the fact table
// and dimensions will, of course, be hidden.
// The very last N available for a customer will,
// of course, be different among customers as one
// can't reasonably expect that all customers have
// placed the exact number of orders. Once this
// setup is in place, you can write a measure that will
// return the average number of days between [Order No] = n
// [Order No] = n+1 (all within the current context!).

[Avg No of Days To Next Order] =
// First, need to make sure that the column
// Orders[Order No] is filtered. If there's
// a filter on [Order No] = 1, then we'll be
// calculating the average num of days between
// the order numbers 1 and 2. If there's a filter
// on order number 10, the measure will return
// the avg number of days between order number
// 10 and 11 and so on...
if( hasonefilter( Orders[Order No] ),

var __orderNumber = selectedvalue( Orders[Order No] )
var __avg =
averagex(
Orders,
// For the current order find the next one
// (within the same customer AND THE CURRENT
// CONTEXT!) and caclulate the difference in
// the order dates.
var __currentOrderDate = Orders[OrderDate]
return
calculate(
// Get the next order for the same
// customer if there exists such an
// order. If not, BLANK will be returned
// and hence such a customer will not
// participate in the calculation of
// the average, as required. Please note
// that this calculation fully respects
// the current context, so based on the
// filters there might not be an (n+1)th
// order for the customer, even though it
// will exist in a modified (broader) context.
var __nextOrderDate =
CALCULATE(
SELECTEDVALUE( Orders[OrderDate], BLANK() ),
Orders[Order No] = __orderNumber + 1
)
return
DATEDIFF(
__currentOrderDate,
__nextOrderDate,
DAY
)
)
)
)
return
__avg

)

 

Thank you @Fowmy  I need that out put, I don't have it in the (pbix) file, I need measures to achive this 

Please back to the earliest discussion with Mr. @Anonymous  he send me a measure to do this but I need to call this measure that calculate the average days BTW 2 Orders but the problem I don't know how to call this measure , I did post asking for your help to achive the results.


Fowmy
Super User
Super User

@MAAbdullah_47 

Can you provide an example based on the actual data that you have in your file with the expected result?

________________________

If my answer was helpful, please click Accept it as the solution to help other members find it useful

Click on the Thumbs-Up icon if you like this reply 🙂


Website YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thank you @Fowmy 

Here is the expected output:

MAAbdullah_47_0-1607505430470.png

 

Please note the Order No means the series of the order here is an example:

Order Number                Series
-----------------                  ----          
A                                           1

B                                            2

D                                           3

N                                           4

 

Thank you 

 

MAAbdullah_47
Helper V
Helper V

Hi @Member123456 

Still I didn't get the solution , let me know what do you think , Mr. @Anonymous  appreciating his initiative for helping but he not welling in adding further assitance , I tried his solution it is not working because the order number is string , he told me you have to call the measure , I ask him how to call the measure but he never respond.
Thank you.

MAAbdullah_47
Helper V
Helper V

Hi @Anonymous  again, can you show me at least how to call the measure above? 

Anonymous
Not applicable

Mate, this is exactly what it means in my code 😡 Please read this once again with a focus. Thanks.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.