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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
ADV__ROHIT
Frequent Visitor

How to use COUNTIF in Power Query.

Hi everyone,

I need some help with Power Query. I'm trying to create a new column that indicates if a transaction in my data is the first occurrence or not. In Excel, I achieved this with the following formula:
"=IF(COUNTIF($A$2:A2,A2)=1,1,0)"
This formula checks if the transaction is the first occurrence (returns 1) or not (returns 0). I want to replicate this functionality in Power Query. My project is currently stuck because I can't figure out how to do this. Any guidance would be greatly appreciated!

ADV__ROHIT_0-1721992840964.png

ADV__ROHIT_1-1721993325811.png

 

 

1 ACCEPTED SOLUTION
PwerQueryKees
Super User
Super User

Start Table

PwerQueryKees_0-1721995269513.png

The Code, I made some internmediate columns for clarity. You can combine the formulas or remove the columns as you like:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Duplicates" = Table.AddColumn(#"Added Index", "Custom", each Table.SelectRows(#"Added Index", (T) => T[Transaction] = [Transaction] and T[Index] <= [Index])),
    #"Added IsFirst" = Table.AddColumn(#"Added Duplicates", "isFirst", each Table.RowCount([Custom])= 1)
in
    #"Added IsFirst"

Result:

PwerQueryKees_1-1721995391327.png

Result sorted by Transaction for easy checking:

PwerQueryKees_2-1721995438147.png

You're welcome!

View solution in original post

1 REPLY 1
PwerQueryKees
Super User
Super User

Start Table

PwerQueryKees_0-1721995269513.png

The Code, I made some internmediate columns for clarity. You can combine the formulas or remove the columns as you like:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Duplicates" = Table.AddColumn(#"Added Index", "Custom", each Table.SelectRows(#"Added Index", (T) => T[Transaction] = [Transaction] and T[Index] <= [Index])),
    #"Added IsFirst" = Table.AddColumn(#"Added Duplicates", "isFirst", each Table.RowCount([Custom])= 1)
in
    #"Added IsFirst"

Result:

PwerQueryKees_1-1721995391327.png

Result sorted by Transaction for easy checking:

PwerQueryKees_2-1721995438147.png

You're welcome!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

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.