Forum Discussion

CurtisMiddleton's avatar
CurtisMiddleton
Qrew Trainee
2 years ago

JINJA If Statement Help

Hi,

I'm building a pipeline and am struggling with an if statement.
What I want it to do is look at the prior month and round up to the nearest quarter for that prior month. Ex: Today would round to 202303 whereas July 15, 2023 would round to 202306.

Here is what I have so far. I've troubleshooted my set variables and those function as expected. It's when I get to the if statement where it isn't happy:

{% set Reporting_MM = "{:%m}".format(time.now - time.delta(months=1)) %}
{% set Reporting_YYYY = "{:%Y}".format(time.now - time.delta(months=1)) %}
{% set MARCH = Reporting_YYYY ~ '03' %}
{% set JUNE = Reporting_YYYY ~ '06' %}
{% set SEPTEMBER = Reporting_YYYY ~ '09' %}
{% set DECEMBER= Reporting_YYYY ~ '12' %}

{% if Reporting_MM in [ '01', '02', '03'] %}

{{ MARCH }}

{% elif Reporting_MM in [ '04', '05', '06'] %}

{{ JUNE }}

{% elif Reporting_MM in [ '07', '08', '09'] %}

{{ SEPTEMBER }}

{% elif Reporting_MM in [ '10', '11', '12'] %}

{{ DECEMBER }}

{% else %}

{{ ERROR }}

{% endif %}

------------------------------
Curtis Middleton
------------------------------

3 Replies

  • DougHenning1's avatar
    DougHenning1
    Community Manager
    Hi Curtis,

    I think it's easier to compute the quarter, then the month is an easy multiplication. Instead of the if block, this should work better:

    {% set reportDate = time.today - time.delta(months=1) -%}
    {% set year = reportDate.strftime('%Y') -%}
    {% set qtr = (reportDate.month/3) | round(method='ceil') | int %}
    {% set month = "{:02d}".format(qtr * 3) %}
    {{ year ~ month }}​


    Hope that helps!



    ------------------------------
    Doug Henning
    ------------------------------
  • Nevermind... it seems the pipeline just took a ton of time to actually read the formula and tell me it was ok to run. Probably 5+ minutes.

    ------------------------------
    Curtis Middleton
    ------------------------------
  • Thanks for posting this,

    I'm all things Jinja & pipelines but is there a reason you can't achieve this with a simple formula and let pipeline simply copy the "quarter name" . It should be wicked fast as well

    ------------------------------
    Prashant Maheshwari
    ------------------------------