Extracting unique values from a column in a table - am I doing this as efficiently as possible?

diversification

New Member
Joined
Jun 24, 2020
Messages
37
Office Version
  1. 365
Platform
  1. Windows

^ This is an example of the data I'm using. In reality, Sheet1 is ~30,000 Rows deep, and Sheet2 is ~800 Rows deep.

What I'm trying to accomplish:
  • Table1 (in Sheet1) is the raw data showing a ton of Account#s with the Symbols they hold, and for each Symbol, it breaks out the purchase lots across multiple rows, and specifies whether each lot has an Unrealized Gain or Loss associated with it.
  • Table 2 (in Sheet2) has a greatly reduced subset of the same Account#s and Symbols in Table1. Each unlike in Table1 which has the same Account# & Symbol combination appearing in multiple rows, each combination of Account# and Symbol only appears in one single row in Sheet2.
  • For each row in Table2, I want the following:
    • Match the Account# and Symbol to all occurrences in Table1
    • For all matches, sum separately the Short Unrealized Gain or Loss and the Long Unrealized Gain or Loss
    • In a new column in Sheet2, combine the following items: (1) The Symbol from that row (2) The Value from that row (3) The aforementioned summed Short Unrealized Gain or Loss (4) The aforementioned summed Long Unrealized Gain or Loss

My Steps:

  1. In Table1, Column V, I create a column titled "Acct, Ticker, L/S" which combines the Account #, the Symbol, and the type of Unrealized Gain (Long or Short) into one cell using the following formula:
    Excel Formula:
    = TEXT(TEXTJOIN(", ", TRUE,TRIM([@[Account '#]]), TRIM([@Symbol]), TRIM([@[Short or Long Term]])),)
  2. In Sheet1, Column Z, outside of Table1, I created a column titled "Unique Acct, Ticker, L/S". This column isolates the unique values from the "Acct, Ticker, L/S" column from Step 1 (you'll recall that I said in Table1, there are multiple occurrences of the same combination of Account #, Symbol, and the 'Long' or 'Short' designation.
    Excel Formula:
    =UNIQUE(Table1[Acct, Ticker, L/S])
  3. In Sheet1, Column AA, outside of Table1, I created a column titled "G/L Total". Using the neighboring cell in Column Z (Step 2 - "Unique Acct, Ticker, L/S") this formula looks for matches in Column V (Step 1 - "Acct, Ticker, L/S") and sums the Unrealized Gain or Loss from Table1[Unrealized Gain Loss] (Column P).
    Excel Formula:
    =SUMIF(Table1[Acct, Ticker, L/S],@Z:Z,Table1[Unrealized Gain Loss])
  4. In Table2, Column M, I created a column titled "Unrealized Short G/L" looks at the Table2[Account Number], Table2[Symbol] and uses the following formula to pull in the associated "G/L Total" (Step 3 - Sheet1, Column AA) from Sheet1.
    Excel Formula:
    =INDEX(Sheet1!$AA:$AA, MATCH((TRIM([@[Account Number]])&", "&TRIM([@Symbol])&", Short"),Sheet1!$Z:$Z, 0))
  5. In Table2, Column N, I created a column titled "Unrealized Long G/L" and did the same exact thing as in Step 4, except this time it looks for "Long" instead of "Short." =INDEX(Sheet1!$AA:$AA, MATCH((TRIM([@[Account Number]])&", "&TRIM([@Symbol])&", Long"),Sheet1!$Z:$Z, 0))
    Excel Formula:
    =INDEX(Sheet1!$AA:$AA, MATCH((TRIM([@[Account Number]])&", "&TRIM([@Symbol])&", Long"),Sheet1!$Z:$Z, 0))
  6. In Table2, Column P ("Symbol, Val, G/L"), for each row, I combine the corresponding values from columns F, H, M, N ("Symbol", "Value", "Unrealized Short G/L", "Unrealized Long G/L"). This gives me what I wanted.
    Excel Formula:
    =[@Symbol]&" -- Current Value: "&TEXT([@Value],"$#,##0;($#,##0)")&",   Short: "&IFERROR(TEXT([@[Unrealized Short G/L]],"$#,##0;($#,##0)"),TEXT(0,"$0"))&",   Long: "&IFERROR(TEXT([@[Unrealized Long G/L]],"$#,##0;($#,##0)"),TEXT(0,"$0"))

QUESTION:
Is there a better, more efficient, and less resource-intensive way to find the unique values in Step 2 WITHOUT a macro / VBA code? I don't understand that stuff well, so if it breaks I'm going to be back here trying to get troubleshooting help, and that could cause too much of a delay for something I already have working (albeit slowly.) I'm guessing the reason this particular formula slows down the sheet so much is because it's having to check each of the ~30,000 cells in the column against the already-found unique values, which obviously is a ton of work. I'd also like to avoid doing it manually using the "eliminated duplicates" button or whatever it is.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
its easier understood with a snapshot of the excel sheet atleast
 
Upvote 0
For M2 on sheet 2, you could use
Excel Formula:
=SUM(FILTER(Table1[Unrealized Gain Loss],(Table1[Account '#]=[@[Account Number]])*(Table1[Symbol]=[@Symbol])*(Table1[Short or Long Term]="Short"),0))
 
Upvote 0
Did you see my suggestion in post#3?
 
Upvote 0
I included the dropbox link to the workbook at the very top. Is that not preferable to a snapshot?
the way i see that data, its possible to use formulas but having some cells that take raw data and the rest have formulas to receive it. for me it will take time to make it all happen but its true to say formulas are possible.
 
Last edited:
Upvote 0
You can get rid of cols Z & AA & col V if you don't it & just use

unrealized cap gain loss finder project.xlsx
FGHIJKLMNOP
1SymbolBlah4ValueBlah5Blah6Blah7Blah8Unrealized Short G/LUnrealized Long G/LBlah9Symbol, Val, G/L
2AFOJdata$36,514,654.00datadatadatadata$67.20$0.00dataAFOJ -- Current Value: $36,514,654, Short: $67, Long: $0
3GIOJNSdata$351.25datadatadatadata$12.15$85.00dataGIOJNS -- Current Value: $351, Short: $12, Long: $85
4OAGIdata$444.00datadatadatadata$115.61($28.50)dataOAGI -- Current Value: $444, Short: $116, Long: ($29)
5OREUTYdata$6,668.44datadatadatadata$908.98$118.38dataOREUTY -- Current Value: $6,668, Short: $909, Long: $118
6RRRdata$56,165.00datadatadatadata($567.45)($2,324.85)dataRRR -- Current Value: $56,165, Short: ($567), Long: ($2,325)
7AOKFJdata$596,849,648.00datadatadatadata$2,171.85$58.33dataAOKFJ -- Current Value: $596,849,648, Short: $2,172, Long: $58
8ALDFSKJdata$81,647.96datadatadatadata$330.26$1,016.37dataALDFSKJ -- Current Value: $81,648, Short: $330, Long: $1,016
9TOIJFdata$56,648.11datadatadatadata$6,076.89$0.00dataTOIJF -- Current Value: $56,648, Short: $6,077, Long: $0
10SOFKdata$46,165.66datadatadatadata$1,938.09$1,422.16dataSOFK -- Current Value: $46,166, Short: $1,938, Long: $1,422
11AFOJdata$146,161.98datadatadatadata$1,041.52($955.92)dataAFOJ -- Current Value: $146,162, Short: $1,042, Long: ($956)
12GIOJNSdata$546,128.00datadatadatadata$2,999.58$0.00dataGIOJNS -- Current Value: $546,128, Short: $3,000, Long: $0
13OAGIdata$54,773.00datadatadatadata$0.00$0.00dataOAGI -- Current Value: $54,773, Short: $0, Long: $0
14OREUTYdata$354.00datadatadatadata$39.70$0.00dataOREUTY -- Current Value: $354, Short: $40, Long: $0
15ALAKOdata$654,642.58datadatadatadata$0.00$97.65dataALAKO -- Current Value: $654,643, Short: $0, Long: $98
16LAFKNdata$54.00datadatadatadata($96.68)$2,060.80dataLAFKN -- Current Value: $54, Short: ($97), Long: $2,061
17OIUBdata$3.00datadatadatadata$0.00$22.95dataOIUB -- Current Value: $3, Short: $0, Long: $23
18EWOAdata$75.00datadatadatadata$16.00$15.06dataEWOA -- Current Value: $75, Short: $16, Long: $15
19OOdata$67,586.56datadatadatadata($462.27)$244.66dataOO -- Current Value: $67,587, Short: ($462), Long: $245
20WEOTUIdata$45.00datadatadatadata$0.00($250.45)dataWEOTUI -- Current Value: $45, Short: $0, Long: ($250)
21AFSOLIJdata$5,436.11datadatadatadata($1.86)$173.60dataAFSOLIJ -- Current Value: $5,436, Short: ($2), Long: $174
22VGOIdata$3,746,344.00datadatadatadata$0.00$163.12dataVGOI -- Current Value: $3,746,344, Short: $0, Long: $163
23IWLDdata$34.00datadatadatadata($41.23)$2.00dataIWLD -- Current Value: $34, Short: ($41), Long: $2
24WOWOdata$1,654.55datadatadatadata$178.58($320.54)dataWOWO -- Current Value: $1,655, Short: $179, Long: ($321)
25AOFKJdata$43.23datadatadatadata$0.00$201.75dataAOFKJ -- Current Value: $43, Short: $0, Long: $202
26AFOJdata$43,345.43datadatadatadata$0.00$103.95dataAFOJ -- Current Value: $43,345, Short: $0, Long: $104
27GIOJNSdata$543.00datadatadatadata$139.08$247.27dataGIOJNS -- Current Value: $543, Short: $139, Long: $247
28OAGIdata$354.00datadatadatadata$1,296.11$2,037.20dataOAGI -- Current Value: $354, Short: $1,296, Long: $2,037
29OIUBdata$434,345.34datadatadatadata$0.00$103.99dataOIUB -- Current Value: $434,345, Short: $0, Long: $104
30EWOAdata$65,882.96datadatadatadata$60.87$0.00dataEWOA -- Current Value: $65,883, Short: $61, Long: $0
31OOdata$4,354.00datadatadatadata$0.00$747.97dataOO -- Current Value: $4,354, Short: $0, Long: $748
Sheet2
Cell Formulas
RangeFormula
M2:M31M2=SUM(FILTER(Table1[Unrealized Gain Loss],(Table1[Account '#]=[@[Account Number]])*(Table1[Symbol]=[@Symbol])*(Table1[Short or Long Term]="Short"),0))
N2:N31N2=SUM(FILTER(Table1[Unrealized Gain Loss],(Table1[Account '#]=[@[Account Number]])*(Table1[Symbol]=[@Symbol])*(Table1[Short or Long Term]="long"),0))
P2:P31P2=[@Symbol]&" -- Current Value: "&TEXT([@Value],"$#,##0;($#,##0)")&", Short: "&TEXT([@[Unrealized Short G/L]],"$#,##0;($#,##0)")&", Long: "&TEXT([@[Unrealized Long G/L]],"$#,##0;($#,##0)")
 
Upvote 0
For M2 on sheet 2, you could use
Excel Formula:
=SUM(FILTER(Table1[Unrealized Gain Loss],(Table1[Account '#]=[@[Account Number]])*(Table1[Symbol]=[@Symbol])*(Table1[Short or Long Term]="Short"),0))

You're the MVP. It wasn't working at first, so I threw in a TRIM() around all the account number and symbol references, and it fixed it. I assume there were hidden spaces / characters or the columns were formatted differently or something like that. This solution is MUCH more lightweight than my method. Thank you!!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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