Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Countif for specific customers

Hello,

 

I have searched for a solution of my problem for hours but could'nt find any satisfying one.

I want to "translate" this Excel formula in DAX, knowing that all the information are in the same table (SALES). The final objective is to make a SUM of the column G in order to know how many purchases are made by customers who have made their first purchase during the current year.

 

2017-08-04 13_04_22-Microsoft Excel - Classeur1.png

 

Thanks for your help !

 

2 ACCEPTED SOLUTIONS

Hey,

 

it seems this measure

NoOfPurchasesInFirstYear = 
CALCULATE(
SUMX(
	'Purchases'
	,var customer = 'Purchases'[Customer]
	var yearoffirstpurchase = YEAR('Purchases'[PurchaseDate])
	return
		COUNTROWS(
			FILTER(
				ALL('Purchases')
				,Purchases[Customer] = customer 
				&& YEAR('Purchases'[PurchaseDate]) = yearoffirstpurchase
			)
		)
	)
,'Purchases'[IsFirstPurchase] = "Yes"
)

creates what you are looking for
2017-08-04_14-58-48.png

 

Here is sample data if you want to recreate my example

PurchaseDate	Customer	IsFirstPurchase
2017-01-07	C1	No
2017-07-09	C1	No
2016-08-02	C1	No
2016-06-01	C1	Yes
2017-01-07	C2	Yes
2017-07-09	C2	No
2016-07-04	C1	No

Hope this is what you are looking for



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

Be my guest.

 

The result

2017-08-04_18-29-26.png

 

The Measure

SumOfPurchasesInFirstYear = 
CALCULATE(
SUMX(
	'Purchases'
	,var customer = 'Purchases'[Customer]
	var yearoffirstpurchase = YEAR('Purchases'[PurchaseDate])
	return
		CALCULATE(SUM(Purchases[Amount]),
			FILTER(
				ALL('Purchases')
				,Purchases[Customer] = customer 
				&& YEAR('Purchases'[PurchaseDate]) = yearoffirstpurchase
			)
		)
	)
,'Purchases'[IsFirstPurchase] = "Yes"
)

The enhanced sample data (sligthly enhanced :-))

PurchaseDateCustomerIsFirstPurchaseAmount
2017-01-07C1No1
2017-07-09C1No2
2016-08-02C1No3
2016-06-01C1Yes4
2017-01-07C2Yes5
2017-07-09C2No6
2016-07-04C1No7

 

Cheers



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

7 REPLIES 7
TomMartens
Super User
Super User

Hm,

 

wondering how the Excel formula would work, to me it seems that it would count also the first purchase  in a previous year. Because there is no check in the formula considering the purchase date.

 

My understanding

  • all purchases of one customer in its first year would be flagged as TRUE
  • You want to count the number of Purchases from new customer and not the customer

Can you please provide feedback if my understanding is correct?

 

Regards



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Actually you're right about the fact that it counts all purchases without consideration of the year (I thought I'll tackle this problem later on :)). If I could have all the following purchases (count of it) from one customer whose 1st purchase is in the filtered year, it would already be great success for my calculation.

In my Excel sheet, as long as a customer has made his first purchase (labelled as "true"), all the following purchases he makes are not labelled as "true" which means my COUNTIF will give me only one value (the number of all purchases following a first purchase, plus this first purchase) for these specific customers with first purchase.

 

2017-08-04 14_04_08-Microsoft Excel - Classeur1.png

 

Hope this helps for your understanding.

 

Thanks for your help.

Hey,

 

it seems this measure

NoOfPurchasesInFirstYear = 
CALCULATE(
SUMX(
	'Purchases'
	,var customer = 'Purchases'[Customer]
	var yearoffirstpurchase = YEAR('Purchases'[PurchaseDate])
	return
		COUNTROWS(
			FILTER(
				ALL('Purchases')
				,Purchases[Customer] = customer 
				&& YEAR('Purchases'[PurchaseDate]) = yearoffirstpurchase
			)
		)
	)
,'Purchases'[IsFirstPurchase] = "Yes"
)

creates what you are looking for
2017-08-04_14-58-48.png

 

Here is sample data if you want to recreate my example

PurchaseDate	Customer	IsFirstPurchase
2017-01-07	C1	No
2017-07-09	C1	No
2016-08-02	C1	No
2016-06-01	C1	Yes
2017-01-07	C2	Yes
2017-07-09	C2	No
2016-07-04	C1	No

Hope this is what you are looking for



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

I wonder if you can help me further:

 

I'd like to make the sum of the total amount (from Purchased value) paid by these customers whose first purchase was made this current year. The sum is the total amount paid by these specific customers for the filtered year, not only the sum of their first purchase.

 

Once again thank you for your help and I wish you a great weekend.

Be my guest.

 

The result

2017-08-04_18-29-26.png

 

The Measure

SumOfPurchasesInFirstYear = 
CALCULATE(
SUMX(
	'Purchases'
	,var customer = 'Purchases'[Customer]
	var yearoffirstpurchase = YEAR('Purchases'[PurchaseDate])
	return
		CALCULATE(SUM(Purchases[Amount]),
			FILTER(
				ALL('Purchases')
				,Purchases[Customer] = customer 
				&& YEAR('Purchases'[PurchaseDate]) = yearoffirstpurchase
			)
		)
	)
,'Purchases'[IsFirstPurchase] = "Yes"
)

The enhanced sample data (sligthly enhanced :-))

PurchaseDateCustomerIsFirstPurchaseAmount
2017-01-07C1No1
2017-07-09C1No2
2016-08-02C1No3
2016-06-01C1Yes4
2017-01-07C2Yes5
2017-07-09C2No6
2016-07-04C1No7

 

Cheers



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Once again, thank you very much for your help ! It works perfectly 🙂 !

Anonymous
Not applicable

Thanks a lot, it works perfectly 😄 !

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.