+ Reply to Thread
Results 1 to 29 of 29

Unique Count Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Unique Count Problem

    I have many rows (over 100,000). I want to figure out a way to get the unique counts of rows given certain parameters.

    In my data set I have rows: Day, Month, Year, Physician ID, Patient ID, Encounter ID, Procedure Name

    There are multiple procedures per Encounter. Patient IDs occur many times a month.

    I want to know how many unique Patient IDs exist per month, per a certain physician.

    A1 in a different sheet contains the Physician ID
    A2 contains Procedure Name

    A3:A15 are Month Names.
    I want B3:B15 to populate.

    An example of a formula I use sometimes {=SUM(IF(FREQUENCY(IF('EP Data'!I:I>=A3,IF('EP Data'!I:I<=B3,IF('EP Data'!D:D=$A$1,IF(LEFT('EP Data'!E:E,2)="OP",'EP Data'!I:I)))),'EP Data'!I:I),1))} to count unique instances of multiple parameters around a variable. This formula is extremely slow and does not work on a huge dataset.

    Any good way of approaching this general problem would be greatly appreciated.

    Thank you,

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Unique Count Problem

    If you use Array function on huge rows of data its offcourse it will be very slow
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Unique Count Problem

    Hi, vichisov,

    if the data is set up in a way that all information is located on each single row a Pivot Table could be used to update the results. Maybe you can post some sample data in a workbook to have a look at in order to provide some code towards a solution.

    Ciqo,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    Please see attached file for help.

    I would be cycling through all the providers based on a drop list that I would have in G1
    Attached Files Attached Files

  5. #5
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Unique Count Problem

    Don't use entire columns as range references in array formulas.

    Use smaller specific ranges. That should improve the calc time for that specific formula but that type of formula will always be somewhat slow to calc on 100k rows of data.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  6. #6
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    I've done that before where I would sort by patient and then use floating ranges of 1k or something to cut down on iterations. This severely limits my querrying ability when I try doing analysis around other variables on the same data (eg Doc ID).

    Thanks,

  7. #7
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Unique Count Problem

    The fastest user defined function to count unique values that I know of is the COUNTDIFF function in the free Morefunc.xll add-in.

    It'll work in Excel 2007.

    It can be downloaded here:

    http://excelenthusiasts.sharepoint.c...s/Morefunc.zip

    You should still not use entire columns as range references.

  8. #8
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    I have 64-Bit 2013. Is there a version that will work with it?

  9. #9
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Unique Count Problem

    Quote Originally Posted by vichisov View Post
    I have 64-Bit 2013. Is there a version that will work with it?
    Unfortunately, the add-in won't run on 64 bit Excel (Office).

  10. #10
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    Hi vichisov,

    Not sure if this is even close to what you want, but you can check it out if you like. It involves some vba and a lot of formulas that you'd have to put into your workbook and probably still need some help from this forum to tweak it the way you want. There was a big assumption that you'd be filtering your data fields from your sheet1 as provided in your sample attachment. I had to delete some of your data so the workbook wouldn't exceed the 1mb limit to upload. Anyway...hope this helps.
    Attached Files Attached Files
    Last edited by lilsnoop; 01-27-2014 at 01:44 AM. Reason: found an issue with patient ID filter-now fixed

  11. #11
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    I am not seeing any VBA in the attached file. What do you mean?

  12. #12
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    Hi vichisov,

    I'm using excel 2007 on Windows 8 but I believe the the workbook will work for you. The macros used that I was referencing by vba is in module one
    Function SumVisible(WorkRng As Range) As Double
    'Update 20130907
    Dim rng As Range
    Dim total As Double
    For Each rng In WorkRng
    If rng.Rows.Hidden = False And rng.Columns.Hidden = False Then
    total = total + rng.Value
    End If
    Next
    SumVisible = total
    
    
    End Function
    as well as some in the sheet1
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Application.Sheets("Sheet1").Calculate
    
    
    End Sub
    Anyway, the example was meant as an option. There are probably better ways to accomplish what you want but this is the best I could do. Good luck!

  13. #13
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    If the function (i.e. macro used above) doesn't work in a 64-bit version, there might be another way to auto sum the filtered "1's" used to individualize occurrences, in each field you want summarized. As the "1's" are generated off a formula, I had difficulty finding a way to sum them when filtered without the use of the macro above. I'm sure someone might be able to assist you who has more knowledge than I in such formulas. The plus side of finding an alternate method is that you could then hide those columns which contain the "1's", which I could not currently do with this macro. Also of note, I replaced the sample workbook above and replaced it with a newer version. I found that the patient id's when filtered would sometimes render "patient ID" in the summary page and not the actual #. This now has been fixed. If you find a formula to fix the summation of the "1's", I'd be interested to know what you use. Best of luck to you!

  14. #14
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    lilsnoop,

    Why not use a countifs? If something else is generating the flags (1's) it should work if I am understanding what you mean by filtered 1s.

    Honestly this just might be above Excel's pay grade and in reality a SQL problem. I did find http://colinlegg.wordpress.com/2013/...alues-vba-udf/ (apologies if I am violating board rules by posting a link). I got one of the codes to work on a large range, but could not figure out how to incorporate certain filters for other parameters such as Doc ID/Procedure ID/Date Range.

    Thoughts?

  15. #15
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    vichisov,

    The 1's for instance in column "A" are used so that when you filter for example Doctor ID #25 in my sample workbook above, it would count the filtered 1's in column "A", which would then tell you that you have 155 entries for that particular doctor. In an unfiltered state there is 5347 1's in that column which would account for all doctor Id's #'s and entries. Without the macro I couldn't get it sum the filtered 1's correctly; which in this example would be 25. Again, using my example any formula that could replace the macro would need to replace the formula in cells: c1,f1,j1 & m1. I'll continue to see if I can find an alternative in the meantime.
    Last edited by lilsnoop; 01-27-2014 at 07:55 AM.

  16. #16
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    =subtotal(102,Range)

    This would count the number of unhidden elements in the range.

  17. #17
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Unique Count Problem

    Out of curiosity
    Is this any faster?

    Sub Maybe()
        Dim j As Long, lr As Long
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        Application.ScreenUpdating = False
        For j = 1 To 12
            With Sheet1.Cells(1).CurrentRegion
                .AutoFilter
                .AutoFilter Field:=1, Criteria1:=[H1]
    
                With Sheet1.Cells(1).CurrentRegion.SpecialCells(12)
                    .AutoFilter Field:=2, Criteria1:=[H2]
    
                    With Sheet1.Cells(1).CurrentRegion.SpecialCells(12)
                        .AutoFilter Field:=4, Criteria1:=j
    
                    End With
                End With
            End With
            On Error Resume Next
            Range("H" & j + 3).Value = Sheets("Sheet1").Range("A3:A" & lr).SpecialCells(12).Count
            On Error GoTo 0
        Next j
        ActiveSheet.AutoFilterMode = False
        Application.ScreenUpdating = True
    End Sub

  18. #18
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    I tried that but it doesn't work. Not sure if it is because the "1's" are based off a formula themselves, etc. Excel 2010 and I presume excel 2013 have an "Aggregate Function" function that might work, but since I don't have either version I cant test out. Might be worth looking into.


    edited to add: your countif idea does work:
    =COUNTIF(A3:A10000,"1")
    Now if you don't mind the extra columns, we can try to work on your summary monthly breakdown a little better.
    Last edited by lilsnoop; 01-27-2014 at 01:18 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    Ok. Solved your monthly break down issue as well. This workbook contains no macros only formulas. I've explained any shortcomings in the summary tab of this attached workbook. Otherwise I hope it works for you.
    Attached Files Attached Files

  20. #20
    Valued Forum Contributor adyteo's Avatar
    Join Date
    01-10-2013
    Location
    Calgary
    MS-Off Ver
    Excel 2010
    Posts
    540

    Re: Unique Count Problem

    For sure it is not an elegant solution but it is probably the fastest of any solution. Download and use PowerPivot (Microsoft add-in) and use it due to the very high number of records you have. I have created a pivottable based on the powerpivot with your 10,000 rows.
    You can filter by Doctor ID, Procedure ID and select the month you want from the left menu and then you get the unique patient ID count.
    I attach the file (you may need to install PowerPivot before being able to use it)
    Attachment 293107
    Click on the star if you think I helped you

  21. #21
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    Ok. I believe this will do exactly what you want. A lot of formulas though.
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    lilsnoop,

    Thank you for all the time you spent on this. I think ultimately this still wont work. I have 30 or so procedures and another 80 physicians. Manually working through this for various time frames will not work. I am very impressed however by how you manage to count unique cells so quickly.

    I spent much of the day reading up on powerpivot expansion. Seems like it is the way to go. I will give it a try when I get home (can't do it on the work machine). Will see if I can replicate what adyteo suggested. Perhaps the longhand of creating something that will be truly input driven is your solution in conjunction with jolivanes' (still trying to understand what is going on in the code and how to use it) autofiltration.

  23. #23
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Unique Count Problem

    another solution with VBA.
    - macro "Add1Million" = adds 1.000.000 extra random rows of data to that table (takes a while) so that you have a better idea about the speed.
    - then select a doctor and a procedure (or leave those cells empty) and press the green button.
    On my old PC with excel2007 it takes 5 sec to do the counting, so on your 64-bits ???
    - the 3rd macro was for deleting those added rows, otherwise it was too big for here.
    Now you could add an extra macro do make the calculations for your different doctors.
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  24. #24
    Valued Forum Contributor
    Join Date
    03-17-2007
    Location
    Michigan
    MS-Off Ver
    Excel 2021
    Posts
    974

    Re: Unique Count Problem

    No problem. Best of luck to you!

  25. #25
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    Check out bsalv's solution. I think he got it.

  26. #26
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    bsalv,

    Seeing as I am not nearly as proficient as you, what is the general principle that you use to speed this up so dramatically?

  27. #27
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Unique Count Problem

    i do everything in memory and i use dictionaries.
    If you want an introduction on that subject, see http://www.snb-vba.eu/VBA_Dictionary_en.html
    In a dictionary, you can simply add new entries, if they are already present, they don't duplicate, so you just add without checking for doubles, easy.

    As you said earlier, you have 5 million entries and still adding. How is your layout ? Is that 5 tables of 1 million rows ?
    Last edited by bsalv; 01-28-2014 at 06:20 PM.

  28. #28
    Registered User
    Join Date
    04-15-2013
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Unique Count Problem

    Sorry for the slow response. I was away from a computer for some time.

    I tried finding where I said something about 5 million entries and could not find it. In any case, I should have said ~500k. Everything fits in one table.

    Right now I am working on learning what you did and then trying to replicate it for a possibly more complicated application as the example we used does not have much in terms of if parameters.

  29. #29
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,419

    Re: Unique Count Problem

    you're right, perhaps i was confused with another problem here on the site.
    Good luck and if you're still having problems, 1 address.

+ 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] Unique List/Count Problem
    By pauldaddyadams in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-07-2013, 06:27 AM
  2. [SOLVED] How To Count Unique Values in COL A Subject for each unique value in COL B ??
    By amirtehrani in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-06-2012, 03:00 AM
  3. [SOLVED] changing a current mod (count unique visible cells ->count unique visible cells criteria
    By liranbo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2012, 03:58 AM
  4. Replies: 0
    Last Post: 03-22-2012, 08:44 PM
  5. Replies: 17
    Last Post: 08-24-2009, 08:58 AM

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