# Subscription space template: Formula missing zero padding?

**URL:** <https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452>\
**Category:** Bugs & Issues\
**Created:** [November 27, 2023, 7:37am UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452 "2023-11-27T07:37:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [November 27, 2023, 7:37am UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/1 "2023-11-27T07:37:15Z")

</div>

Just tried installing the Subscription template yesterday. Didn’t do anything else with it. An automation was executed this morning with the following error.

It seems the month (and perhaps day?) needs som zero padding?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/f/fda592c2e354d06e52af9b778dc340b17ea20b88.png)

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 27, 2023, 12:51pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/2 "2023-11-27T12:51:42Z")

</div>

It’s actually not a zero-padding issue, but rather the automation is attempting to set the month to -1  
We’ll look into it and update it (and post the fix here)

---

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [November 27, 2023, 12:52pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/3 "2023-11-27T12:52:38Z")

</div>

Ah yes, I see it now that I looked more clearly 😃 Thanks!

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 27, 2023, 1:24pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/4 "2023-11-27T13:24:56Z")

</div>

You’ll need to update this automation rule:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/c/c30c274be90d3cd1024ba057f901da6a9171f11e.png)

The update action has an error in the formula.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/8/88f9d42d1e793cd8b7b22d8748eef975e1e4e034.png)

The correct formula is this:

```auto
Date(
  Year([Step 1 Subscription].[Next billing date]) +
    RoundDown(Month([Step 1 Subscription].[Next billing date]) / 12, 0),
  Month([Step 1 Subscription].[Next billing date]) +
    1 -
    12 *
      RoundDown(Month([Step 1 Subscription].[Next billing date]) / 12, 0),
  Least(
    Day(
      Date(
        Year([Step 1 Subscription].[Next billing date]) +
          RoundDown(
            Month([Step 1 Subscription].[Next billing date]) / 12,
            0
          ),
        Month([Step 1 Subscription].[Next billing date]) +
          2 -
          12 *
            RoundDown(
              (Month([Step 1 Subscription].[Next billing date]) + 1) /
                12,
              0
            ),
        1
      ) - Days(1)
    ),
    Day([Step 1 Subscription].[Next billing date])
  )
)

```

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 27, 2023, 1:25pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/5 "2023-11-27T13:25:32Z")

</div>

And you will probably want to run the formula, to take care of the previous failed automation:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/5/54e4fedc508a8a81df24a0a7a593df47e6fd0ba2.png)

---

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [November 27, 2023, 1:34pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/6 "2023-11-27T13:34:10Z")

</div>

Thanks, works correctly now!

---

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [November 27, 2023, 6:21pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/7 "2023-11-27T18:21:23Z")

</div>

Would it be possible to add an automation for Quarterly updates as well? We have a few subscriptions that are billed on a quarterly basis.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 27, 2023, 6:36pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/8 "2023-11-27T18:36:03Z")

</div>

Have a look at this thread:

> [@Add a number of months to a date](https://community.fibery.io/t/add-a-number-of-months-to-a-date/4695/5):
>
> Something like this should work Date( Year(CurrentDate) + RoundDown((Month(CurrentDate) + IntervalPeriod - 1) / 12, 0), Month(CurrentDate) + IntervalPeriod - 12 \* RoundDown((Month(CurrentDate) + IntervalPeriod - 1) / 12, 0), Least( Day( Date( Year(CurrentDate) + RoundDown((Month(CurrentDate) + IntervalPeriod - 1) / 12, 0), Month(CurrentDate) + IntervalPeriod + 1 - 12 \* RoundDown((Month(CurrentDate) + IntervalPer…

I hope you’ll be able to figure out from the current automations in the template, together with the code snippet in that topic how to do it.

---

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [November 27, 2023, 6:47pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/9 "2023-11-27T18:47:32Z")

</div>

I was already on my way to simplify the setup by introducing an Occurrence field (number of times, 1-12, in a year) and this suggestion thread was exactly what I needed. Thanks!

Final working formula:

```auto
Date(
  Year([Step 1 Subscription].[Next billing date]) +
    RoundDown(
      (Month([Step 1 Subscription].[Next billing date]) +
        [Step 1 Subscription].Occurrence -
        1) /
        12,
      0
    ),
  Month([Step 1 Subscription].[Next billing date]) +
    [Step 1 Subscription].Occurrence -
    12 *
      RoundDown(
        (Month([Step 1 Subscription].[Next billing date]) +
          [Step 1 Subscription].Occurrence -
          1) /
          12,
        0
      ),
  Least(
    Day(
      Date(
        Year([Step 1 Subscription].[Next billing date]) +
          RoundDown(
            (Month([Step 1 Subscription].[Next billing date]) +
              [Step 1 Subscription].Occurrence -
              1) /
              12,
            0
          ),
        Month([Step 1 Subscription].[Next billing date]) +
          [Step 1 Subscription].Occurrence +
          1 -
          12 *
            RoundDown(
              (Month([Step 1 Subscription].[Next billing date]) +
                [Step 1 Subscription].Occurrence) /
                12,
              0
            ),
        1
      ) - Days(1)
    ),
    Day([Step 1 Subscription].[Next billing date])
  )
)

```

I now have one automation instead of two which is perfect for my needs.

---

<div class="post-metadata">

**Author:** ![aoe](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/aoe/32/6018_2.png) [@aoe](https://community.fibery.io/u/aoe)\
**Post date:** [December 10, 2023, 4:42pm UTC](https://community.fibery.io/t/subscription-space-template-formula-missing-zero-padding/5452/10 "2023-12-10T16:42:20Z")

</div>

Revisited this with a much simpler to read (and understand) script version. Leaving it here for anyone interested.

```auto
// Developer reference is at api.fibery.io/#action-buttons
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    // Get current date
    const currentDate = new Date(entity['Next billing date']);

    // How many months per year does this happen?
    const occurrence = entity['Occurrence'];

    // Calculate next date
    const nextDate = new Date(currentDate.setMonth(currentDate.getMonth() + 12 / occurrence));

    // Update next billing date
    await fibery.updateEntity(entity.type, entity.id, {
        'Next billing date': nextDate.toISOString()
    });
}

```
