Question

It's my first question here, so please don't kill me if something is wrong. I have found numerous solutions on this site, but not this time. Unfortunately I can't post images yet. It won't be easy, but I will try.

To the point:

My data has the following headers:

Decision_Id Opponent1 Opponent2 Opponent3 Suitor1 Suitor2 Suitor3 Suitor4

Decision_id is a unique integer identifier. The rest are strings.

Each row represents a particular judicial decision. Each Decision can have UP TO 3 opponents (defending party) and UP TO 4 suitors (attacking party).A particular party can be a suitor in one decision and an opponent in another one.

What I want to get :

Cross-table where both rows and columns headers are all distinct parties I encounter in the table. (no problem with that, done.) Where each cell shows in how many distinct decisions a particular opponent (defined by row header) was attacked by a particular suitor (column header) => All diagonal cells equal ZERO (a party can't attack itself) and table is not symmetric.

I have tried

to apply to the first cell and than expand:

=COUNTIFS("Fixed range of all opponents :$B$2:$D$6","the wanted opponent value : $A2", "Fixed range of all suitors :$E$2:$H$6", "the wanted suitor value : B$1")

I had an error. I figured out that criteria ranges have to be of the same size. OK, created dummy empty columns => no error, BUT, the results are clearly underestimated. I think that there is a match only if opponent and suitor have the same "number". In details: For each row excel tests the opponent1 and suitor1 towards corresponding values, then opponent2 and suitor2, then opponent3 and suitor3... This actually explains why the ranges have to be of the same size.

So, What I would need

Is, for each row, to make excel test all opponents towards the wanted opponent value, test all suitor towards the wanted suitor value. If at least one opponent and one suitors correspond, give it a match and count this decision.(Even though opponent1 and suitor3 had the wanted values)

Remarks

I have already made a VBA code which does the job, but it's too slow (around 5 hours for the whole table) and I expect to do the same for different tables of this kind and/or modify this one. So I am interested in "pure excel", fast solution.

Thank you very much!

Was it helpful?

Solution

The difficult part here is to separate multi-column ranges into separate rows - one way to do that is with OFFSET within COUNTIF, i.e. this formula

=SUMPRODUCT(COUNTIF(OFFSET($B$2:$D$6,ROW($B$2:$D$6)-ROW($B$2),0,1),$A2),COUNTIF(OFFSET($E$2:$H$6,ROW($E$2:$H$6)-ROW($E$2),0,1),B$1))

That assumes that all suitors are different on any one row and all opponents are different on any one row (although formula can be modified if that isn't the case).

You can extend the ranges to any size you want - although the number of rows must be the same for each part

....or here's another more obscure way using MMULT function

=SUMPRODUCT(MMULT(($B$2:$D$6=$A2)+0,{1;1;1}),MMULT(($E$2:$H$6=B$1)+0,{1;1;1;1}))

the {1;1;1} and {1;1;1;1} represent the number of columns in each section so if you have 6 and 8 those need to be changed accordingly

OTHER TIPS

Another possibility is to try this array formula:

=SUM(MMULT(-TRANSPOSE($B$2:$D$6=$A2),-($E$2:$H$6=B$1)))

entered using CTRL+SHIFT+ENTER (or defined as a name and entered normally eg =Total.)

This should do it:

=       COUNTIFS($B$2:$B$6,$A2, $E$2:$E$6, B$1) 
      + COUNTIFS($C$2:$C$6,$A2, $E$2:$E$6, B$1)
      + COUNTIFS($D$2:$D$6,$A2, $E$2:$E$6, B$1)
      + COUNTIFS($B$2:$B$6,$A2, $F$2:$F$6, B$1)
      + COUNTIFS($C$2:$C$6,$A2, $F$2:$F$6, B$1)
      + COUNTIFS($D$2:$D$6,$A2, $F$2:$F$6, B$1)
      + COUNTIFS($B$2:$B$6,$A2, $G$2:$G$6, B$1)
      + COUNTIFS($C$2:$C$6,$A2, $G$2:$G$6, B$1)
      + COUNTIFS($D$2:$D$6,$A2, $G$2:$G$6, B$1)
      + COUNTIFS($B$2:$B$6,$A2, $H$2:$H$6, B$1)
      + COUNTIFS($C$2:$C$6,$A2, $H$2:$H$6, B$1)
      + COUNTIFS($D$2:$D$6,$A2, $H$2:$H$6, B$1)

These look simpler if you make your data into a table, or define named ranges for the Opponent1, Opponent2, Suitor1 columns etc...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top