0

Account flows

In the Accounts table, I have a view called Flows with the following script. It gives the correct dataset but would like to filter out from that dataset the selected account ledger line items (that is, xAccountId).

in other words, using another analogy, give me all products that a customer also purchased when they purchased Product X but do not list Product X.

"PURPOSE: SELECT ALL TRANSACTION LINE ITEMS WHERE JOURNAL ENTRY NUMBER EQUALS ALL JOURNAL ENTRY NUMBERS IN ACCOUNT";
let xAccountId := number(Id);
let xLedgerId := text(unique(concat((select Ledger)[Accounts.Id = xAccountId].Journal.Id)));
(select Ledger)[contains(xLedgerId, text(Journal.Id))]

52 replies

null
    • Fred
    • 4 mths ago
    • Reported - view

    Take a look at this resonse. Some very helpful tips on how to manipulate arrays.

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       Way over my head. Thanks anyway. 

    • Fred
    • 4 mths ago
    • Reported - view

    We can help build the code.

    Question: This line:

    let xLedgerId := text(unique(concat((select Ledger)[Accounts.Id = xAccountId].Journal.Id)));
    

    You want to find all related Journal records to a particular Account record. They are related through the Ledger table. You can try writing the code:

    let xLedgerId := text(concat(unique(Ledger.Journal.Id)));
    

    This way uses the power of relationships and stays away from unnecessary select statements.

    Now I don't understand what you want to do here:

    (select Ledger)[contains(xLedgerId, text(Journal.Id))]
    

    Aren't you just finding the same records that you have in xLedgerId?

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       Thank you very much indeed for your time. The script above gives the desired dataset except I’d like to reduce it further.

      To use another analogy, give me all products the customer also purchased when they purchased product X, but don’t list product X. The script above does that but also includes product X. I would like to filter out product X.

      Back to the accounting database above: Give me all transaction ledger line items that the journal entries affecting the selected account on the Accounts table posted to, but do not list the line items posted directly to the selected account. 

      For example, a journal entry is made to increase the bank account $200 and increase the revenue account for the same amount. When one selects the bank account in the Accounts table, let’s say, the Flows view tab should list the transaction ledger line item, that is, the increase to the revenue account instead of the increase to the bank account (the latter is listed in the Ledger view tab).

    • Fred
    • 4 mths ago
    • Reported - view

    Ok, now spending a few more moments looking at things, I think I understand this:

    let xAccountId := number(Id);
    let xLedgerId := text(unique(concat((select Ledger)[Accounts.Id = xAccountId].Journal.Id)));
    (select Ledger)[contains(xLedgerId, text(Journal.Id))]
    

    So line 2 finds all related Journal records to the current Account record.

    Then in line 3 you want to find all Ledger records that have the same related Journal records but minus the Journal records found in line 2.

    You can try, I'm thinking off the top of my head so I can't say for sure will work:

    let xAccLedger := Ledger;
    let xAccJournal := xAccLedger.Journal;
    let journalrelatedLedger := (select Ledger [var j := Journal; xAccJournal[=j]];
    journalrelatedLedger[var t := this; not xAccLedger[=t]]
    

    I like to work with base records so I've changed the code to remove reference to Id.

    Line 1 gets all related Ledger records to the current record. This is returned as an array of rid (record Ids).

    Line 2 gets all related Journal records. This is returned as an array of rid (record Ids).

    Line 3 finds all Ledger records that have the same Journal records. This will find records that would match line 1.

    Line 4 is where we take the second array (of Ledger records) and subtract the first array (of related Ledger records to the current record) and we are left with records that have the same related Journal records to the current record but subtracts Ledger records related to the current record in Accounts.

    I hope it works. If it doesn't can you upload a sample DB so I can test things out?

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view
      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

      Not working yet. Attached database above

    • Alain_Fontaine
    • 4 mths ago
    • Reported - view

    What about:

    let myact := this;
    Ledger.Journal.Ledger[Accounts != myact]
    
      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

      Thank you very much indeed! 

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       What would that script look like if instead of the Accounts table Flows view it’s the Financials table Flows view? That is, give me ledger line items related to the selected Financials record where related Financials record is not the selected Financials record? In other words, it’s the same as your script except it’s one relationship hop further away?

      • Alain_Fontaine
      • 4 mths ago
      • Reported - view

       Would this give the desired result?

      let myfin := this;
      Accounts.Ledger.Journal.Ledger[Accounts.Financials != myfin]
      
      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       amazing! Finally, if it was in the Contacts table Flows view would it be:

      let myact := this;
      Journal.Ledger[Journal.Contacts != myact]

      It’s not working.

      • Alain_Fontaine
      • 4 mths ago
      • Reported - view

       "Journal.Ledger" returns all the records in table "Ledger" that refers to a record in the table "Journal", which in turn refers to the current record in table "Contacts". If you add a condition that excludes the records in table "Ledger" that refers to a record in the table "Journal", which in turn refers to the current record in table "Contacts", you will of course always get an empty set. I don’t understand which set of records you want to see.

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       Of course! I removed the flows view of the contacts table since the ledger view already gives all transaction line items for the selected contact. Thank you very much indeed! 

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       I have tested the above script and it’s not working always as intended. Images below show that the account balance in Ledger view is eventually zero, but was expecting a zero balance also in the Flows view but 160.18 remains. The cause is the related line items in journal entry 81238 in Journal view is not flowing properly in the Flows view

      • Alain_Fontaine
      • 4 mths ago
      • Reported - view

       So the actual data are such that the same ledger can sometimes be viewed more than once. We need to forbid this:

      let myact := this;
      unique(Ledger.Journal.Ledger[Accounts != myact])
      
      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       It works! Thank you very much indeed! 

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       do I have the script correct when running the flows view in the Financials table? The Prepaid Expenses, for example, has different totals between its Ledger and Flows views, when the total Ledger view of 1,297.32 should also be in Flows, not 648.66

      • Alain_Fontaine
      • 4 mths ago
      • Reported - view

       Your dabase is (moderately) complex, and seems to contain loads of data. So it is difficult to understand the cause of a subtle problem with just a few screen captures. Can you produce a nearly empty copy of your database, with just the minimum amount of (fake) data to activate the issue?

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       No need. It turns out I misspoke. Thank you very much indeed! I figured out what happened. Your script is working. The ledger view was incorrect - pulling in records twice! The script in the Financials Ledger view was “Accounts.Financials.Accounts.Ledger” instead of “Accounts.Ledger”

    • Fred
    • 4 mths ago
    • Reported - view
     said:
    Not working yet.

     Can you write out an example using the records in your DB?

    When I use Alain's example in your view in the Flow tab of the Groceries record (Account Id = 731)  it seems to work for me.

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

      - please return to me the database file previously provided? I’ll take a look. I cannot get the revised script to work on my own.

      • Fred
      • 4 mths ago
      • Reported - view

      I've updated the flow tab in Accounts.

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

      Thank you very much indeed! All works now.

      • Erik_Ivan_Bech
      • 4 mths ago
      • Reported - view

       what would the script be to add a running balance field in the Accounts table Ledger view tab, let’s say, so that it takes the accumulated sum of the DrCr field when ledger line items are sorted by date and then by ledger Id given that new journal entries may be back-dated to a prior period?

Content aside

  • Status Answered
  • 4 mths agoLast active
  • 52Replies
  • 324Views
  • 4 Following