+ Reply to Thread
Results 1 to 22 of 22

Formula for co-occurrence with counting and percentage?

  1. #1
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Formula for co-occurrence with counting and percentage?

    Hello~

    I want to create a co-occurrence table in Excel that counts and shows the percentage of how many times a specific item comes across other times.

    PRODUCT #1 PRODUCT #2 PRODUCT #3 COLOR
    DAY1 SHIRT PANTS JACKET RED
    DAY2 PANTS JACKET COAT YELLOW
    DAY3 COAT PANTS SHIRT BLUE
    DAY4 JACKET COAT PANTS BLUE
    DAY5 SHIRT PANTS JACKET RED
    DAY6 PANTS SHIRT COAT YELLOW
    DAY7 SHIRT COAT PANTS BLUE

    This is what I have right now and it's what I want to achieve, but it's all manual input...

    SHIRT PANTS COAT JACKET
    SHIRT 0
    PANTS 5 0
    COAT 2 5 0
    JACKET 2 4 2 0

    Can you please help me?

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    Welcome to the forum.

    Please explain the results - for example, the 5 for shirst and pants.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    No. of rows in which they both occur. It'll probably be a horrible MMULT... MMULT /or I can do... MMULT /and is more tricky.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    How about this (not that horrific)?

    =IF(I$1=$H2,0,SUMPRODUCT((MMULT(($B$2:$D$8=I$1)+($B$2:$D$8=$H2),{1;1;1})=2)+0))

    AliGW on MS365 Beta Channel (Windows 11) 64 bit

    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    1
    PRODUCT #1 PRODUCT #2 PRODUCT #3 COLOR SHIRT PANTS COAT JACKET
    2
    DAY1 SHIRT PANTS JACKET RED SHIRT
    0
    5
    3
    2
    3
    DAY2 PANTS JACKET COAT YELLOW PANTS
    5
    0
    5
    4
    4
    DAY3 COAT PANTS SHIRT BLUE COAT
    3
    5
    0
    2
    5
    DAY4 JACKET COAT PANTS BLUE JACKET
    2
    4
    2
    0
    6
    DAY5 SHIRT PANTS JACKET RED
    7
    DAY6 PANTS SHIRT COAT YELLOW
    8
    DAY7 SHIRT COAT PANTS BLUE
    Sheet: Sheet1

  5. #5
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    Better than mine!! Even though I did add on a few "extras"


    =IF(COLUMNS($I2:I2)>ROWS(I$2:I2),"",IF($H2=I$1,0,SUM((MMULT(--($B$2:$D$8=$H2),TRANSPOSE(COLUMN($B$2:$D$8)))>0)*(MMULT(--($B$2:$D$8=I$1),TRANSPOSE(COLUMN($B$2:$D$8)))>0))))

    PS. There was a mistake in your expected answers.
    Attached Files Attached Files

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    Yes, the 2 for coat should have been a 3.

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    So, a best-of-both would read:

    =IF(COLUMNS($I2:I2)>ROWS(I$2:I2),"",IF(I$1=$H2,0,SUMPRODUCT((MMULT(($B$2:$D$8=I$1)+($B$2:$D$8=$H2),{1;1;1})=2)+0)))
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Re: Formula for co-occurrence with counting and percentage?

    Quote Originally Posted by AliGW View Post
    How about this (not that horrific)?

    =IF(I$1=$H2,0,SUMPRODUCT((MMULT(($B$2:$D$8=I$1)+($B$2:$D$8=$H2),{1;1;1})=2)+0))

    AliGW on MS365 Beta Channel (Windows 11) 64 bit

    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    1
    PRODUCT #1 PRODUCT #2 PRODUCT #3 COLOR SHIRT PANTS COAT JACKET
    2
    DAY1 SHIRT PANTS JACKET RED SHIRT
    0
    5
    3
    2
    3
    DAY2 PANTS JACKET COAT YELLOW PANTS
    5
    0
    5
    4
    4
    DAY3 COAT PANTS SHIRT BLUE COAT
    3
    5
    0
    2
    5
    DAY4 JACKET COAT PANTS BLUE JACKET
    2
    4
    2
    0
    6
    DAY5 SHIRT PANTS JACKET RED
    7
    DAY6 PANTS SHIRT COAT YELLOW
    8
    DAY7 SHIRT COAT PANTS BLUE
    Sheet: Sheet1
    Hello,

    Thank you for your quick reply!!

    I have a problem with your formula, meaning that I$1 is not changing into J$1, K$1, and so on. I'm using MS2019 and I'm not sure if that's causing the issue.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    It will when you drag copy it across and down. See attached.
    Attached Files Attached Files

  10. #10
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    In your version of Excel, this is probably an array formula.

    So, please refer to the file. If the formulae are enclosed within a pair of { }, these are array formulae.

    These are a little different from ordinary formulae in that they MUST be confirmed in the FIRST CELL ONLY by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. After that, the array can be dragged down as normal, to cover the desired range.

    You will know the array is active when you see the curly brackets { } appear around the outside of your formula. If you do not use CTRL+SHIFT+ENTER you will (almost always) get an error message or an incorrect answer. Press F2 on that cell and try again.

    Don't type the curly brackets yourself - it won't work...

  11. #11
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    Can the same product occur more than once in each row?

    Eg can shirt occur twice in row 4? If so, does it count as 1 or as 2 rows????

  12. #12
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    Ali's formula will calculate it as 2 occurrences... mine (Post 5) as ONE.

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    Some comment from the OP at this point would be helpful - are either of us on the right track here?

  14. #14
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Re: Formula for co-occurrence with counting and percentage?

    Quote Originally Posted by Glenn Kennedy View Post
    Can the same product occur more than once in each row?

    Eg can shirt occur twice in row 4? If so, does it count as 1 or as 2 rows????
    It shouldn't appear multiple times in the same row.

    I have a pretty big worksheet (+2K rows) that's similar to my first example, but the formulas are not working, even if I copy-paste them and then CTRL+ALT+ENTER; they either show 0 (zero) or #VALUE! (error).

  15. #15
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    We cannot diagnose an invisible problem. We need to see a sheet. CTRL SHIFT enter

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    Are you extending the ranges appropriately? Are there just three product columns or more? If more, how many?

  17. #17
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Re: Formula for co-occurrence with counting and percentage?

    Quote Originally Posted by Glenn Kennedy View Post
    We cannot diagnose an invisible problem. We need to see a sheet. CTRL SHIFT enter
    Sorry for my typo.

    Quote Originally Posted by AliGW View Post
    Are you extending the ranges appropriately? Are there just three product columns or more? If more, how many?
    There are 7 columns in total, but 6 are to be used for data.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    So SIX product columns?

    Try this:

    =IF(I$1=$H2,0,SUMPRODUCT((MMULT(($B$2:$G$2000=I$1)+($B$2:$G$2000=$H2),{1;1;1;1;1;1})=2)+0))

    Extend the ranges further, if required.

  19. #19
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,002

    Re: Formula for co-occurrence with counting and percentage?

    Use this:

    =IF(COLUMNS($I2:I2)>ROWS(I$2:I2),"",IF(I$1=$H2,0,SUMPRODUCT((MMULT(($B$2:$D$8=I$1)+($B$2:$D$8=$H2),TRANSPOSE(COLUMN($B$2:$D$8))^0)=2)+0)))

    with the ranges changed appropriately.

  20. #20
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Re: Formula for co-occurrence with counting and percentage?

    I edited the final codes with {1;1;1;1;1;1} and now both of your formulas are working.

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,653

    Re: Formula for co-occurrence with counting and percentage?

    Glad to have helped.

    If that takes care of your original question, please choose Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank those who have helped you by clicking the small star icon (* Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.

  22. #22
    Registered User
    Join Date
    08-15-2022
    Location
    US
    MS-Off Ver
    2019
    Posts
    6

    Re: Formula for co-occurrence with counting and percentage?

    Thank you very much, guys!! You're awesome!! <3<3<3

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Counting occurrence streaks of '2's between three '1's
    By Hein Htut Oo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2022, 02:15 PM
  2. Need to calculate percentage of an occurrence
    By sm370 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-13-2017, 01:54 AM
  3. [SOLVED] Percentage Occurrence (Possibly Frequency function)
    By adrem7 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-08-2013, 06:33 PM
  4. Calculating percentage occurrence of numbers
    By jordi55 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-20-2012, 10:43 PM
  5. [SOLVED] Formula for counting occurrence of dates
    By BlueScoob60 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 08-08-2012, 07:35 AM
  6. Excel 2007 : Counting Occurrence of value across workbooks
    By jonwickert in forum Excel General
    Replies: 0
    Last Post: 05-03-2012, 10:47 AM
  7. Replies: 4
    Last Post: 09-24-2009, 09:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1