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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
ManjunathaEP
Helper II
Helper II

Count of orders for Return and New Customers

Hi, Thank you in advance for looking into my sample data and providing a DAX for it

 

I have simple sample data as mentioned below and I would like to calculate the count of orders for repeat customers

CustomerOrder NoYearAmount
ABC1201910
XYZ2201920
YRP3201934
HM4201956
XYZ11202067
ABC22202044
YRP33202046
HYD44202066
ABC55202077
XYZ66202067

 From the above table there are 3 customers (XYZ, ABC and YRP) are returned in 2020 compared to 2019. 

I would like to calculate the count of the orders for the above repeat customers in 2020 and the count of distinct orders=5

 

Please help me by providing suitable DAX for it

 

Regards,

Manjunatha EP

1 ACCEPTED SOLUTION

 

[# Repeat Customer Orders] =
var FirstDateOfCurrentPeriod = MIN( 'Calendar'[Date] )
var CustomersWhoBoughtBefore =
    CALCULATETABLE(
        // This calculation is correct on condition
        // that an entry in the fact table, call it
        // Sales, means a customer has bought something.
        // If this is not the case, you'll have to
        // adjust this condition.
        DISTINCT( Sales[CustomerID] ),
        // Please bear in mind that Calendar must 
        // be marked as a date table in the model
        // for this time-intel function to work OK.
        'Calendar'[Date] < FirstDateOfCurrentPeriod
    )
var Result =
    CALCULATE(
        [# Orders],
        KEEPFILTERS( CustomersWhoBoughtBefore )
    )
return
    Result

 

The only change is to the period that you want to consider "prior." This time the prior period is everything from the beginning of time to the very last date before the start of the current period. All the conditions I named before must hold for this to work correctly.

View solution in original post

3 REPLIES 3
daxer-almighty
Solution Sage
Solution Sage

 

 

// My understanding of the task:
// Need to find out which customers
// bought something in the same
// period last year and then calculate
// the number of orders in the current
// period for these customers.
// To do the above correctly you have to
// have a Calendar table in the model.
// One-table model is NOT ENOUGH. Also,
// you should have a dimension called
// Customers that will hold all the customers
// in your model. Assuming the above is
// in place, you can write:

[# Orders] = DISTINCTCOUNT( Sales[Order No] )

[# Repeat Customer Orders] =
var CustomersWhoBoughtLY =
    CALCULATETABLE(
        // This calculation is correct on condition
        // that an entry in the fact table, call it
        // Sales, means a customer has bought something.
        // If this is not the case, you'll have to
        // adjust this condition.
        DISTINCT( Sales[CustomerID] ),
        // Please bear in mind that Calendar must 
        // be marked as a date table in the model
        // for this time-intel function to work OK.
        SAMEPERIODLASTYEAR( 'Calendar'[Date] )
    )
var Result =
    CALCULATE(
        [# Orders],
        KEEPFILTERS( CustomersWhoBoughtLY )
    )
return
    Result

 

 

Thank you for providing DAX, I have given the sample table. Actually, repeat customers or return customer means, a customer returned back and purchased a product. It means, If a customer purchased in 2018 and the same customer purchases in 2020, I need to treat that customer as Return/repeat customers. Then, I have to calculate the count of the orders of that customer in 2020.

In the given table, please assume one of the customers (PS) was placed an order in 2018 and then returned in 2020, I have to considered as return/repeat customer in 2020 and need to calculate the count of orders for the year 2020.

 

 

[# Repeat Customer Orders] =
var FirstDateOfCurrentPeriod = MIN( 'Calendar'[Date] )
var CustomersWhoBoughtBefore =
    CALCULATETABLE(
        // This calculation is correct on condition
        // that an entry in the fact table, call it
        // Sales, means a customer has bought something.
        // If this is not the case, you'll have to
        // adjust this condition.
        DISTINCT( Sales[CustomerID] ),
        // Please bear in mind that Calendar must 
        // be marked as a date table in the model
        // for this time-intel function to work OK.
        'Calendar'[Date] < FirstDateOfCurrentPeriod
    )
var Result =
    CALCULATE(
        [# Orders],
        KEEPFILTERS( CustomersWhoBoughtBefore )
    )
return
    Result

 

The only change is to the period that you want to consider "prior." This time the prior period is everything from the beginning of time to the very last date before the start of the current period. All the conditions I named before must hold for this to work correctly.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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