Forum Discussion

Yqy's avatar
Yqy
Helper I
3 years ago
Solved

Count the Variable when it appear the first time

Hi, i have a table with many variables and it have duplicates, what i want to do is to have count for the variable when it appear the first time and does not count the variable when is appear the second time onwards.

Var VarCount
A A1
A Anull
B B1
B Bnull
C C1
D D1
  • Hi again, 
    Yes, it is possible please follow these few steps (after adding index column):
    1. Group by with counting all rows:

    2. Expand only index

    3. Add a conditional column

    4. remove unnecessary columns
    RESULT :

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.




5 Replies

  • Hi Yqy 
    You can achieve it with 2steps :
    1 . In PQ add the index column:

    2. In the Data editor add a column with this Dax:

    first occurrence check =
    IF (
        CALCULATE (
            COUNTROWS ( 'Table' ),
            FILTER (
                ALLEXCEPT ( 'Table','Table'[Var]),
                'Table'[Index] <= EARLIER (  'Table'[Index] )
            )
        )
            > 1,
        blank(),
        1
    )

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.



    • Yqy's avatar
      Yqy
      Helper I

      Hi, is it possible to do the second step in Query using M language instead of in the data editor using DAX

      • Ritaf1983's avatar
        Ritaf1983
        Super User

        Hi again, 
        Yes, it is possible please follow these few steps (after adding index column):
        1. Group by with counting all rows:

        2. Expand only index

        3. Add a conditional column

        4. remove unnecessary columns
        RESULT :

        If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.