Forum Discussion

Diego_Vialle's avatar
Diego_Vialle
Icon for Helper II rankHelper II
4 years ago
Solved

DAX - Customer retention

Calculate the sum of customers who bought and stopped buying in the last 12 months, and returned to buy in the quarter.   tamerj1  do you have any idea?
  • tamerj1's avatar
    tamerj1
    4 years ago

    Hi Diego_Vialle 

    Please try the following code. To be honest I have no idea whether it works or not. However I tried to optimize it only by sense as I have no data to work with and measure the actual performance. This is a heavy code which in total requires a great number of context transitions and multiple iterations over the complete table. This iteration cannot be avoided otherwise there is no other way to gain access to each individual date which is required to calculate the period gap between each two consecutive transactions of the same customer. You might find it a bet complex but in fact it is simple and I hope it works. 

    Further you shall be able to use the same code to flag the customers who matches this criteria with the flexibility to filter different ranges of date (You can use a time slider based on dCalendar[Date] to select start and end dates of the selected period)

    Teste aula = 
    SUMX (
        CALCULATETABLE ( 
            VALUES ( SBOPRODMS[Código do cliente] ), 
            SBOPRODMS[Documento] = "Nota fiscal de saída"
        ),
        VAR CurrentCustomerTable = 
            CALCULATETABLE ( SBOPRODMS, SBOPRODMS[Documento] = "Nota fiscal de saída" )
        VAR T1 = 
            ADDCOLUMNS ( 
                CurrentCustomerTable, 
                "Period", 
                VAR CurrentDate = 
                    SBOPRODMS[Data Base]
                VAR PreviousDate = 
                    MAXX ( FILTER ( CurrentCustomerTable, SBOPRODMS[Data Base] < CurrentDate ), SBOPRODMS[Data Base] )
                RETURN 
                    IF ( NOT ISBLANK ( PreviousDate ), DATEDIFF ( PreviousDate, CurrentDate, DAY ) )
            )
        VAR MaxiDisengagementPeriod = 
            MAXX ( T1, [Period] )
        RETURN
            IF ( MaxiDisengagementPeriod > 365, 1 )
    )