谁能给我解释一下 PowerBI 中这个失去客户的公式?
Who can explain me this formula of lost customers in PowerBI?
我无法理解这个公式。你能帮助我吗? 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]
)
)
)
使用适当的变量名重写代码会更容易理解。
[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 )
我无法理解这个公式。你能帮助我吗? 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]
)
)
)
使用适当的变量名重写代码会更容易理解。
[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 )