Does it work on a month as 30-day period or calendar month… it returns 1 if I compare today 30/07 with 31/05, shouldn’t that be two?
Hey @Dawid_Nawrot! This is a good question. I think that the behavior you’re seeing makes sense if you are thinking the difference between two days in “full” months. Since 05/31 is the last day of the month, then a “full” month has only passed if the end date is the last day of the month. The last day of July is 07/31, making 07/30 the second-to-last day of the month, so only one “full” month passes between 05/31 and 07/30. If you use 05/31-07/31, then Looker’s
diff_months function returns
2 as expected.
However, I wanted to make sure, so I did a bit of digging and one of our engineers told me that we use the
Moment.diff function from the
moment. This pull request lays out their thinking behind their decision: https://github.com/moment/moment/pull/571
This example shows how their approach can sometimes yield unintuitive results:
// Jan 31 - Feb 28 31 - 28 = 3; // difference in days (31 + 28) / 2 = 29.5; // average month length 3 / 29.5 = 0.101; // the partial difference in months 0 - 1 = -1; // the integer difference in months -1 + 0.101 = -0.899; // the total difference in months
- calculate the difference in days (
extract_dayson the start and end date and simple subtraction)
- Find the average month length (find the total number of days in a month, so for May it would be 31 since 05/31 is the last day of that month)
- Divide the difference in days from Step 1 by the average month length from Step 2
- Find the simple difference in months (
extract_monthson the start and end date and simple subtraction)
- Add the results of Step 3 and Step 4 together to find the total difference.
Looker then truncates that value to an integer, so
-0.899 here becomes
Let me know if you have any more questions about how this works, or if I’ve missed anything in my explanation!
Okay I know Moment a little bit and see why they think that. The only reason why I might disagree a little bit is that (assumption) most of us, who use BI Tools, come from some kind of SQL background and most often than not, we expect the behaviour of such functions to be as close as to those of SQL (I know SQLs might differ too).
I do have JS and PHP background too, so it’s not like it’s a rocket science to me, but somehow I wish a lot of coding behind Looker resembled more of what usually happens in SQL.
Anyway, thank you for detailed explanation