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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

Count if row value repeats. Count number of times value repeats in column

In PowerQuery I want to add a custom column that evaluates how often a value repeats. I want to do this in PowerQuery, not DAX.

 

For example, if the value in Column 1 repeats twice then the new custom column would show "2" in each row. The below screen shot is what I'm trying to create where Column A exists and Column B is the column that does the countif.

 

I realize I could do a grouping and count, but I need to maintain the detail. I also don't want to create another group table and then merge it back (which is what I'm currently doing creates other problems)

 

If I were to do this in excel the formula looks like this: =countif(A;A,A2)

 

Countif.png

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

One way to solve this is with List functions in the query editor.  You can add this formula to a custom column (you can put whole let ... in expression in a custom column too).  If not performant, you could try wrapping #'Changed Type"[Column1] with List.Buffer( ).

 

= let
thisrow = [Column1],
templist = #"Changed Type"[Column1]
in
List.Count(List.Select(templist, each _ = thisrow))

 

Please see this whole query for an example with your sample data.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVorVQdCOTs6YdCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Repeats", each let
thisrow = [Column1],
templist = List.Buffer(#"Changed Type"[Column1])
in
List.Count(List.Select(templist, each _ = thisrow)))
in
#"Added Custom"

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Microsoft Employee
Microsoft Employee

One way to solve this is with List functions in the query editor.  You can add this formula to a custom column (you can put whole let ... in expression in a custom column too).  If not performant, you could try wrapping #'Changed Type"[Column1] with List.Buffer( ).

 

= let
thisrow = [Column1],
templist = #"Changed Type"[Column1]
in
List.Count(List.Select(templist, each _ = thisrow))

 

Please see this whole query for an example with your sample data.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVorVQdCOTs6YdCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Repeats", each let
thisrow = [Column1],
templist = List.Buffer(#"Changed Type"[Column1])
in
List.Count(List.Select(templist, each _ = thisrow)))
in
#"Added Custom"

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


HotChilli
Super User
Super User

You can maintain the detail by doing two aggregations in the 'Group By' , the first one is the 'Count Rows', the second is  'All Rows'.

Expand the all column (and remove it if required)

Anonymous
Not applicable

 Thanks, can you give me a little more detail on how this would work? I did the group by  below

 

group by.png

 

 

 

 

 

 

 

 

 

But then PowerBI gave me an error "There was an error deserializing the evaluation results. The operaiton might succeed on a retry. I've retried a few times but still get the error.

error 9.png

 

Well, I've never seen that one before.  Size of the dataset? Something else unusual about the data?

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors