Create helper column that marks distinct values

hypetaxx

New Member
Joined
Nov 26, 2012
Messages
3
hi,

I have a table with 200.000 rows.

I need to create a helper column, that marks distinct values with 1 and not distinct "" or 0.

I tried the following but both use countif and I believe it is the problem it's just to slow.

=IF(COUNTIF($A$2:A2;A2);1;0)
=1/COUNTIF($A$2:A2;A2)

but both are really slow +20 mins to calculate and many times Excel crashes( 2007 32bit).

The workbook is really simple

Col1 (number formatted as text) - Col2 =distinct


Can anyone help me out I googled, searched the forums, checked out youtube, but I can't seem to find a way that works.
If it is better to do this by VBA and recalculating by a macro then that it is a fine solution as long as it works and doesn't take forever to finish!.

Thanks in advance

BR
Thomas
 
Hi and welcome to the forum,

An easy (and fast) way to get the distinct values is using a PivotTables:
  • Select all your data (including the column header)
  • Go to the Insert Tab, select PivotTable and click OK (or keyboard shortcut Alt > D > P and then Finish)
  • Drag the field name into the Row Labels section
Here are a few useful resources for learning more about PivotTables:
 
Upvote 0
You can use this formula instead:
=IF(MATCH(A2,$A$2:$A$200000,0)=ROW()-1,1,0)

This will tag the first occurrence of your data.

My suggestion is to use match and row functions instead. Lookup functions always return the first occurrence when the lookup value is found multiple times on a range.
Match function returns the position number the lookup value is located while row() function returns the current row the formula is entered.

Countif function is a heavy function since it will be comparing one criteria with a range, which for a 200,000 row data, will eventually crash Excel. It is not advisable to use this for heavy data.
 
Upvote 0
Hi and welcome to the forum,

An easy (and fast) way to get the distinct values is using a PivotTables:
  • Select all your data (including the column header)
  • Go to the Insert Tab, select PivotTable and click OK (or keyboard shortcut Alt > D > P and then Finish)
  • Drag the field name into the Row Labels section
Here are a few useful resources for learning more about PivotTables:


I should have mentioned it, but the helper column is to be used in a pivotable where I need a count of unique ID's.
 
Upvote 0
I should have mentioned it, but the helper column is to be used in a pivotable where I need a count of unique ID's.
Ok, then there are a number of fast alternatives you can consider including:
  • PowerPivot (for Excel 2010+)
  • Using SQL within Excel
  • A VBA solution using Charles Williams' COUNTU UDF
  • The COUNTDIFF function from the MoreFunc add-in
PowerPivot is the easiest in my opinion - see here for how to use it:
For more on the other options see for example:
 
Last edited:
Upvote 0
I have the morefunc add-in using it with much pleasure.

I took a look at the COUNTDIFF can't seem to get it to do what I want, can you assist with how I would build the formula?

Thanks advance
 
Upvote 0
I have the morefunc add-in using it with much pleasure.

I took a look at the COUNTDIFF can't seem to get it to do what I want, can you assist with how I would build the formula?

Thanks advance
To get a count of the unique ID's you should just be able to do something like:
=COUNTDIFF(A2:A200000)

(And use the optional arguments of the function if you want - i.e. to ignore blanks or exclude certain values).
 
Upvote 0

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