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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Yqy
Helper I
Helper I

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
1 ACCEPTED SOLUTION

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

Ritaf1983_0-1685934745914.png

2. Expand only index

Ritaf1983_1-1685934826919.png

3. Add a conditional column

Ritaf1983_2-1685934904129.png

4. remove unnecessary columns
RESULT :

Ritaf1983_3-1685934966299.png

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




Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

View solution in original post

5 REPLIES 5
Ritaf1983
Super User
Super User

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

Ritaf1983_0-1685932313938.png

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.



Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

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

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

Ritaf1983_0-1685934745914.png

2. Expand only index

Ritaf1983_1-1685934826919.png

3. Add a conditional column

Ritaf1983_2-1685934904129.png

4. remove unnecessary columns
RESULT :

Ritaf1983_3-1685934966299.png

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




Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

Thanks for the help, your solution works.

I was happy to help 😊

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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