Forum Discussion

EllieSiroco1's avatar
EllieSiroco1
New Member
1 year ago
Solved

Error when calculating Returning Customers

Ihave a PowerBi table called "orders-web2019" with the following dimensions: "Order date", "customer email" , and the following metric "Sales inc VAT".   I have written DAX to define a new calculat...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi EllieSiroco1 

     

    Please try this:

    Here I create a set of sample:

    Then add a calculated column:

    Returning Customers =
    VAR CurrentPeriodStart =
        DATE ( 2024, 11, 01 )
    VAR CurrentPeriodEnd =
        DATE ( 2024, 12, 31 )
    VAR _previousSales2 =
        CALCULATE (
            SUM ( 'orders-web2019'[Sales inc VAT] ),
            FILTER (
                ALLSELECTED ( 'orders-web2019' ),
                'orders-web2019'[customer email] = EARLIER ( 'orders-web2019'[customer email] )
                    && YEAR ( 'orders-web2019'[Order date] )
                        = YEAR ( EARLIER ( 'orders-web2019'[Order date] ) ) - 1
                    && MONTH ( 'orders-web2019'[Order date] )
                        = MONTH ( EARLIER ( 'orders-web2019'[Order date] ) )
            )
        )
    RETURN
        IF (
            'orders-web2019'[Order date] >= CurrentPeriodStart
                && 'orders-web2019'[Order date] <= CurrentPeriodEnd,
            IF (
                _previousSales2 > 0
                    && 'orders-web2019'[Sales inc VAT] > 0,
                "Returning",
                "New"
            )
        )
    

    The result is as follow:

     

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • speedramps's avatar
    speedramps
    1 year ago

    EllieSiroco1 

    Sorry I think I misunderstood you before.

    I think this simply solution will do what you you want ...

     

    Create 4 measures ... 

     

     

    Has NovDec2023 sales = 
    // create a temp file of dates in range 
    VAR mydates =  FILTER('Calendar', 'Calendar'[Date] >= DATE(2023,11,01) && 'Calendar'[Date] <= DATE(2023,12,31))
    RETURN
    // return 1 if there were any sales within date range
    CALCULATE(
    INT(NOT(ISEMPTY(Sales))),
    mydates
    )

     

     

     

     

     

    Has NovDec2024 sales = 
    // create a temp file of dates in range 
    VAR mydates =  FILTER('Calendar', 'Calendar'[Date] >= DATE(2024,11,01) && 'Calendar'[Date] <= DATE(2024,12,31))
    RETURN
    // return 1 if there were any sales within date range
    CALCULATE(
    INT(NOT(ISEMPTY(Sales))),
    mydates
    )

     

     

     

     

     

    New customers = 
    // create temp file of qualifiying customers  
    var mysubset =
    FILTER(VALUES(Sales[CustomerKey]), 
        [Has NovDec2023 sales] = 0 &&
        [Has NovDec2024 sales] = 1)
     RETURN
     // count the rows
     COUNTROWS(mysubset)  

     

     

     

     

     

    Returning customer = 
    // create temp file of qualifiying customers  
    var mysubset =
    FILTER(VALUES(Sales[CustomerKey]), 
        [Has NovDec2023 sales] = 1 &&
        [Has NovDec2024 sales] = 1)
     RETURN
     // count the rows
     COUNTROWS(mysubset)   

     

     

     

    Please click the [accept solution] and thumbs up button. Thank you.

     

    Click here to download PBIX example from Onedrive

    Click here