Streaks, maximum streaks for la

DRSteele

Well-known Member
Joined
Mar 31, 2015
Messages
2,639
Office Version
  1. 365
Platform
  1. Windows
Can someone please help me?

I have over 43000 rows of data in an ExcelTable. I need an efficient forumla that determines streaks of zeros & ones and uses Excel Table nomenclature.

In this sample, there are two pieces of data (i.e., wtn high and wtn low) and four corresponding columns (in yellow) that require formulas.

For the streak high and streak low columns, I need this: If the data point is a 0, I want to show a -1 for the streak, and if the subsequent data point is 0, show -2, etc. If the data point is 1,
I want to show a 1 for the streak, and if the subsequent data point is 1, show 2, etc.

Then for the sh end and sl end columns, I want to show the maximum value of the current streak.

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Book1
AKLNOPQ
7datewtn highwtn lowstreak highstreak lowsh endsl end
81/1/190000-1-100
91/2/190000-2-200
101/3/190000-3-300
111/4/190000-4-40-4
121/5/190001-51-50
131/6/1900111210
141/7/190001-13-10
151/8/1900111404
161/9/1900102-10-1
171/10/1900113100
181/11/1900114200
191/12/1900115300
201/13/1900116400
211/14/1900117500
221/15/1900118600
231/16/1900119700
241/17/19001110800
251/18/19001111900
261/19/190011121000
271/20/190011131100
281/21/190011141200
291/22/190011151300
301/23/190011161400
311/24/1900111715015
321/25/19001018-118-1
331/26/190001-1101
341/27/190000-2-1-20
351/28/1900101-20-2
Sheet1
<strike>
</strike>
[/FONT]
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
How about:

ABCDEFG
7datewtn highwtn lowstreak highstreak lowsh endsl end
81/1/190000-1-100
91/2/190000-2-200
101/3/190000-3-300
111/4/190000-4-40-4
121/5/190001-51-50
131/6/1900111210
141/7/190001-13-10
151/8/1900111404
161/9/1900102-10-1
171/10/1900113100
181/11/1900114200
191/12/1900115300
201/13/1900116400
211/14/1900117500
221/15/1900118600
231/16/1900119700
241/17/19001110800
251/18/19001111900
261/19/190011121000
271/20/190011131100
281/21/190011141200
291/22/190011151300
301/23/190011161400
311/24/1900111715015
321/25/19001018-118-1
331/26/190001-1101
341/27/190000-2-1-20
351/28/1900101-210

<tbody>
</tbody>
Sheet10

Worksheet Formulas
CellFormula
D8=IF([@[wtn high]]=OFFSET([@[wtn high]],-1,0),(ABS(N(OFFSET([@[streak high]],-1,0)))+1),1)*IF([@[wtn high]]=0,-1,1)
E8=IF([@[wtn low]]=OFFSET([@[wtn low]],-1,0),(ABS(N(OFFSET([@[streak low]],-1,0)))+1),1)*IF([@[wtn low]]=0,-1,1)
F8=IF([@[wtn high]]=OFFSET([@[wtn high]],1,0),0,[@[streak high]])
G8=IF([@[wtn low]]=OFFSET([@[wtn low]],1,0),0,[@[streak low]])

<tbody>
</tbody>

<tbody>
</tbody>



Note that the last row differs in the last 2 columns, since the formulas are based on looking at the next row, which doesn't exist. And sorry, this uses the volatile OFFSET, but I don't know of another way to use table nomenclature to get previous/next rows.


Edit: you can use INDEX instead. Here's the F8 formula with INDEX:

=IF([@[wtn high]]=INDEX([wtn high],ROW([@[wtn high]])-ROW(INDEX([wtn high],1))+2),0,[@[streak high]])
 
Last edited:
Upvote 0
Solution
Eric, forgive my late reply.

I can't thank you enough! The formula and daily query I had took over 21 minutes to run in the workbook with 43000+ rows, and Excel used the CPU constantly for the entire time, showing 'not responding' mode and occasionally crashing. Your formula took just 31 seconds.

Here is my old formula for 'streak high'. I think the expanding range was the culprit for bogging down everything. Function OFFSET seems immensely faster for some unexpected reason.

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]=IF([@[wtn high]],1,-1)*IF(COUNTIF($K$8:K8,$K$8)=COUNTA($K$8:K8),COUNTA($K$8:K8),MAX(ROW($K$8:K8))-MAX(($K$8:K8<>INDEX($K$8:K8,ROWS($K$8:K8)))*($K$8:K8<>"")*ROW($K$8:K8)))[/FONT]

I devised this formulas for 'sh end', which seems to work without it having to be altered for the final rows.

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]=MIN(ABS(IF(OFFSET([@[streak high]],1,)<>[@[streak high]]+1,[@[streak high]],0)),IF(OFFSET([@[streak high]],1,)<>[@[streak high]]-1,[@[streak high]],0))


[/FONT]
 
Upvote 0
You may be right about the expanding range, none of my formulas used more than 4 values total. Nice job updating the 'sh end' formulas to work for the final rows, I was going to tweak them after you tried them out, but I won't need to now! :)

I once read some comments from a programmer who worked at Microsoft about OFFSET. In short, he said that yes OFFSET is volatile, but it's very fast, so don't be afraid to use it. This may be a good example of that.

In any event, glad it works for you!
 
Upvote 0
Terrific! I like OFFSET because it seems intuitive.

What I am doing here is trying to identify maximum streaks for above/below average outcomes of various kinds. The sample data I choose to experiment on is what we have here, Calgary's weather. I made a query that gets our historical weather back to 1/1/1900, calculates the mean highs and lows for each of the 366 days, compares each record to those means and then puts the table into an ExeclTable. I realise that the means include ALL the data (both ante and post each date) but I don't care.

Then the formulas we have here determine the streaks and others extract dates of neat things. For example, ending March 10, 2019 Calgary had lows that were below average for 38 consecutive days - what a rotten February we just had! Someone bleated to me that global warming is actually global cooling cuz that's some kind of record. But our record for days below average for the lows is 49 days, set in 1950.

I really don't care about the weather, but 43000 rows is enough to devise formulas like these. I am really using this to identify streaks for financial derivatives action in trade-by-trade data.
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,266
Members
448,558
Latest member
aivin

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