Using months in a calculation

Hi,

I’m trying work out if a month within a financial period is earlier than the current period, but seem to be struggling with the use of the < and > signs in my equations.

For example, to create a simple illustration, I can recognise when a financial period month is equal to the current period month with the following code:

{% stripnewlines %}
|-10%-|-10%-|-10%-|-70%-+
{% newline %}
|**{% t "Month end" %}**
|**{% t "Period end" %}**
|**{% t "Test" %}**
|{% comment %}spacer{% endcomment %}

{% for month in period.month_end_dates %}
{% newline %}
 {% assign month_end = month | date:'%B %Y' %}
 {% assign period_end = period.end_date | date: '%B %Y' %}
|{{month_end}}
|{{period_end}}
|{% if month_end == period_end %}
    {% assign check_ind = 0 %}
    {% unexplained check_ind as:indicator%}
 {% else %}
 {% endif %}
|{% comment %}spacer{% endcomment %}

{% endfor %}
{% endstripnewlines %}

This will provide a table similar to the following, which will show what the current period is:
image

However, if I want to also identify all earlier periods (and therefore simply change my month_end == period_end to month_end <= period_end), I get the following result:
image

Is there something I am missing / am I being naive with my calculation?! Any help would be greatly appreciated.

Many thanks
Joe

Edit: Formatting of code

HI @jhanley,

Above your calculation in the code you assigned month_end and period_end with formatting. As a result the calculation of your if-statement will take this formatting into account.

You can easily fix this by moving the formatting in your code to where the month_end and period_end are printed, like this:

{% stripnewlines %}
|-10%-|-10%-|-10%-|-70%-+
{% newline %}
|{% t “Month end” %}
|{% t “Period end” %}
|{% t “Test” %}
|{% comment %}spacer{% endcomment %}

{% for month in period.month_end_dates %}
{% newline %}
{% assign month_end = month %}
{% assign period_end = period.end_date %}
|{{month_end | date:’%B %Y’ }}
|{{period_end | date: ‘%B %Y’}}
|{% if month_end <= period_end %}
{% assign check_ind = 0 %}
{% unexplained check_ind as:indicator%}
{% else %}
{% endif %}
|{% comment %}spacer{% endcomment %}

{% endfor %}
{% endstripnewlines %}

Hope this answers your question!

Kind regards,
Kimberly

1 Like

Thanks @Kimberly_Vlietinck, that worked exactly as I was hoping it would! It’s simple once you know how!! :bulb:

Kind regards
Joe

1 Like