EXCEL- Count instances that have matching combinations

拜拜、爱过 提交于 2020-01-05 03:46:13

问题


I’m pretty good at excel but for some reason can’t find the right solution to this problem (maybe I’m over complicating it):

Basically I have 2 columns with info like below:

Column 1    Column 2
A                  Red
A                  Blue
A                  Yellow
B                  Blue
B                  Green
B                  Brown
C                  Red
C                  Blue
C                  Brown

What I want is to create a cross tab type of table which counts how many items in column 1 have the correct combination of values in column 2

For instance, I’d expect the result below:

         Red      Blue     Brown
Red   2            2            1

Red-Red would evaluate to 2 because there are 2 instances in column 1 where the items have Red in Column 2

Red-Blue would evaluate to 2 because there are two instances where the values in column A have values of both Red and Blue in column 2 (A-Red/A-Blue and C-Red/C-Blue)

Red-Brown would evaluate to 1 because C is only value in Column 1 that has a value of Red and Brown in Column 2

I hope that’s clear. I’ve tried multiple combinations of countifs/sumifs but haven’t had any luck getting the desired result. :(


回答1:


Formula based approach

The formula in Cell E2 in my solution is as below, and as it is an array formula you need to press Ctrl+Shift+Enter to make it work.

{=SUM(--ISNUMBER(MATCH(IF((Column_1&$D2=Column_1&Column_2),Column_1)&E$1,Column_1&Column_2,0)))}

Please note I have added some sample data to your original set, and I've named the following ranges:

Column_1 stands for all data in the first column A;

Column_2 stands for all data in the second column B.

My logic is to

  1. Use IF function and = to find out, for a given color in Column D, what is the corresponding value in Column_1? If we are looking at Red in cell D2, my IF function will return the following {"A";FALSE;FALSE;FALSE;FALSE;FALSE;"C";FALSE;FALSE;FALSE;"D";FALSE;FALSE;FALSE;FALSE};

  2. Use & to combine the range from last step with the look up value in Row 1. If we combine the range with Red in cell E1, we will have {"ARed";"FALSERed";"FALSERed";"FALSERed";"FALSERed";"FALSERed";"CRed";"FALSERed";"FALSERed";"FALSERed";"DRed";"FALSERed";"FALSERed";"FALSERed";"FALSERed"};

  3. Next step is to MATCH the range from last step with the given combination which is Column_1&Column_2 that represents ARed, ABlue, AYellow, BBlue, BGreen, etc. Continue with my example the results would be {1;#N/A;#N/A;#N/A;#N/A;#N/A;7;#N/A;#N/A;#N/A;11;#N/A;#N/A;#N/A;#N/A} in which the numerical values are the positions of ARed, CRed, and DRed in the range of the given combination Column_1&Column_2.

  4. The last step to use ISNUMBER to find out how many values within the range from last step are number, which shall return {TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;FALSE} and then SUM up all TRUE results by converting them into numerical value 1 using --.

Power Query Based Approach

Using the same source data in my solution, A1:B16, load this to the Power Query Editor, then you should have the following:

Please note I have sorted the table by Column 2 and then Column 1 ascending consecutively. This is an optional step just to help making the following steps easier to be understood.

The next step is to merge the table with its own by matching Column 1:

Expand the new column to show Column 2:

Highlight the last column, use Pivot Column function to transform the table, then you should have:

The only problem is that the colors will be positioned differently to the source.

If it is important to keep the colors in original order, one way of doing that is to

  1. create a look up table showing the desired order of the colors with an index column;
  2. merge this look up table before pivot column in the first query, and match the colors in the third column, expand and sort the index column and then remove it;
  3. do the Pivot Column, and then merger the look up table again, and expand, sort and remove the index column again.

Here are the codes behind the scene for reference only:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column 1", type text}, {"Column 2", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Column 2", Order.Ascending}, {"Column 1", Order.Ascending}}),
    #"Merged Queries" = Table.NestedJoin(#"Sorted Rows", {"Column 1"}, #"Sorted Rows", {"Column 1"}, "Filtered Rows", JoinKind.LeftOuter),
    #"Expanded Filtered Rows" = Table.ExpandTableColumn(#"Merged Queries", "Filtered Rows", {"Column 2"}, {"Filtered Rows.Column 2"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Filtered Rows", List.Distinct(#"Expanded Filtered Rows"[#"Filtered Rows.Column 2"]), "Filtered Rows.Column 2", "Column 1", List.Count)
in
    #"Pivoted Column"

Let me know if you have any questions. Cheers :)




回答2:


This is a version using Sum and Countifs (maybe the sort of thing that @AlexT82 was thinking of):

=SUM(COUNTIFS($A$2:$A$10,$A$2:$A$10,$B$2:$B$10,IF($B$2:$B$10=$D2,E$1)))

Must be entered using CtrlShiftEnter

Assumes that each colour only occurs once for each item in Column 1.

You should be able to correct for any repeats within an item by dividing by the number of times they occur within each item:

=SUM(IFERROR(COUNTIFS($A$2:$A$12,$A$2:$A$12,$B$2:$B$12,IF($B$2:$B$12=$D2,E$1))
/COUNTIFS($A$2:$A$12,$A$2:$A$12,$B$2:$B$12,$D2)/COUNTIFS($A$2:$A$12,$A$2:$A$12,$B$2:$B$12,E$1),0))




回答3:


You can use the solution below that uses a helper table that counts what you're looking for:

The formula for the cell F12 on the helper table is:

=IF(COUNTIFS($B$3:$B$12,$E12,$C$3:$C$12,F$11)>0,1,0)

And the formula on the Final Result table:

 =IF($E4<>F$3,SUMPRODUCT(INDEX($F$12:$J$14,0,MATCH($E4,$F$11:$J$11)),INDEX($F$12:$J$14,0,MATCH(F$3,$F$11:$J$11))),COUNTIF($C$3:$C$12,$E4))

Note: This is a recycled answer from a similar question, see the original.



来源:https://stackoverflow.com/questions/57500746/excel-count-instances-that-have-matching-combinations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!