Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

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 s...
  • TomMartens's avatar
    TomMartens
    9 years ago

    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

     

    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

  • TomMartens's avatar
    TomMartens
    9 years ago

    Be my guest.

     

    The result

     

    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