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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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