Thursday, August 29, 2013

Google Spreadsheet Count instances in an array with conditions

Question:

( by amj05 )


If I have two columns that look like this:

AX, Y
BY
CX, Z
BX, Y, Z
DZ
AY, Z
AX, Y, Z

Is there a formula to count the total number of times Y appears to the right of A (whether or not Y appears with anything else)? The only way I know involves way too many lines (especially since there will be more variables than in this example). Thank you!

Solution:

Have a look at the following screenshot:



I have the data in Column A and B.
And I have the following formula in Cell D1:
=query(A:B;"select A,count(B) where B contains 'Y' group by A label count(B) '' ";0)

And if you want to only count the number of times Y appears to the right of A, then I have the following formula in Cell G1:
=counta(iferror(query(A:B;"select A where A = 'A' and B contains 'Y' ";0)))


I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

3 comments:

  1. Thanks Kishan!

    This works great, but unfortunately it seems the example I gave you is too simple and this will not work with my problem. This example is much more similar to what I am trying to solve: https://docs.google.com/a/altimetergroup.com/spreadsheet/ccc?key=0Ao4iG5cRdy4ZdExFTWF4b2tIeVJCV0V4YWJvcjB4aUE#gid=0

    ReplyDelete
    Replies
    1. You can use the following formula in Cell C12:
      =counta(arrayformula(if((B3:B9="Orange")*iferror(search("Sweet";D3:D9);0);"true";"")))

      Delete
    2. And you can also try the following formula in cell C12:

      =counta(iferror(query(A:D;"select B where B = 'Orange' and D contains 'Sweet' ";0)))

      Delete