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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
DSPVarma
Frequent Visitor

Dax Formula for the count of Unique value

Please find the attached screenshot. I want a DAX formula that should assign a value of 1 to the first occurrence of each unique value in the "UBID" column of the "DSRReport" table, and 0 to subsequent occurrences of the same value.

In Excel, I used the following formula to get the result '=IF(COUNTIF($B$1:B2,[@Product])>1,0,1)', but I am unable to get it in DAX. Please help me with this. Thanks in advance.

DSPVarma_0-1711712696726.png

 

 

2 ACCEPTED SOLUTIONS
qqqqqwwwweeerrr
Super User
Super User

Hi @DSPVarma 

 

This is just a work around. If you get stuck you can use this solution. 

For this i have created one more column "Index in power query and first short your data in power query by product and create new column index"

qqqqqwwwweeerrr_1-1711716645104.png

 

once you create this than you can create column using this Dax 

Flag =
var _index= CALCULATE(MIN('Sheet10'[Index]),ALLEXCEPT('Sheet10','Sheet10'[Product]))
return IF('Sheet10'[Index]= _index,1,0)
This is my output
qqqqqwwwweeerrr_2-1711716676025.png

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
Check for more interesting solution here: www.youtube.com/@Howtosolveprobem


Regards

View solution in original post

Ritaf1983
Super User
Super User

Hi @DSPVarma 
You can achieve the goal with the power query.

The steps are :
1. Group the rows with the option "All rows" 

Ritaf1983_0-1711716428357.pngRitaf1983_1-1711716478518.png

2. Add a custom column with "Table.AddIndexColumn"

Ritaf1983_2-1711716619131.pngRitaf1983_3-1711716642841.png

3. Remove other columns 

Ritaf1983_4-1711716706186.png

4. Expend 

Ritaf1983_5-1711716761833.pngRitaf1983_6-1711716784259.png

5. Add a conditional column that based on the index column returns 1 / 0 :

Ritaf1983_7-1711716903365.png

6. Remove unnecessary index column

Ritaf1983_8-1711716970218.png

Result:

Ritaf1983_9-1711717009829.png

PBIX is attached

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

3 REPLIES 3
Ritaf1983
Super User
Super User

Hi @DSPVarma 
You can achieve the goal with the power query.

The steps are :
1. Group the rows with the option "All rows" 

Ritaf1983_0-1711716428357.pngRitaf1983_1-1711716478518.png

2. Add a custom column with "Table.AddIndexColumn"

Ritaf1983_2-1711716619131.pngRitaf1983_3-1711716642841.png

3. Remove other columns 

Ritaf1983_4-1711716706186.png

4. Expend 

Ritaf1983_5-1711716761833.pngRitaf1983_6-1711716784259.png

5. Add a conditional column that based on the index column returns 1 / 0 :

Ritaf1983_7-1711716903365.png

6. Remove unnecessary index column

Ritaf1983_8-1711716970218.png

Result:

Ritaf1983_9-1711717009829.png

PBIX is attached

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
qqqqqwwwweeerrr
Super User
Super User

Hi @DSPVarma 

 

This is just a work around. If you get stuck you can use this solution. 

For this i have created one more column "Index in power query and first short your data in power query by product and create new column index"

qqqqqwwwweeerrr_1-1711716645104.png

 

once you create this than you can create column using this Dax 

Flag =
var _index= CALCULATE(MIN('Sheet10'[Index]),ALLEXCEPT('Sheet10','Sheet10'[Product]))
return IF('Sheet10'[Index]= _index,1,0)
This is my output
qqqqqwwwweeerrr_2-1711716676025.png

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
Check for more interesting solution here: www.youtube.com/@Howtosolveprobem


Regards
Zang_Mi
Resolver II
Resolver II

Hello, with the first column as input data, you can use Query Editor in Power BI to create a custom column to obtain that result in 2 steps.

Zang_Mi_0-1711716278151.png

  1. Index per Product. Group rows by Product column and add index to each group.
  2. Result. If the value in Index per Product is equal to 1, then return 1, otherwise, return 0.

You can copy the following query to your dashboard and check how each step has been implemented.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1UElnZBIZzDpAiZdwaQbEukOJj3wkp5g0kspNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Product"}, {{"Index Group", each Table.AddIndexColumn(_, "Index", 1)}}),
    #"Expanded Index Group" = Table.ExpandTableColumn(#"Grouped Rows", "Index Group", {"Index"}, {"Index per Product"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Index Group", "Result", each if [Index per Product] = 1 then 1 else 0)
in
    #"Added Custom"

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors