Trying to Compute Months between two Dates

I used these formula to calculate number of months between two dates.
Whereas,
Start Date - LastPrincipalChangeDate
End Date - DateofTransaction

Expand for the Expression Used

IF(YEAR([LastPrincipalChangeDate])=YEAR([DateofTransaction]),
MONTH([DateofTransaction])-MONTH([LastPrincipalChangeDate]),

IF((YEAR([LastPrincipalChangeDate])+1)=YEAR([DateofTransaction]),
(12-MONTH([LastPrincipalChangeDate]))+MONTH([DateofTransaction]),

(12-MONTH([LastPrincipalChangeDate]))+MONTH([DateofTransaction])+
((YEAR([DateofTransaction])-YEAR([LastPrincipalChangeDate])-1)*12)
))
+
IF(
Day([LastPrincipalChangeDate])<=DAY([DateofTransaction]),
,0,-1)

Then this pops-up, Can anyone enlighten me?

2X_8_82e5ad233153755dd6263c6dc5d9158bdfcb1a89.png

When I tried to put the IF statements in different VCs, it seems Appsheet wonโ€™t allow me to multiply the difference in years by 12.

Appreciate anyoneโ€™s response

Regards,
Edison

Solved Solved
0 14 2,074
1 ACCEPTED SOLUTION

Nothing wrong with the above, but, OH MY GOODNESS, Finally Figured it out! The problem was not the Formula. But the other VCs referring to the VC containing the formula. My bad, I went all through this trouble just to find that out.

To sum things up, I used your shorter expression and added some expressions to consider the day of the months.

(
((year([DateofTransaction]) * 12) + month([DateofTransaction])) - ((year([LastPrincipalChangeDate]) * 12) + month([LastPrincipalChangeDate]))
)
+IF(
OR(
Day([LastPrincipalChangeDate])<=DAY([DateofTransaction]),
AND(MONTH([LastPrincipalChangeDate])=MONTH([DateofTransaction]),
YEAR([LastPrincipalChangeDate])=YEAR([DateofTransaction]))
)
,
0,
-1)

FINALLY, itโ€™s now resolved. Sorry for the trouble I caused you, and thanks for your help Steve! Appreciate it very much!

Well at least, I get to learn a shorter expression from you! Haha

View solution in original post

14 REPLIES 14

Steve
Platinum 4
Platinum 4

Perhaps not significant, thereโ€™s an extraneous comma:
2X_2_2988ea9b03312e677d818b2be3335cbadc31d9ba.png

Steve
Platinum 4
Platinum 4

That suggests you tried just the following in a virtual column?

((YEAR([DateofTransaction])-YEAR([LastPrincipalChangeDate])-1)*12)

If not, would you?

Hi Steve!

Thanks for showing your interest on my question! Appreciate it a lot.

Yes I did that, and I feel like that seems to be the problem.

Amazing that youโ€™ve come up with a shorter formula! However,
when I tried it out, it still gives me the same error. I dissected your formula, and found out that (please expand each bullet):

Straight multiplication to a YEAR(Date) was ok. No error in the apps

But subtracting two value results of a multiplied YEAR() gives me the app error.

On the other hand, adding two value results of a multiplied YEAR() works, Why then does it not work in subtraction?
  • (sorry I canโ€™t show you the app view on the side as it contains sensitive info.)
When I try to input a text with a date format manually, the subtracting them works

But then again, when I try to convert my Dates to TEXT just to mimic the above formula, The app gets an error. Tho the expression says it's not erroneous. The App says error again.

And to add some context, Iโ€™m not sure if these are factors for the error?

DateofTransaction is an actual Column in my database, Date Column Type and not DateTime
  • Auto Compute doesnโ€™t have any formula, except for initial value - TODAY()
LastPrincipalChangeDate is a Virtual Column, Date Column Type and not DateTime (computed by getting the max Date of a specific table, but should be lower than DateofTransac)
  • All else doesnโ€™t have anything
This is the Info of the VC using the calculation

Iโ€™m just concerned the fact that, thereโ€™s nothing in the INFO > ERRORS to guide me on how I can troubleshoot this issue when I get that error in my app.
And that, the expression builder seems to tell me that, my expressions are not erroneous.

Maybe this is a bug?

Or it would really be helpful if anyone can give me an alternative formula to compute for months. I tried some month calculation formula in this forum, but were not working as I intended (e.g. some only limits itโ€™s count to a year)

I suspect the problem is related to LastPrincipalChangeDate being a virtual column. Please provide a screenshot of its complete app formula expression.

SCREENSHOT of the Expression builder in the LastPrincipalChangeDate

EXPAND FOR THE EXPRESSION USED

MAX(

SELECT(Daily Loan Transaction[DateofTransaction],
AND(
[PNNo.]=[_THISROW].[PNNo.],
[DateofTransaction]<[_THISROW].[DateofTransaction],
OR(NOT([LoanReceivable]=0),ISNOTBLANK([LoanReceivable]))
))
+
LIST([PNNo.].[DateAvailed])
)

Basically, what Iโ€™m trying to compute here is to get the max date in the same table , with the following condition:

  • the filtered table should be lower than the transaction date of the current row.
  • the filtered table should not have records with LoanReceivable column valued at 0. Meaning there was a transaction for the Loan Receivable either an increase or decrease in LoanReceivable.
  • the filtered table should have the same ParentKey (PNNo.) with the current row.

Then after those are filtered, get the MAX date. together with the Date Availed under the Parent Table (in cases, thereโ€™s no record under the filtered table.

Since youโ€™ve mentioned it, I tried using ANY instead of MAX and it worked. Maybe then, Iโ€™m using MAX expression incorrectly? although the expression builder doesnt meantion of any errors.

Although, I'm not sure if the error lies in LastPrincipalChangeDate alone. ,I tried TODAY() instead of DateofTransaction. Just to see if LastPrincipalChangeDate is the error. And when I did that, it works, and there was no error... so I don't know already haha.

Thank you for taking the time to solve this with me.

So itโ€™s working as you want?

Hi steve,

No not yet, was just appreciating your effort to help me out until now.

Going back here, what happens when you click the Test button?

It's the same error showing

And when I click that ? button, it leads me nowhere in the Errors and Warnings, just plain page.

Please do the following:

  1. Create a virtual column with an app formula of YEAR([DateofTransaction]). Use the Test button and review the results for unexpected values. Do not Save the expression or leave Expression Assistant.

  2. Replace the expression from (1) with YEAR([LastPrincipalChangeDate]). Again, use the Test button and review the results for oddities. Do not leave Expression Assistant.

  3. Repeat the process again with the expression YEAR([DateofTransaction]) * 12.

  4. And repeat again with the expression YEAR([LastPrincipalChangeDate]) * 12.

  5. Press the Cancel button to leave Expression Assistant.

  6. Delete this new virtual columnโ€“it was only for testing.

Stop at the first step that produces unexpected results and post a screenshot that demonstrates those results.

Yeahโ€ฆ Thatโ€™s not surprisingโ€ฆ

Nothing wrong with the above, but, OH MY GOODNESS, Finally Figured it out! The problem was not the Formula. But the other VCs referring to the VC containing the formula. My bad, I went all through this trouble just to find that out.

To sum things up, I used your shorter expression and added some expressions to consider the day of the months.

(
((year([DateofTransaction]) * 12) + month([DateofTransaction])) - ((year([LastPrincipalChangeDate]) * 12) + month([LastPrincipalChangeDate]))
)
+IF(
OR(
Day([LastPrincipalChangeDate])<=DAY([DateofTransaction]),
AND(MONTH([LastPrincipalChangeDate])=MONTH([DateofTransaction]),
YEAR([LastPrincipalChangeDate])=YEAR([DateofTransaction]))
)
,
0,
-1)

FINALLY, itโ€™s now resolved. Sorry for the trouble I caused you, and thanks for your help Steve! Appreciate it very much!

Well at least, I get to learn a shorter expression from you! Haha

OUT-FRIKKINโ€™-STANDING!!! Well done!

Steve
Platinum 4
Platinum 4

For my own clarity, I reformatted your expression. Iโ€™m including it here just for my reference:

(
  IF(
    (
      YEAR([LastPrincipalChangeDate])
      = YEAR([DateofTransaction])
    ),
    (
      MONTH([DateofTransaction])
      - MONTH([LastPrincipalChangeDate])
    ),
    IF(
      (
        (YEAR([LastPrincipalChangeDate]) + 1)
        = YEAR([DateofTransaction])
      ),
      (
        (12 - MONTH([LastPrincipalChangeDate]))
        + MONTH([DateofTransaction])
      ),
      (
        (12 - MONTH([LastPrincipalChangeDate]))
        + MONTH([DateofTransaction])
        + (
          (
            YEAR([DateofTransaction])
            - YEAR([LastPrincipalChangeDate])
            - 1
          )
          * 12
        )
      )
    )
  )
  + IF(
    (
      DAY([LastPrincipalChangeDate])
      <= DAY([DateofTransaction])
    ),
    ,
    0,
    -1
  )
)

Steve
Platinum 4
Platinum 4

Or just try this (TEST THOROUGHLY!!!๐Ÿ˜ž

(
  ((year([DateofTransaction]) * 12) + month([DateofTransaction]))
  - ((year([LastPrincipalChangeDate]) * 12) + month([LastPrincipalChangeDate]))
)
Top Labels in this Space