Forum Discussion

FatBlackCat30's avatar
FatBlackCat30
Microsoft Employee
6 years ago
Solved

D count Column in Power Query

Hello all,

 

I am wanting to create a Custom Column in Power Query :

 

1. I need to count the number of unique items per customer. BUT I dont want to include the items "All Other" or "Both" in the count.

 

here is an example of the data and the outcome.

 

CustomerItems  CustomerItemsCount
1Plant  1Plant2
1Cat  1Cat2
2Dog  2Dog1
3Car  3Car1
4Both  4Both1
4Bottle  4Bottle1
5All Other  5All Other0
6Phone  6Phone2
6Tree  6Tree2

 

 

Is there a good resource I can find info on formulas like this so I dont have to keep asking for help?

  • az38's avatar
    az38
    6 years ago

    FatBlackCat30 

    try this

    Measure = 
    var _countItems = CALCULATE(DISTINCTCOUNT('Table'[Items]);ALLEXCEPT('Table';'Table'[Customer]);NOT ('Table'[Items]  IN ({"Both"; "All Other"})))
    RETURN
    IF(_countItems < 1; 0; _countItems)

     

    do not hesitate to give a kudo to useful posts and mark solutions as solution

4 Replies

  • az38's avatar
    az38
    Community Champion

    Hi FatBlackCat30 

    Is it mandatory to use Power Query?

    It is a great, easy and very logical task for DAX:

    Measure = 
    var _countItems = CALCULATE(DISTINCTCOUNT('Table'[Items]);ALLEXCEPT('Table';'Table'[Customer]);NOT ('Table'[Items]  IN ({"Both"; "All Other"})))
    RETURN
    IF(SELECTEDVALUE('Table'[Items])="Both" || SELECTEDVALUE('Table'[Items])="All Other";0; _countItems)

     

    do not hesitate to give a kudo to useful posts and mark solutions as solution

    LinkedIn

    • FatBlackCat30's avatar
      FatBlackCat30
      Microsoft Employee

      az38 this works perfectly, no requirement to use Power Query. 

       

      is there a way I can add a 0 for the two values I am not counting in the formula

       

      thanks for the help

      • az38's avatar
        az38
        Community Champion

        FatBlackCat30 

        try this

        Measure = 
        var _countItems = CALCULATE(DISTINCTCOUNT('Table'[Items]);ALLEXCEPT('Table';'Table'[Customer]);NOT ('Table'[Items]  IN ({"Both"; "All Other"})))
        RETURN
        IF(_countItems < 1; 0; _countItems)

         

        do not hesitate to give a kudo to useful posts and mark solutions as solution