Lukeos93
Lukeos93

Reputation: 37

Who can explain me this formula of lost customers in PowerBI?

I have problems to understand this formula. Can you help me? https://www.daxpatterns.com/new-and-returning-customers

...................................................................

[Lost Customers] :=
COUNTROWS (
FILTER (
    ADDCOLUMNS (
        FILTER (
            CALCULATETABLE (
                ADDCOLUMNS (
                    VALUES ( <customer_key_column> ),
                    "CustomerLostDate", 
                        CALCULATE ( MAX ( <fact_date_column> ) ) + [LostDaysLimit]
                ),
                FILTER (
                    ALL ( <date_table> ),
                    AND (
                        <date_column> < MIN ( <date_column> ),
                        <date_column>
                            >= MIN ( <date_column> ) - [LostDaysLimit]
                    )
                )
            ),
            AND (
                AND (
                    [CustomerLostDate] >= MIN ( <date_column> ),
                    [CustomerLostDate] <= MAX ( <date_column> )
                ),
                [CustomerLostDate] 
                    <= CALCULATE ( MAX ( <fact_date_column> ), ALL ( <fact_table> ) )
            )
        ),
        "FirstBuyInPeriod", CALCULATE ( MIN ( <fact_date_column> ) )
    ),
    OR (
        ISBLANK ( [FirstBuyInPeriod] ),
        [FirstBuyInPeriod] > [CustomerLostDate]
    )
)

)

Upvotes: 0

Views: 289

Answers (1)

Kosuke Sakai
Kosuke Sakai

Reputation: 2411

Rewriting the code using appropriate variable names would make it easier to comprehend.

[Lost Customers] :=

-- First and last dates in the currently sliced period
VAR SelectedFirstDate = MIN ( <date_column> )
VAR SelectedLastDate = MAX ( <date_column> )

-- The absolute last date with sales in the data model
VAR LastWorkingDate = CALCULATE (
    MAX ( <fact_date_column> ),
    ALL ( <fact_table> )
)

-- Subset of the date table in which to consider the last purchase.
VAR LastPeriodToConsider =
    FILTER (
        ALL ( <date_table> ),
        <date_column> >= SelectedFirstDate - [LostDaysLimit]
        && <date_column> < SelectedFirstDate
    )

-- Calculate CustomerLostDate, which is the limit date by when the customer
-- should buy any product again or she is considered to be lost.
VAR CustomersWithLostDate = 
    CALCULATETABLE (
        ADDCOLUMNS (
            VALUES ( <customer_key_column> ),
            "CustomerLostDate", 
            CALCULATE( MAX ( <fact_date_column> ) ) + [LostDaysLimit]
        ),
        LastPeriodToConsider
    )

-- Pick up customers who's CustomerLostDate comes in the currently sliced period.
-- If there is not purchase by the date, she is considered to be lost.
VAR CustomersMaybeLost =
    FILTER (
        CustomersWithLostDate,
        -- CustomerLostDate comes in the currently sliced period
        [CustomerLostDate] >= SelectedFirstDate 
        && [CustomerLostDate] <= SelectedLastDate
        -- and it is before the last working date
        && [CustomerLostDate] <= LastWorkingDate 
    )

-- Calculate the first date in current period when the customer buys any product.
-- May be BLANK if the customer doesn't buy anything.
VAR CustomersWithFirstBuyDate =
    ADDCOLUMNS (
        CustomersMaybeLost,
        "FirstBuyInPeriod", CALCULATE ( MIN ( <fact_date_column> ) )
    )

-- If there is no buying before the limit date, she is lost.
VAR LostCustomers =
    FILTER(
        CustomersWithFirstBuyDate,
        ISBLANK ( [FirstBuyInPeriod] )
        || [FirstBuyInPeriod] > [CustomerLostDate]
    )

RETURN COUNTROWS ( LostCustomers )

Upvotes: 1

Related Questions