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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TheGreatestGoat
Frequent Visitor

Count Of Orders By Customer ID

Hi Guys,

 

Tried a few solutions from the boards and none of them seem to be working for me. 

I have the following sales query, it's broken down in rows by product_id so there are many reoccurring values of order_name and customer_id :

sales-query-image.jpg

 

I am trying to create a calculated column for the number of orders per customer. Currently I have a measure which works correctly and returns the right values:

Total Orders = DISTINCTCOUNT('Sales Fact Table'[order_id])

 

However I need this to be a column as I want to use 'order count' as a slicer. 

I have tried this:

Order Count =
CALCULATE (
    DISTINCTCOUNT ( 'Sales Fact Table'[OrderID] ),
    FILTER (
        ALL ( 'Sales Fact Table' ),
        [customer_id] = MAX ( 'Sales Fact Table'[customer_id] )
    )
)

 
This does return values, however they are incorrect.

Anyone know what the correct formula should be? 

Thanks! 

1 ACCEPTED SOLUTION

@TheGreatestGoat

 

When is Use the formula I proposed above I get the following results (which I expect)

 

Do you expect something else

 

counnt orders.png

 


Regards
Zubair

Please try my custom visuals

View solution in original post

7 REPLIES 7
Zubair_Muhammad
Community Champion
Community Champion

@TheGreatestGoat

 

Try with following

 

Order Count =
CALCULATE (
    DISTINCTCOUNT ( 'Sales Fact Table'[OrderID] ),
    ALLEXCEPT ( 'Sales Fact Table', 'Sales Fact Table'[customer_id] )
)

Regards
Zubair

Please try my custom visuals

Hey @Zubair_Muhammad,

Thanks so much for your reply, it helps a lot!

Tried this out but didn't work. Values were 10 or 20 times expected.

I also tried swapping around order_id and customer_id in your formula like this:

Customer Product Count = CALCULATE ( DISTINCTCOUNT ( 'Sales Fact Table'[customer_id] ), ALLEXCEPT ( 'Sales Fact Table', 'Sales Fact Table'[order_id] ) )

 

This was actually closer and returned the total number or products per customer. I.e returned the distinct values for product_id 

I.e. Customer A purchased 49 products. However it did not return the number of orders which was 14

This is a useful number to have as well, but not quite what I was after. I feel like the solution is close but not quite there. 

Any other ideas? 

Hi

 

Could you copy paste some data with expected results.

 

If data is in a format (copiable) like following it becomes easy to find a solution

 

 

InvoiceDateProductPriceExpecetd Last Price
Sunday, October 1, 2017Product A                      15                                   4
Wednesday, November 1, 2017Product B                      10                                   5
Thursday, March 1, 2018Product A                         8                                   4
Friday, June 1, 2018Product B                         5                                   5
Friday, June 1, 2018Product A                         4                                   4

 

 


Regards
Zubair

Please try my custom visuals

Hey @Zubair_Muhammad

Here is an exceprt of data for two customers. Thanks for your help 🙂

purchase_dateproduct_idorder_idcustomer_id
24/05/201410069700304746911468682
24/05/201420069700292586911468682
24/05/2014111169700284266911468682
24/05/201480069700300906911468682
24/05/201410069700295786911468682
24/05/2014200069700288106911468682
27/05/2014110069700983786911468682
1/06/201440069701861226911468682
1/06/201490069701865066911468682
1/06/201410069701869546911468682
5/09/2013300069667210346904791178
5/09/2013200069667210346904791178
5/09/2013100069667210346904791178
7/07/201490069709562346904791178
8/07/201480069709705066904791178
10/07/2014110069709997546904791178
11/07/201440069710254826904791178
29/01/201530069805516906904791178
29/01/201510069805511146904791178
29/01/201520069805516906904791178
29/01/201520069805511146904791178
29/01/201520069805511146904791178
29/01/20155006169805511146904791178
29/01/2015130069805516906904791178
11/02/20152000069809788906904791178
11/02/2015617469809788906904791178
11/02/2015894669809788906904791178

@TheGreatestGoat

 

When is Use the formula I proposed above I get the following results (which I expect)

 

Do you expect something else

 

counnt orders.png

 


Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Have similar scenario I am trying to figure out.  What if I wanted to take the count of orders where the Product ID was lets say less than 1000 in the example provided.  This would result in customer 6911458582 having 7 (instead of 10) in the example.

 

Appreciate any help in what additional logic should be applied.

Hi @Zubair_Muhammad

 

I'm sorry your solution does work indeed! 

 

I got confused, I had to turn it to 'Do Not Summarize Data' in my table!

 

Thanks so much for your help!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.