DAX - sort order transactions by logic

ejheflin

New Member
Joined
May 22, 2015
Messages
12
Hey guys. I have a challenge that has me stuck. I have a data dump of bank transactions. The simplified data looks like this:

OpeningLedgerBalDebitCreditEndRunningLedger
250
10320
25010340
25010300
250100350
25010330
25010310
25010290

<tbody>
</tbody>

My goal is to create a helper column that orders the transactions like this:
OpeningLedgerBalDebitCreditEndRunningLedgerOrder#
250103204
250103402
250103006
2501003501
250103303
250103105
250102907

<tbody>
</tbody>

My ultimate goal is to get a working DAX formula but a regular formula might help me get there. I cannot think of a solution that doesn't throw a circular reference error. My first attempt was this:
=IF([OpeningLedgerBal]-[Debit Amt]+[Credit Amt]=[RunningLedgerBal],"1",LOOKUPVALUE([Order#],[RunningLedgerBal],[CalcBegLedgerBal])+1)

Where [CalcBegLedgerBal] was a helper column. Any ideas? I would appreciate any input at all!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Dear Ejheflin,

Is it always in the context where first a credit appears (only one?), followed by a bunch of debit transactions, while it all has the same openingledger?

Best regards,
 
Upvote 0
Dear Ejheflin,

Is it always in the context where first a credit appears (only one?), followed by a bunch of debit transactions, while it all has the same openingledger?

Best regards,
Unfortunately no. The detail can contain unlimited debits/credits in any order. The OpeningLedgerBal is all the same because it is the balance at the beginning of the month.
 
Upvote 0
Hi again,

I couldn't come up with a formula. However, I liked the challenge your question had (as I still have plenty to learn), so I turned to VBA.

For this to work (I assume your data is currently in PowerPivot), you must export your data out of PowerPivot to an Excel worksheet.

The worksheet should be built as follows:
-Column A containing the Openingledgerbal.
-Column B containing the Debit.
-Column C containing the Credit.
-Column D containing the Endrunningledger.
-Column E and F must be empty (except for the first row).
-The first Row of column a,b,c,d,e,f must be filled (with however you want to name the column).

Furthermore, the value in endrunningledgar must be exactly the value of the previous endrunningledgar + Credit or - Debit of the current transaction (even a penny in difference will cause this code to fail).
E.g. Previous endrunningledgar was 321.21, credit on current transaction is 10.00, current endrunningledgar must be exactly 331.21.

Lastly, only one of the following statements should be true and may only occur once:
previous endrunningledgar + credit current transaction = current endrunningledgar.
previous endrunningledgar - debit current transaction = current endrunningledgar.

If these statements are correct twice in the given list, the code will only take notice of the first occurrence.
E.g.:
Previous endrunningledgar = 350
Transaction 1 Debit 10
Transaction 2 Credit 10
Transaction 1 endrunningledgar = 340
Transaction 2 endrunningledgar = 360

So both statements are correct, the code misses the second transaction which might have occurred before the second transaction.

Code:
Sub test()    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual


    Dim Rank As Integer
    Dim lstrw As Integer
        
    lstrw = Range("A" & Rows.Count).End(xlUp).Row
    Rank = 1
    
    Range("F2:F" & lstrw).Value = Range("A2:A" & lstrw).Value
    
    For x = 2 To lstrw


    If Cells(x, 5).Value = "" And Cells(x, 3).Value > 0 And Cells(x, 6).Value + Cells(x, 3).Value = Cells(x, 4).Value Or Cells(x, 2).Value > 0 And Cells(x, 6).Value - Cells(x, 2).Value = Cells(x, 4).Value Then
    
        Cells(x, 5).Value = Rank
        Range("F2:F" & lstrw).Value = Cells(x, 4).Value
        
        Rank = Rank + 1
        
        x = 1
        
    End If
    
    Next x
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
        
End Sub

Hopefully, this gets you on the right track.

Best Regards,
Mart van Gestel
 
Upvote 0

Forum statistics

Threads
1,215,509
Messages
6,125,216
Members
449,215
Latest member
texmansru47

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