Skip to content

Instantly share code, notes, and snippets.

@mlongoria
Created August 8, 2016 00:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlongoria/ae76049dcfeb35dba0bcc4cda02707d4 to your computer and use it in GitHub Desktop.
Save mlongoria/ae76049dcfeb35dba0bcc4cda02707d4 to your computer and use it in GitHub Desktop.
Shows the calculation of lost customers based upon sales fact using DAX for SSAS Tabular
Lost Customers :=
IF (
NOT (
MIN ( 'Date'[Full Date] )
> CALCULATE ( MAX ( Sales[Invoice Date] ), ALL ( Sales ) )
),
COUNTROWS (
FILTER (
ADDCOLUMNS (
FILTER (
CALCULATETABLE (
ADDCOLUMNS (
CALCULATETABLE (
VALUES ( Customer[Customer No] ), Sales ),
"CustomerLostDate", CALCULATE (
MAX ( Sales[Invoice Date] ),
ALLEXCEPT ( Customer, Customer[Customer No] )
)
+ [Lost Days Limit]
),
FILTER (
ALL ( 'Date' ),
AND (
'Date'[Full Date] < MIN ( 'Date'[Full Date] ),
'Date'[Full Date] >=
MIN ( 'Date'[Full Date] ) - [Lost Days Limit]
)
)
),
AND (
AND (
[CustomerLostDate] >= MIN ( 'Date'[Full Date] ),
[CustomerLostDate] <= MAX ( 'Date'[Full Date] )
),
[CustomerLostDate] <=
CALCULATE ( MAX ( Sales[Invoice Date] ), ALL ( Sales ) )
)
),
"FirstBuyInPeriod", CALCULATE ( MIN ( Sales[Invoice Date] ) )
),
OR (
ISBLANK ( [FirstBuyInPeriod] ),
[FirstBuyInPeriod] > [CustomerLostDate]
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment