Grouping items in a loop based on similar characters

I have some code that calls all the journals affecting profit and loss that are not zero, showing the description and amount and works well.

However would it be possible to group together all the journals that have the same description ? Including one description and then the sum of all the appropriate journal amounts ?

{% stripnewlines %}
||||£

{% newline %}

|---60%-----|---10%----|:---15%----:|:---15%-----:#

{% newline %}

{% for adj in period.adjustments %}
   {% for tr in adj.transactions %}
     {% capture adj_account %}{{ tr.account.mapped_number }}{% endcapture %} 
        {% if adj_account >='400001.000' and adj_account < '930000.000' or adj_account > '930200.000' %}
        {% $1+ tr.value %}
        
        {% endif %}
   {% endfor %}
   {% if $1 != 0 %}
   
      {% if dummy == 0 %}
        | _Increases in profit_
        |{% newline %}
        {% assign dummy = 1 %}
      {% endif %}
      

      {% for tr in adj.transactions %}
      {% $4+ 1 %}
      {% capture adj_id %}{{ $4 }}{% endcapture %}
      {% capture adj_account %}{{ tr.account.mapped_number }}{% endcapture %} 
        {% if tr.value < 0 %}
        {% if adj_account >='400001.000' and adj_account < '930000.000' or adj_account > '930200.000' %}
          |  {% input custom.[adj_id].narrative default:tr.description  %} 

          || {{ tr.value*-1 | currency}}
          {% $2- tr.value | currency %}      
          {% newline %}    
        {% endif %}
        {% endif %}
      {% endfor %}
    {% endif %} 
   {% $1- $1%}
   


{% endfor %}
{% if dummy == 1 %}
{% assign  total_increases = $2-profit %}
        |||| {{ total_increases | currency }}
        {% newline %}
        {% assign dummy = 0 %}
{% endif %}

{% endstripnewlines %}

Hi @shellldon,

Thank you for reaching out.

If I understand your query correctly you are trying to get the total sum for all journal entries that have a matching description.

There are a number of ways to achieve this and I have demonstrated one implementation by adding some additional logic within your ‘for loop’.

Essentially, we are assigning ‘key value pairs’ for the description text & values entered whilst accounting for human input errors and printing the final values.

{% for adj in period.adjustments %}
   {% for tr in adj.transactions %}
     {% capture adj_account %}{{ tr.account.mapped_number }}{% endcapture %} 
        {% if adj_account >='400001.000' and adj_account < '930000.000' or adj_account > '930200.000' %}
        
        {% comment %}Here we assign the descripion key. As the description text is a free-form field I formatted it to account for human input/errors. 
          E.g "test description  " , "testdescription" and "Test Description" will be treated equally  {% endcomment %}
        {% capture description_text %}{{ tr.description }}{% endcapture %}
        {% assign description_key = description_text | downcase | replace:" ","" | replace:",","" | replace:":","" | append:"Z" %}
        
        {% comment %}Adding total for transactions with the same description{% endcomment %}
        {% capture description_key_total %}{{ description_key }}_total{% endcapture %}
        {% assign [description_key_total] = [description_key_total]+tr.value %}
        
        {% comment %}Creating an array of different description{% endcomment %}
        {% comment %}Checking on key values, in case descriptions have exra space, different capital letters{% endcomment %}
        {% unless description_key_array contains description_key %}
          {% assign description_text_array = description_text_array | append:description_text | append:"|" %}
          {% assign description_key_array = description_key_array | append:description_key | append:"|" %}
        {% endunless %}
        
        {% endif %}
   {% endfor %}
{% endfor %}


{% comment %}Display of total values per description{% endcomment %}

{% assign description_text_array = description_text_array | split:"|" %}
{% assign description_key_array = description_key_array | split:"|" %}

{% for item in description_key_array %}

        {% comment %}Code to access total value{% endcomment %}
        {% capture description_key_total %}{{ description_key_array[forloop.index0] }}_total{% endcapture %}

        {{ description_text_array[forloop.index0] }} {{ [description_key_total] | currency:2 }}

{% endfor %}

I hope this helps but if you need further clarification you can refer to these Developer guide links >
adjustments
adjustment
transaction

Many thanks

Tom

Hi Tom,

Thank you for looking into this.

I was trying to get the value added to any previous iteration with the same description rather than have the results after the loop.

For example if there were two journals that matched my original code’s parameters and had identical descriptions, the second, instead of creating another line and putting the same description and value, it rather added to the first one’s value above.

Is this possible or would I have to create the array and then print afterwards ?

Sheldon

Hi @shellldon,

Thank you for coming back to me and for the extra detail.

I believe that using the “group_by” filter will allow you to achieve this as it allows you to group items that have the same variable value such as the journal description text.

Take a look at this use case post and developer guide for how to implement it in your code and let me know if you need any further assistance > group_by implementation / Collection guide

Many thanks

Tom