Looking for unique values with 2 conditions

Zibi

Board Regular
Joined
Feb 2, 2012
Messages
73
Hello,

can someone please help me? I need to look through 300K rows and identify unique count if 2 conditions are meet. Column A has dates which would be used to identify as unique column B contains Day mane witch it is the 1 st criteria and column C has store ID witch it is 2 criteria.

ABCDEFGHIJKLM
1DateDaySTORE_IDStoreDayDayDayDayDayDayDay
212/28/2017Thursday12ThursdayFridaySaturdaySundayMondayTuesdayWednesday
312/28/2017Thursday2
412/28/2017Thursday2Output400300
512/28/2017Thursday3
61/4/2018Thursday5
71/4/2018Thursday2
81/4/2018Thursday1
91/11/2018Thursday2
101/11/2018Thursday2
111/18/2018Thursday1
121/18/2018Thursday5
131/25/2018Thursday6
141/25/2018Thursday2
151/25/2018Thursday2
1612/28/2017Monday1
1712/28/2017Monday8
1812/28/2017Monday9
1912/28/2017Monday6
201/4/2018Monday5
211/4/2018Monday2
221/4/2018Monday1
231/11/2018Monday2
241/11/2018Monday2
251/18/2018Monday1
261/18/2018Monday5
271/25/2018Monday6
281/25/2018Monday2
291/25/2018Monday2

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:

ABCDEFGHIJKLM
1DateDaySTORE_IDStoreDayDayDayDayDayDayDay
212/28/2017Thursday12ThursdayFridaySaturdaySundayMondayTuesdayWednesday
312/28/2017Thursday2
412/28/2017Thursday2Output4000300
512/28/2017Thursday3
61/4/2018Thursday5
71/4/2018Thursday2
81/4/2018Thursday1
91/11/2018Thursday2
101/11/2018Thursday2
111/18/2018Thursday1
121/18/2018Thursday5
131/25/2018Thursday6
141/25/2018Thursday2
151/25/2018Thursday2
1612/28/2017Monday1
1712/28/2017Monday8
1812/28/2017Monday9
1912/28/2017Monday6
201/4/2018Monday5
211/4/2018Monday2
221/4/2018Monday1
231/11/2018Monday2
241/11/2018Monday2
251/18/2018Monday1
261/18/2018Monday5
271/25/2018Monday6
281/25/2018Monday2
291/25/2018Monday2
30

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet4

Array Formulas
CellFormula
G4{=SUM(SIGN(FREQUENCY(IF(($C$2:$C$29=$F$2)*($B$2:$B$29=G2),$A$2:$A$29),$A$2:$A$29)))}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Put the array formula in G4, confirm with Control+Shift+Enter. Then drag right as needed. Let us know how it works.
 
Upvote 0
Hi Eric, thanks for the reply to my post, I did try that formula. It works but for large data set i am working with it takes forever to calculate. I am hoping that VBA would be able to do this more efficient.
again Thank you.
 
Upvote 0
You didn't say if you wanted a UDF (a user defined function to use in place of the formula in G4), or a regular macro that calculates the values and places them on the sheet. The regular macro would be faster, and cause less delay in your spreadsheet, but it only runs when called. I took that approach.

Open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. On the sheet that opens, paste this code:

Rich (BB code):
Sub CountUniques()
Dim MyRange As Range, MyTable As Range, MyTab As Variant, MyDictofDicts As Object, i As Long, MyData As Variant

    Set MyRange = Sheets("Sheet4").Range("A:C")
    Set MyTable = Sheets("Sheet4").Range("F1:M4")
    
    MyData = MyRange.Resize(MyRange.Resize(1, 1).Offset(Rows.Count - 1).End(xlUp).Row).Value
    MyTab = MyTable.Value
    
    Set MyDictofDicts = CreateObject("Scripting.Dictionary")
    For i = 1 To 7
        MyDictofDicts.Add MyTab(2, i + 1), CreateObject("Scripting.Dictionary")
    Next i
    
    For i = 2 To UBound(MyData)
        If MyData(i, 3) = MyTab(2, 1) Then MyDictofDicts(MyData(i, 2))(MyData(i, 1)) = 1
    Next i
    
    For i = 1 To 7
        MyTab(4, i + 1) = MyDictofDicts(MyTab(2, i + 1)).Count
    Next i
    MyTable = MyTab
    
End Sub
Change the ranges in red to match your sheet. Switch back to Excel. Press Alt-F8, choose CountUniques, and press Run.

Let me know if this works for you, or if you want a UDF.
 
Upvote 0

Forum statistics

Threads
1,215,879
Messages
6,127,518
Members
449,385
Latest member
KMGLarson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top