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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Cihan_G
New Member

Count rows with conditions in Power Query

Hey Guys,

 

I need help counting the occurance of a value within a specific key value.

The data I have looks something like this:

KeyParameter within keyValue
Aaaa1
Aaab2
Aaac2
Aaad4
Bbba1
Bbbb2
Bbbc3
Bbbd4

 

What I need would be this:

Key# of 2's
A2
B1

 

I already made this work using DAX, but I have to implement this in the query editor using Power Query / M Language, because I have to merge the resulting table to another table and calculate more columns using "# of 2's"

 

Can someone please help me out with this?

 

Thanks in advance and best regards,

 

Cihan

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Cihan_G 

Download this sample PBIX with code

Use this in Power Query

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMTASShkqxOjB+EpA0QuIno/FTgKQJmO8EZCUlIfRD+Aj9ED5IvzESH6o/FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, #"Parameter within key" = _t, Value = _t]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Value] = "2")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Key"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

 

count-nums.png

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

5 REPLIES 5
v-eqin-msft
Community Support
Community Support

Hi @Cihan_G ,

 

you could follow these steps:

duplicate the table --> Filter and Groupby respectively--> finally merge the two tables as shown below:

2.1.1.1.gif

 

But it will lead to an increase in memory/cpu to a certain extent since I have create a new table.And normally memory consumption will reduce the performance and brings heavier load to the model. 

 

So you could disable "Enable load" option.

2.1.1.2.jpg

 

Please take a look at the pbix file here.

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

PhilipTreacy
Super User
Super User

Hi @Cihan_G 

Can you please be clear about the desired result.  What about counting 1's?  Do you want to add the count of 3's and 4's togeher?

Bear in mind that the code may specifically be looking for the values 1, 2 3, etc so when you come to use it on real data not may not work.  Can you supply some actual data?

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hey Phil, 

 

sorry for beeing unclear.

The actual data also contains these values, so your code works just fine.

I want to add the counts (in the sample for 3's and 4's) together for the final table. The table, i need for the acual data will look like this:

 

Key      //    GREEN       //        YELLOW       //       RED

A        //        ...            //              ...           //          ...

B        //        ...            //              ...           //          ...

...

 

Where GREEN counts 1's, 2's & 3's YELLOW counts 4's and RED counts 5's ,6's, 7's

Other than that, the actual data looks just like the example, I provided.

 

Best regards,

 

Cihan

Cihan_G
New Member

Hey Phil,

 

thank you for your fast reply! 

This worked so far, but how can I now add more columns, based on other filters to that?

 

Key   //   # of 2's  //  # of 3's and '4s

A      //       2        //            1

B      //       1        //            2

 

Can you please help me out with this aswell?

 

Thank you very much,

Cihan

PhilipTreacy
Super User
Super User

Hi @Cihan_G 

Download this sample PBIX with code

Use this in Power Query

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMTASShkqxOjB+EpA0QuIno/FTgKQJmO8EZCUlIfRD+Aj9ED5IvzESH6o/FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, #"Parameter within key" = _t, Value = _t]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Value] = "2")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Key"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

 

count-nums.png

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.