Forum Discussion

APR92's avatar
APR92
Frequent Visitor
2 years ago
Solved

Detect Past Month lost customer

Hi, I'm trying to find the customers that I lost from the previous month. So far I've been able to calculate it individually but I'm stuck with the subtotals.   The model is a single table inndic...
  • giammariam's avatar
    giammariam
    2 years ago

    Hey APR92, there is probably a much cleaner way to do this, but try this:

    Lost2 = 
    VAR selectedDate =
        SELECTEDVALUE(Consulta1[Date])
    VAR selectedCategory =
        SELECTEDVALUE(Consulta1[Category])
    VAR tbl =
        ADDCOLUMNS (
            ADDCOLUMNS (
                Consulta1,
                "Active Last Month",
                    MAXX (
                        FILTER (
                            ALL ( Consulta1 ),
                            Consulta1[Date] < EARLIER(Consulta1[Date])
                                && (
                                    Consulta1[Category] = EARLIER(Consulta1[Category])
                                        || ISBLANK ( EARLIER(Consulta1[Category]) )
                                )
                        ),
                        Consulta1[Active]
                    )
            ),
            "Lost",
                SWITCH (
                    TRUE,
                    ISBLANK ( [Active Last Month] ), BLANK (),
                    [Active Last Month] < Consulta1[Active], 0,
                    [Active Last Month] - Consulta1[Active]
                )
        )
    VAR lost = 
        SUMX (
            FILTER (
                tbl,
                Consulta1[Date] = selectedDate
                    && (
                        Consulta1[Category] = selectedCategory
                            || ISBLANK ( selectedCategory )
                    )
            ),
            [Lost]
        )
    RETURN lost