Forum Discussion
Condition for counting new customers
Hello community
My task is to count new customers according to the following condition
The total number of purchases must be more than 3 purchases and the number of purchased goods must be more than 5,000 units
For example, we have a customer with ID 71, he made 1 purchase of 7,500 units in January and 2 purchases of 14,000 units in February, such a customer will be new. If the customer had more than 3 purchases and more than 5,000 units in the current month, we consider him as a new customer, but do not indicate in the next month that he is a new customer.
I tried to write the following calculation, but for some reason it does not give me the desired result.
New Customers =
VAR CurrentMonth =
MAX('Dataes'[Month])
VAR _PreviousMonth =
EOMONTH(CurrentMonth, -1)
VAR NewCustomers =
CALCULATETABLE(
VALUES('TableFact'[ID_Customers]),
'Dataes'[Month] = CurrentMonth,
'TableFact'[Amount] >= 5000 &&
COUNTROWS('TableFact') >= 3
)
VAR ReturningCustomers =
CALCULATETABLE(
VALUES('TableFact'[ID_Customers]),
'Dataes'[Month] = CurrentMonth,
'TableFact'[Amount] < 5000,
'Dataes'[Month] = _PreviousMonth,
'TableFact'[Amount] >= 5000 &&
COUNTROWS('TableFact') >= 3
)
VAR NewCustomersWithMoreThanThreePurchases =
CALCULATETABLE(
NewCustomers,
'TableFact'[ID_Customers] IN
CALCULATETABLE(
VALUES('TableFact'[ID_Customers]),
'Dataes'[Month] = CurrentMonth,
'TableFact'[Amount] >= 5000 &&
COUNTROWS('TableFact') >= 3
)
)
RETURN
COUNTROWS(NewCustomersWithMoreThanThreePurchases) - COUNTROWS(ReturningCustomers)
What measure should I use for the desired result
Hi DimaMD
I just tried. Seems to be workingNew Customers - TJ 2 = VAR SelectedDates = ALLSELECTED ( Dataes ) VAR CurrentMonth = MAX ( 'Dataes'[YearMonth] ) VAR CurrentCustomers = VALUES ( TableFact[ID_Customers] ) VAR CurrentNewCustomers = FILTER ( CurrentCustomers, CALCULATE ( SUM ( TableFact[Amount] ), Dataes[YearMonth] <= CurrentMonth, SelectedDates ) >= 5000 && CALCULATE ( COUNTROWS ( TableFact ), Dataes[YearMonth] <= CurrentMonth, SelectedDates ) >= 3 ) VAR PreviousCustomers = CALCULATETABLE ( VALUES ( 'TableFact'[ID_Customers] ), 'Dataes'[YearMonth] < CurrentMonth, SelectedDates ) VAR PreviousNewCustomers = FILTER ( PreviousCustomers, CALCULATE ( SUM ( TableFact[Amount] ), Dataes[YearMonth] < CurrentMonth, SelectedDates ) >= 5000 && CALCULATE ( COUNTROWS ( TableFact ), Dataes[YearMonth] < CurrentMonth, SelectedDates ) >= 3 ) RETURN COUNTROWS ( EXCEPT ( CurrentNewCustomers, PreviousNewCustomers ) )
13 Replies
- tamerj1
Community Champion
DimaMD
Please refer to updated sample file with the proposed solutionNew Customers - TJ = VAR SelectedDates = ALLSELECTED ( Dataes ) VAR CurrentMonth = MAX ( 'Dataes'[YearMonth] ) VAR CurrentCustomers = VALUES ( TableFact[ID_Customers] ) VAR CurrentNewCustomers = FILTER ( CurrentCustomers, CALCULATE ( SUM ( TableFact[Amount] ), Dataes[YearMonth] <= CurrentMonth, SelectedDates ) >= 5000 && CALCULATE ( COUNTROWS ( TableFact ), Dataes[YearMonth] <= CurrentMonth, SelectedDates ) >= 3 ) VAR OneMonthBefore = MAXX ( FILTER ( ALLSELECTED ( Dataes[YearMonth] ), Dataes[YearMonth] < CurrentMonth ), Dataes[YearMonth] ) VAR CustomersOneMonthBefore = CALCULATETABLE ( VALUES ( 'TableFact'[ID_Customers] ), 'Dataes'[YearMonth] = OneMonthBefore, SelectedDates ) VAR NewCustomersOneMonthBefore = FILTER ( CustomersOneMonthBefore, CALCULATE ( SUM ( TableFact[Amount] ), Dataes[YearMonth] < CurrentMonth, SelectedDates ) >= 5000 && CALCULATE ( COUNTROWS ( TableFact ), Dataes[YearMonth] < CurrentMonth, SelectedDates ) >= 3 ) RETURN COUNTROWS ( EXCEPT ( CurrentNewCustomers, NewCustomersOneMonthBefore ) )- DimaMD
Solution Sage
Ні, tamerj1 in cases where condition appears only once, it works.
But in case (like on screenshot customer 531) when you can see that condition worked 2 times - on 2nd and 4th month, it worked twise. Our goal is if this customer was "new" once, he doesnt need to be "new" second time, So if he was new on "month 2" then on "month 4" he isn't new anymore.
- tamerj1
Community Champion
Ok, that requirement was not clear as you mentioned "but do not indicate in the next month that he is a new customer". You should've said next months.
However, I'm now driving back home so cannot test but you may tryNew Customers - TJ =
VAR SelectedDates =
ALLSELECTED ( Dataes )
VAR CurrentMonth =
MAX ( 'Dataes'[YearMonth] )
VAR CurrentCustomers =
VALUES ( TableFact[ID_Customers] )
VAR CurrentNewCustomers =
FILTER (
CurrentCustomers,
CALCULATE (
SUM ( TableFact[Amount] ),
Dataes[YearMonth] <= CurrentMonth,
SelectedDates
) >= 5000
&& CALCULATE (
COUNTROWS ( TableFact ),
Dataes[YearMonth] <= CurrentMonth,
SelectedDates
) >= 3
)
VAR PreviousCustomers =
CALCULATETABLE (
VALUES ( 'TableFact'[ID_Customers] ),
'Dataes'[YearMonth] < CurrentMonth,
SelectedDates
)
VAR PreviousNewCustomers =
FILTER (
PreviousCustomers,
CALCULATE (
SUM ( TableFact[Amount] ),
Dataes[YearMonth] < CurrentMonth,
SelectedDates
) >= 5000
&& CALCULATE (
COUNTROWS ( TableFact ),
Dataes[YearMonth] < CurrentMonth,
SelectedDates
) >= 3
)
RETURN
COUNTROWS ( EXCEPT ( CurrentNewCustomers, PreviousNewCustomers ) )
- tamerj1
Community Champion
With this change the code will retrieve the month if the current filer conext same as CurrentMonth!! I don't believe this will have any advantage. I have completely removed this variable in the 2nd proposed dax. Have you tried?