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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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

 

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] )
)

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

 

 

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

 

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.