Finding biggest observation conditioned on several factors

homie

Board Regular
Joined
Nov 30, 2014
Messages
66
Hi,

I need to make a formula to return the largest number of Deposits of NOK for any given day in the previous 30 days. The deposits are timestamped by the minute. So I need to add the number of deposits per day for each day of the previous 30 days and then find the largest number of deposits for any of those days.

This formula/solution needs to be in one cell. I will copy it downward and the "present day" will change, so the previous 30 days will change as I copy it downward. The table below is where I get the information, but I will present the relevant data in another sheet entirely.

How do I go about solving this?

2nd related problem: I also need to find the largest Amount Deposited of NOK for any given day in the previous 30 days.

The dataset where I will be looking, looks like this:

Timestamp recognized by ExcelTimestamp recognized by Excel, roundedRemove USD,EUR,NOK or XBT, ETH, LTC from stringAmount American number as textCurrencyAmount European numberAction
16.09.2015 00:0916.09.2015 00:099/16/2015 0:09,100100NOK100Deposit
16.09.2015 00:1516.09.2015 00:159/16/2015 0:15,100100USD100Deposit
16.09.2015 05:5416.09.2015 05:549/16/2015 5:54,0.300150.30015XBT0,30015Withdraw
16.09.2015 14:3416.09.2015 14:349/16/2015 14:34,100100NOK100Deposit
16.09.2015 17:5116.09.2015 17:519/16/2015 17:51,48004800NOK4800Deposit
16.09.2015 18:1916.09.2015 18:199/16/2015 18:19,10001000NOK1000Deposit
16.09.2015 20:1916.09.2015 20:199/16/2015 20:19,0.26990.2699XBT0,2699Withdraw
16.09.2015 21:0416.09.2015 21:049/16/2015 21:04,0.7560.756XBT0,756Withdraw
17.09.2015 05:3017.09.2015 05:309/17/2015 5:30,20152015NOK2015Deposit
17.09.2015 06:5017.09.2015 06:509/17/2015 6:50,200200NOK200Deposit
17.09.2015 07:4017.09.2015 07:409/17/2015 7:40,900900NOK900Deposit
17.09.2015 08:4917.09.2015 08:499/17/2015 8:49,0.520.52XBT0,52Withdraw
17.09.2015 08:5317.09.2015 08:539/17/2015 8:53,0.375017920.37501792XBT0,37501792Withdraw
17.09.2015 11:0717.09.2015 11:079/17/2015 11:07,0.027079970.02707997XBT0,02707997Withdraw
17.09.2015 11:2617.09.2015 11:269/17/2015 11:26,0.801502390.80150239XBT0,80150239Withdraw
17.09.2015 11:2817.09.2015 11:289/17/2015 11:28,0.07240.0724XBT0,0724Withdraw
17.09.2015 11:2817.09.2015 11:289/17/2015 11:28,0.50.5LTC0,5Withdraw
17.09.2015 12:0817.09.2015 12:089/17/2015 12:08,0.10510.1051XBT0,1051Withdraw
17.09.2015 12:2717.09.2015 12:279/17/2015 12:27,0.40.4XBT0,4Withdraw
17.09.2015 16:3017.09.2015 16:309/17/2015 16:30,25002500NOK2500Deposit
17.09.2015 16:3317.09.2015 16:339/17/2015 16:33,0.0250.025XBT0,025Withdraw
17.09.2015 17:4017.09.2015 17:409/17/2015 17:40,1.51.5XBT1,5Withdraw
17.09.2015 17:5517.09.2015 17:559/17/2015 17:55,0.0250.025XBT0,025Withdraw
17.09.2015 17:5717.09.2015 17:579/17/2015 17:57,500500NOK500Deposit

<tbody>
</tbody>
 
Last edited:
Your new data sample has only withdrawals for XBT (BTC), no deposits. Is this correct?

M.
Sorry for the late reply, very busy weekend.

Yeah, that is correct. For some reason, our data guy could only extract fiat currency (NOK) deposits (not withdrawels) and only XBT withdrawels (no deposits).
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Ok.
Assuming data sample in Sheet1 columns A:F, headers in row 1, data beginning in row 2
Dates (column A) in ascending order

Sheet2
Criteria --> columns A:D (light blue)
Just for testing purposes i included a row with Withdraw as criteria for XBT

Results --> columns E:L (gray area)

A
B
C
D
E
F
G
H
I
J
K
L
1
Ref Date​
Days Before​
Currency​
Action​
LDA​
LDA Day​
LAA​
LAA Day​
SDA​
SDA Day​
SAA​
SAA Day​
2
19/09/2015​
30​
NOK​
Deposit​
6​
17/09/2015​
28600​
18/09/2015​
1​
16/09/2015​
100​
16/09/2015​
3
19/09/2015​
30​
XBT​
Deposit​
0​
0​
0​
0​
4
19/09/2015​
30​
XBT​
Withdraw​
10​
18/09/2015​
3,65279​
18/09/2015​
1​
16/09/2015​
0,30015​
16/09/2015​

<tbody>
</tbody>


To get these results run the macro Main

Code:
Sub Main()
    Dim lastRow1 As Long, lastRow2 As Long, lDataFirst As Long, lDataLast As Long
    Dim shData As String, rData As Range, i As Long
    
    shData = "Sheet1" '<--adjust the sheet name
    With Sheets(shData)
        lastRow1 = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set rData = .Range("A2:F" & lastRow1)
    End With
        
    With Sheets("Sheet2")
        lastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastRow2
            lDataFirst = GetFirst(rData.Columns(1), .Range("A" & i) - .Range("B" & i))
            lDataLast = GetLast(rData.Columns(1), .Range("A" & i))
            If lDataFirst > lDataLast Then
                .Range("E" & i).Resize(, 8) = Array(0, "", 0, "", 0, "", 0, "")
            Else
                .Range("E" & i).Resize(, 8) = GetResults(rData, lDataFirst, lDataLast, _
                                .Range("C" & i).Value, .Range("D" & i).Value)
            End If
        Next i
    End With
End Sub

Function GetFirst(r As Range, lDay As Date)
    Dim rFound As Range, lFirst As Variant
    
    Set rFound = r.Find(lDay, After:=r.Cells(r.Rows.Count), _
        Lookat:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext)
    
    If rFound Is Nothing Then
        lFirst = Application.Match(CLng(lDay), r)
        If IsError(lFirst) Then
            GetFirst = 1
        Else
            GetFirst = lFirst + 1
        End If
    Else
        GetFirst = rFound.Row - r.Row + 1
    End If
End Function

Function GetLast(r As Range, lDay As Date)
    Dim rFound As Range, lLast As Variant
    
    Set rFound = r.Find(lDay, After:=r.Cells(1), _
        Lookat:=xlWhole, LookIn:=xlValues, SearchDirection:=xlPrevious)
    
    If rFound Is Nothing Then
        lLast = Application.Match(CLng(lDay), r)
        If IsError(lLast) Then
            GetLast = 0
        Else
            GetLast = lLast
        End If
    Else
        GetLast = rFound.Row - r.Row + 1
    End If
End Function

Function GetResults(r As Range, lFirst As Long, lLast As Long, Crit1 As String, Crit2 As String)
    Dim vData As Variant, dic As Object, vKey As Variant, i As Long
    'LDA=LargestNumDepositsAny, LDA=LargestAmountAny
    Dim lLDA As Long, dbLAA As Double
    Dim DayLAA As Variant, DayLDA As Variant
    'SDA=SmallestNumDepositsAny, SAA=SmallestAmountAny
    Dim lSDA As Long, dbSAA As Double
    Dim DaySDA As Variant, DaySAA As Variant
    
    Set dic = CreateObject("Scripting.Dictionary")
    
    vData = Range(r.Cells(lFirst, 1), r.Cells(lLast, 6))
    For i = LBound(vData, 1) To UBound(vData, 1)
        If UCase(vData(i, 4)) = UCase(Crit1) And UCase(vData(i, 6)) = UCase(Crit2) Then
            If dic.exists(vData(i, 1)) Then
                dic(vData(i, 1)) = Array(dic(vData(i, 1))(0) + 1, dic(vData(i, 1))(1) + vData(i, 5))
            Else
                dic(vData(i, 1)) = Array(1, vData(i, 5))
            End If
        End If
    Next i
    
    If dic.Count Then
        'Initialize lSDA and dbSAA with Max possible values
        lSDA = UBound(vData, 1)
        dbSAA = Application.Sum(r.Columns(5))
        
        For Each vKey In dic.keys
            If dic(vKey)(0) > lLDA Then
                lLDA = dic(vKey)(0)
                DayLDA = vKey
            End If
            If dic(vKey)(1) > dbLAA Then
                dbLAA = dic(vKey)(1)
                DayLAA = vKey
            End If
            If dic(vKey)(0) < lSDA Then
                lSDA = dic(vKey)(0)
                DaySDA = vKey
            End If
            If dic(vKey)(1) < dbSAA Then
                dbSAA = dic(vKey)(1)
                DaySAA = vKey
            End If
        Next vKey
    End If
    
    GetResults = Array(lLDA, DayLDA, dbLAA, DayLAA, lSDA, DaySDA, dbSAA, DaySAA)
End Function

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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