如何在两个日期之间查找月份之间的浮点数(Google表格)?

huangapple go评论69阅读模式
英文:

How do I find a float for the number of months between 2 days (Google Sheets)?

问题

我试图使用Google表格来计算基于按月比例计算的补偿待遇的回溯支付。可用的月份数是固定的,我想追踪到目前为止使用了多少个月。理想情况下,我想使用一个单元格来存放 relevant period 的开始日期,一个单元格用于结束日期,然后一个单元格用于月份数,而不是整数,而是浮点数。问题是,福利评估并不将一个月视为固定天数,而是评估每个月的百分比,因此例如,1月份的1天与2月份的1天具有不同的权重。我想能够在一个单元格中评估,例如,2023年1月30日到2023年5月24日之间的月数。我似乎找不到任何相关的公式,而且我没有编写自定义函数的经验,尽管我愿意学习。有现有的公式可以做到我正在尝试做的事吗?

  • 尝试使用DAYS(),然后将结果除以30,但未考虑不同月份的天数。
  • 尝试使用=DAYS(J2,I2-1)/(DAYS(EOMONTH(I2,0),EOMONTH(I2,-1))),对于每组开始和结束日期I[N]和J[N],跨足最多一个月的情况,然后使用SUM来获取该时段的月数。这种方法有效,但不是在一个单元格中完成。

演示表格,显示有效值,对单元格D2-D3所需的计算是在扩展的单元格集合G2-I11上执行的。

英文:

I'm trying to use google sheets to calculate back pay for benefits that evaluate on a pro-rated monthly basis. There is a static number of months available, and I want to track how many have been used so far. Ideally, I'd use one cell for the start date of the relevant period, one cell for the end date, and then one cell for the number of months with a floating point number instead of an integer. The problem is that the benefits evaluation doesn't assess a month as a set number of days, it assesses the percentage of each month, so for example, 1 day in January has a different weight than 1 day in February. I want to be able to evaluate, for example, the number of months between 1/30/23 and 5/24/23, all in one cell. I can't seem to find any relevant formulas, and I'm not experienced with writing custom functions, though I'm willing to learn. Are there existing formulas that can do what I'm trying to do here?

  • Tried using DAYS() and dividing the result by 30, which didn't account for varied month lengths.
  • Tried using =DAYS(J2,I2-1)/(DAYS(EOMONTH(I2,0),EOMONTH(I2,-1))) for each set of start and end dates I[N] and J[N] spanning a max of one month each, then using SUM to get the number of months in the period. This works, but not in one cell.

Demo Sheet , showing valid values, the calculations needed for cells D2-D3 are performed on an expanded set of cells G2-I11.

答案1

得分: 3

In your demo sheet, I think H6 should be 31-Jan-23 instead of 30-Jan-23. If so, then my numbers match yours.

To answer your question, the trick would be

  • to look at only the first and last month
  • and calculate the proportion of only those two months
  • all intervening months count as 100%.

So the single-cell formula works out to the following (where the start date is in cell I35 and the end date is in cell J35):

=(MONTH(J35)-MONTH(I35)-1)+(DAYS(EOMONTH(I35,0),I35-1)/(DAYS(EOMONTH(I35,0),EOMONTH(I35,-1))))+(DAYS(J35,EOMONTH(J35,-1))/(DAYS(EOMONTH(J35,0),EOMONTH(J35,-1))))

Please see this sheet for the details that explain the three parts of the formula:

  1. Intermediate months
  2. First fractional month
  3. Last fractional month

The sheet has view-only rights. To anyone who wants to edit it, please make a copy.

英文:

In your demo sheet, I think H6 should be 31-Jan-23 instead of 30-Jan-23. If so, then my numbers match yours.

To answer your question, the trick would be

  • to look at only the first and last month
  • and calculate the proportion of only those two months
  • all intervening months count as 100%.

So the single-cell formula works out to the following (where the start date is in cell I35 and the end date is in cell J35):

=(MONTH(J35)-MONTH(I35)-1)+(DAYS(EOMONTH(I35,0),I35-1)/(DAYS(EOMONTH(I35,0),EOMONTH(I35,-1))))+(DAYS(J35,EOMONTH(J35,-1))/(DAYS(EOMONTH(J35,0),EOMONTH(J35,-1))))

Please see this sheet for the details that explain the three parts of the formula:

  1. Intermediate months
  2. First fractional month
  3. Last fractional month

The sheet has view-only rights. To anyone who wants to edit it, please make a copy.

答案2

得分: 0

=INDEX(LET(Λ,EOYMONTH(SEQUENCE(DAYS(C2,B2-1),1,B2,1),),
ROUND(SUM(MAP(UNIQUE(Λ),LAMBDA(Σ,COUNTIF(Λ,Σ)/DAY(Σ)))),2)))

  • 在第3行的输出中,与您期望的输出4.03相比,存在4.06的偏差,因为您似乎在拆分计算部分(Cell_I6)中将月底日期视为30-Jan而不是31-Jan
英文:

You may try:

=index(let(Λ,eomonth(sequence(days(C2,B2-1),1,B2,1),),
       round(sum(map(unique(Λ),lambda(Σ,countif(Λ,Σ)/day(Σ)))),2)))
  • there's a deviation of output in row_3 4.06 compared to your expected output of 4.03 since you seem to have taken the end of month date as 30-Jan and not 31-Jan within your breakup calculation part (Cell_I6)

如何在两个日期之间查找月份之间的浮点数(Google表格)?

huangapple
  • 本文由 发表于 2023年5月24日 18:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定