+ Reply to Thread
Results 1 to 28 of 28

COUNTIF/SUMPRODUCT(?) to create a matrix from data

  1. #1
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    COUNTIF/SUMPRODUCT(?) to create a matrix from data

    I have some data from a parking survey. The data shows the time slots in which each vehicle was present, with an individual code for each vehicle. I'd like to create a matrix with the arrival time slots on one axis, the departure time slots in another axis and the number of vehicles arriving and departing in each slot.

    An extract from the raw survey data is below. Looking at row 60 for example, vehicle SE65 arrived at 0700 and departed at 0830, so I would like a '1' in my matrix at the intersection of 0700 and 0830. Similarly, vehicles SB13 and SA69 arrived at 0830 and departed at 0830 so I would like a '2' in my matrix at the intersection of 0830 and 0830; vehicles ROC62 and SP07 both arrived at 0830 and departed at 0900, so I'd like a '2' in the matrix at the intersection of 0830 and 0900.
    Capture.PNG

    The logical test to get the arrival time is if the text appears in a cell but not in the adjoining cell to the left and for the departure time is if the text appears in a cell but not the adjoining cell to the right. I have been trying various SUMPRODUCT functions but can't get anything to work correctly. Any ideas on how to solve this?

  2. #2
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    A picture is worth 1000 words, a sample sheet is worth 1000 pictures!!

    You have attached a non-editable picture of an Excel sheet. That's not very easy to work with. Also... I'm lazy. I have to re-type your information before I can begin to address your problem. That puts me off completely.

    So.... Do yourself a favour and please attach a SMALL sample Excel workbook (10-20 rows of data is usually enough). However, please give us an indication of the approximate number of rows of data you want the solution to work with (100, 1000, 100,000 or whatever).

    Unfortunately the attachment icon doesn't work at the moment. So,... instead click on Go Advanced (below the Edit Window) while composing your reply, then scroll down to and click on Manage Attachments and the Upload window will open. Click on Browse and navigate to (and double-click) the file icon that you want to attach, then click on Upload and then on Close this Window to return to the Edit window. When you have finished composing your post, click on Submit Post.

    Make sure confidential information is removed first!!!!
    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

  3. #3
    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,906

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Welcome to the forum.

    Please read the yellow banner at the top of the page.
    Last edited by AliGW; 11-23-2019 at 12:52 PM.
    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.

  4. #4
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Apolgies. The data is approx. 130 rows by 61 columns. Extract from the data attached, with the output sheet showing an example of one cell of what I'm trying to achieve.
    Attached Files Attached Files

  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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    This looks horrible. It is horrible. But... it does seem to work... I couldn't think of a prettier way to do it!!

    Someone will do it with a punchy one-line formula, and put me to shame!!!

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    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,906

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    I was trying MMULT - not managed it yet.

  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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    MMULT still scares me a bit!!

  8. #8
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Thank you very much Glenn, much appreciated. My matrix is larger than the extract I posted (the times extend to 2200 rather than upto 0930 as I posted) so I'll need to have a think about how to scale the solution to capture the entire dataset. However, you've given me a new avenue to explore - I was bogged down in my own SUMPRODUCT formulae that were stubbornly refusing to work.

  9. #9
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    It shouldn't be too difficult.

    There are only 3 ranges in the formula:
    Header row
    First data row
    Last data row.

    The main thing is... is it working on your sample as desired?

    If so, you're welcome. If not, shout.



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

    It'd also be appreciated if you were to click the Add Reputation button at the foot of any of the posts of all members who helped you reach a solution.

  10. #10
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    It's just about working in my sample thanks, but there are a couple of odd results. For example, the matrix shows four vehicles arriving at 0830 and leaving at 0915, whereas only three do (SD19, J333 and SL61) and it shows one vehicle arriving at 0915 but departing at 0900.
    Last edited by AliGW; 11-23-2019 at 05:16 PM. Reason: Please don’t quote unnecessarily.

  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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    I'm away now. Beer o'clock. Back tomorrow.

  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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    OK. I set up 6 Named ranges to make the formula look simpler. CTRL-F3 to view edit. They select the range of the table for each action needed in SUMPRODUCT.

    They are all based on the use of INDEX;MATCH to select the column of the arrival time the column to the left of it and the column to the right of it (A_Rng0, A_RngMinus and A_RngPlus) and similarly for the departure time. The SUMPRODUCT then looks like this:

    =IFERROR(IF(E$5<$D6,"",SUMPRODUCT((A_Rng0<>"")*(A_Rng0<>A_RngMinus)*(D_Rng0<>"")*(D_Rng0<>D_RngPlus))),"")

    REMEMBER the Named Ranges MUST extend ONE column to the RIGHT of the last column with data.

    N.B. You said: "the matrix shows four vehicles arriving at 0830 and leaving at 0915, whereas only three do (SD19, J333 and SL61)". In fact there are four. You forgot to count SH66.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Thank you very much Glenn. The bottom right cell of my data is BN131. I have added all the data into the worksheet you provided and updated where appropriate the references in the named ranges to refer to row 131 and column BO (ie one after BN, the last column with data). However, I'm getting some odd and incomplete results and wonder if you'd be so kind as to point out where I've gone wrong?
    Attached Files Attached Files

  14. #14
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Back in an hour.

  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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Back again.

    In fact, you have done nothing wrong. Strange things can happen when copy/pasting times. It's due either to time being expressed to a different number of dps or to the fact that excel can't count. (See link). It can be fixed int he formual using some clunky rounding. However, since (in this case) all 3 time axes were identical, I just selected the time axis in your raw data copy/paste special ± transpose into your output. As if by magic, all the blanks disappearred.

    Hopefully, that's everything sorted.

    If so, you're welcome. If not, shout.



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

    It'd also be appreciated if you were to click the Add Reputation button at the foot of any of the posts of all members who helped you reach a solution.

  16. #16
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Ooops. Your file this time.
    Attached Files Attached Files

  17. #17
    Valued Forum Contributor Sadath31's Avatar
    Join Date
    03-02-2011
    Location
    Dammam, Saudi Arabia
    MS-Off Ver
    Office 365
    Posts
    452

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Hi

    Please See the the below formula gives you the desired result

    Please Login or Register  to view this content.
    Attached Files Attached Files

  18. #18
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data


  19. #19
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Quote Originally Posted by Sadath31 View Post
    Hi

    Please See the the below formula gives you the desired result

    Please Login or Register  to view this content.
    Quote Originally Posted by Glenn Kennedy View Post
    Oh, here's the link, as well....
    Thanks very much both. On your solution Glenn, I noticed that there were some odd results in the 0700 row (eg 55 vehicles 0700>0830) but there are so few vehicles at 0700 that I can deal with them manually (perhaps a blank column to the left of 0700 would resolve that).

  20. #20
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Eh? There are no 55's on the sheet I posted @ 16??!!

    I see them now!! Give me 5 mins.

  21. #21
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    That was me being monstrously stupid!!!
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Thanks very much!

  23. #23
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Check it over. The problem was that I foolishly forgot to extend the raw data raneg BACK one column as well as FORWARD one column. All the NR's are (hopefully) OK now.

    Apols. Must be going senile.

  24. #24
    Valued Forum Contributor Sadath31's Avatar
    Join Date
    03-02-2011
    Location
    Dammam, Saudi Arabia
    MS-Off Ver
    Office 365
    Posts
    452

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    HI
    please see the below formula

    HTML Code: 
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Quote Originally Posted by Glenn Kennedy View Post
    Check it over. The problem was that I foolishly forgot to extend the raw data raneg BACK one column as well as FORWARD one column. All the NR's are (hopefully) OK now.

    Apols. Must be going senile.
    Glenn: I was checking through the data for this and there seem to be a few spurious results. For example, we know that only four vehicles were there at 0700 (GO11, SE65, PN15 and KN14). However, on the 'output' tab, the sum of row 6 (the vehicles that arrived at 0700) is 29, implying that there were 29 vehicles that were there at 0700 and departed at various times throughout the day. For example, one vehicle is shown arriving at 0700 and departing at 1100, but that doesn't correspond to any of the departure times of vehicles GO11 (1500), SE65 (0830), PN15 (1230) or KN14 (1200). The arrival and departure times of those four vehicles appear to be showing correctly in the matrix, but there seem to be some additional spurious results too. Any thoughts?

    Quote Originally Posted by Sadath31 View Post
    HI
    please see the below formula

    HTML Code: 
    Thank you Sadath31.

  26. #26
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Grrr.

    Normally, I overcomplicate things. This time, I undercomplicated the problem. Once I saw what was causing the problem, it was easy enough to fix. (I was not checking that the vehicle departing was the same as the one that arrived - if vehicle A arrived at 0700, and left at 0800, it was counted. If another subsequently arrived and left at 1100, I had not checked that it was the same vehicle that had arrived at 0700). I deleted two of the Named ranges, as they are notrequired. So, with a red face, I have now cross-checked my formula as well as I can. I am now confident that we're there. It might be an idea to keep the cross-checking formulae as an ongoing QA measure.
    Attached Files Attached Files

  27. #27
    Registered User
    Join Date
    11-23-2019
    Location
    Edinburgh, Scotland
    MS-Off Ver
    365
    Posts
    19

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    That looks right - thanks very much for all your help.

  28. #28
    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,055

    Re: COUNTIF/SUMPRODUCT(?) to create a matrix from data

    Phew!!! That was a bit of a marathon, but we got there in the end!!

+ 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. [SOLVED] Sumproduct multiple criteria query
    By Millixcel in forum Excel General
    Replies: 3
    Last Post: 05-03-2018, 07:24 AM
  2. Create a new table (or matrix) from an exported set of data from a website.
    By dherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2014, 11:19 AM
  3. Replies: 6
    Last Post: 11-18-2013, 11:28 PM
  4. Trying to create an output grid/matrix with custom data.
    By JoeTheBro in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-28-2013, 10:47 AM
  5. Replies: 3
    Last Post: 10-18-2010, 11:09 AM
  6. Create a matrix from data in three column
    By sa02000 in forum Excel General
    Replies: 3
    Last Post: 06-27-2006, 09:30 PM

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