CASE: group details of current and previous period / year

Let’s say we want to build further on this case:

but we’ll expand our overview of details on details of the current year AND the previous year (if it exists):

How you can access details from a previous period, is explained here:

Our complete code of our case:

{% assign current_year = period.year_end_date | date:"%Y" %}
{% assign prev_year = period.minus_1y.year_end_date | date:"%Y" %}
{% assign categories = "" %}

{% comment %}create var to check if prev_period exists{% endcomment %}
{% if period.minus_1y.exists %}
  {% assign prev_period = true %}
{% endif %}

{% comment %}create dynamic variables for the current year{% endcomment %}
{% for cat in custom.categories %}
  {% assign cat_name = cat.name %}
  {% capture cat_total %}total_{{cat_name}}_{{ current_year }}{% endcapture %}
  {% assign [cat_total] = [cat_total]+cat.value %}
  {% assign categories = categories | append:cat_name | append:";" %}
{% endfor %}

{% comment %}create dynamic variables for previous year IF it exists!{% endcomment %}
{% if prev_period %}
  {% for cat in period.minus_1y.reconciliations.recon_test.custom.categories %}
    {% assign cat_name = cat.name %}
    {% capture cat_total %}total_{{cat_name}}_{{ prev_year }}{% endcapture %}
    {% assign [cat_total] = [cat_total]+cat.value %}
    {% assign categories = categories | append:cat_name | append:";" %}
  {% endfor %}
{% endif %}

{% assign categories = categories | split:";" | uniq %}

<!-------- actual template ----------->

{% stripnewlines %}
| Categorie 
| {{ current_year }} {% if prev_period %}
| {{ prev_year }} {% endif %}
{% newline %}
|----
|---- {% if prev_period %}
|---- {% endif %}
# 
{% newline %}
{% for cat in categories %}
  {% capture cat_total_current %}total_{{cat}}_{{ current_year }}{% endcapture %}
  {% if prev_period %}
  {% capture cat_total_prev %}total_{{cat}}_{{ prev_year }}{% endcapture %}
  {% endif %}
| {{ cat }} 
| {{ [cat_total_current] | currency }} {% if prev_period %}
| {{ [cat_total_prev] | currency }}{% endif %}
{% newline %}
{% endfor %}
{% endstripnewlines %}

<br />

{% stripnewlines %}
| Categorie 
| Value 
{% newline %}
|----
|----# 
{% newline %}
{% fori cat in custom.categories %}
  | {% input cat.name %} 
  | {% input cat.value as:currency %} 
{% newline %}
{% endfori %}
{% endstripnewlines %} 
{% comment %}create var to check if prev_period exists{% endcomment %}
{% if period.minus_1y.exists %}
  {% assign prev_period = true %}
{% endif %}

This is to create a variable to display info IF a previous year exists (there’s no need to display a previous year if there is no data in that year - if the period does not exist)

{% comment %}create dynamic variables for the current year{% endcomment %}
{% for cat in custom.categories %}
{% assign cat_name = cat.name %}
{% capture cat_total %}total_{{cat_name}}_{{ current_year }}{% endcapture %}
{% assign [cat_total] = [cat_total]+cat.value %}
{% assign categories = categories | append:cat_name | append:“;” %}
{% endfor %}

{% comment %}create dynamic variables for previous year IF it exists!{% endcomment %}
{% if prev_period %}
{% for cat in period.minus_1y.reconciliations.recon_test.custom.categories %}
{% assign cat_name = cat.name %}
{% capture cat_total %}total_{{cat_name}}_{{ prev_year }}{% endcapture %}
{% assign [cat_total] = [cat_total]+cat.value %}
{% assign categories = categories | append:cat_name | append:“;” %}
{% endfor %}
{% endif %}

{% assign categories = categories | split:“;” | uniq %}

This is creating dynamic variables both for the current and the previous (if it exists) year.
So, we create dynamic variables where the name of the current year is saved and one where the previous year is saved (so 2 different dynamic variables for each categorie!)

{% stripnewlines %}
| Categorie
| {{ current_year }} {% if prev_period %}
| {{ prev_year }} {% endif %}
{% newline %}
|----
|---- {% if prev_period %}
|---- {% endif %}

{% newline %}
{% for cat in categories %}
{% capture cat_total_current %}total_{{cat}}{{ current_year }}{% endcapture %}
{% if prev_period %}
{% capture cat_total_prev %}total
{{cat}}_{{ prev_year }}{% endcapture %}
{% endif %}
| {{ cat }}
| {{ [cat_total_current] | currency }} {% if prev_period %}
| {{ [cat_total_prev] | currency }}{% endif %}
{% newline %}
{% endfor %}
{% endstripnewlines %}

This is the actual overview where all categories are grouped for the current year and the previous year.

{% stripnewlines %}
| Categorie
| Value
{% newline %}
|----
|----#
{% newline %}
{% fori cat in custom.categories %}
| {% input cat.name %}
| {% input cat.value as:currency %}
{% newline %}
{% endfori %}
{% endstripnewlines %}

This is where the actual input of data happens (manually, or through automatic reconciliation with Excel for instance).

All credits to @Sam for his input on this :ok_hand: