简体   繁体   English

计算日期到最近6个月前的季度

[英]Calculate the date to the nearest quarter 6 months back

What i need is a formula that will calculate the nearest quarter no more than 6 months back. 我需要的是一个可以计算不超过6个月的最近季度的公式。 For example, imagine I have today's date in cell A1 - TODAY() – and today´s date is 13/05/2015. 例如,假设我在单元格A1-TODAY()中有今天的日期,而今天的日期是13/05/2015。 I want cell A2 to give me the date of 31/12/2014. 我希望单元格A2给我日期为31/12/2014。 As another example, today´s date at 15/03/2015, I want the formula in cell A2 to give me a date of 30/09/2014. 再举一个例子,今天的日期为2015年3月15日,我希望单元格A2中的公式给我一个日期为30/09/2014。

Please, any EXCEL geniuses out there to help? 拜托,有EXCEL天才可以提供帮助吗?

This answer is based on LibreOffice, but I think it should work in Excel. 这个答案基于LibreOffice,但我认为它应该在Excel中工作。 You need to use EOMONTH function: 您需要使用EOMONTH函数:

=EOMONTH(A1,IF(MOD(MONTH(A1),3)>0,3-MOD(MONTH(A1),3),0)) = EOMONTH(A1,IF(MOD(MONTH(A1),3)> 0,3-MOD(MONTH(A1),3),0))

Explanation: Just calculate the month of the given date (for 13/05/2015, it would be '5'), and then calculate how many months you need to add in order to make it a 'quarter' (3, 6, 9 or 12). 说明:只需计算给定日期的月份(2015年5月13日为5),然后计算要添加为“季度”(3、6、6)需要增加的月份9或12)。 In your example, it would be 3-MOD(5,3) = 3-2 = 1. Then just use the EOMONTH funcion to get the last day of the N future month (13/05/2015 + 1 month = 13/06/2015, last day = 30/06/2015). 在您的示例中,它将是3-MOD(5,3)= 3-2 =1。然后,只需使用EOMONTH函数即可获取N个未来月份的最后一天(13/05/2015 + 1 month = 13 / 2015年6月6日,最后一天= 2015年6月30日)。

For the '6 months back' part, just replace A1 with EDATE(A1,-6), in order to calculate the A1 date - 6 months back: 对于“ 6个月前”部分,只需将A1替换为EDATE(A1,-6),即可计算6个月前的A1日期:

=EOMONTH(EDATE(A1,-6),IF(MOD(MONTH(EDATE(A1,-6)),3)>0,3-MOD(MONTH(EDATE(A1,-6)),3),0)) = EOMONTH(EDATE(A1,-6),IF(MOD(MONTH(EDATE(A1,-6)),3)> 0,3-MOD(MONTH(EDATE(A1,-6)),3),0 ))

Hope it helps you. 希望对您有帮助。

=IF(MOD(MONTH(TODAY()),3)=0,EOMONTH(TODAY(),-6),IF(MOD(MONTH(TODAY()),3)=1,EOMONTH(TODAY(),-4),EOMONTH(TODAY(),-2)))

I think that this should do what you want it to do. 我认为这应该做您想做的事。

You didn't mention what version of Excel you were using, so the formula may be different in earlier versions of Excel. 您没有提到使用的是哪个版本的Excel,因此该公式在早期版本的Excel中可能会有所不同。

This structure gives you the verbal description of "the nearest quarter no more than 6 months back." 此结构为您提供“不超过6个月的最近季度”的口头描述 however that is not reflected in the example that you gave. 但是,您提供的示例中没有反映出这一点。 You are going to need to take what I gave you and add another set of tests where you work out whether you want the nearest quarter or the nearest quarter greater than 3 months away. 您将需要接受我给您的内容,并添加另一组测试,在此工作中您将要得出最近的一个季度还是距离三个月以上的最近一个季度。

UPDATE 更新

A slightly less elegant way to do this would be to create a 2 x 12 table, your key being 1 through 12 and the value column being the Quarter End date that you want the formula to return. 执行此操作的一种不太优雅的方法是创建一个2 x 12表,您的键是1 through 12 ,值列是希望公式返回的Quarter End日期。 Name the range, something like qtr_ends, then all you need to do is this. 命名范围,类似qtr_ends,然后您要做的就是这个。

=VLOOKUP(MONTH(TODAY()),qtr_ends,2,0)

That being said, you also have not specified if you are okay with a simple End of Month Quarter End, or if that Quarter End needs to be a business day. 话虽如此,您也没有指定您是否可以使用简单的“月末季度末”,或者该季度末需要作为工作日。 If it does, then this becomes much more difficult to do formulaically, and just creating a lookup table where you key is a concatenation of month and year would probably be the best. 如果是这样,那么按公式进行操作将变得更加困难,仅创建一个您将关键字与年份和年份串联的查找表可能是最好的。 It is more manual, but it would probably take a lesser amount of time to manually create the table for several years than is would be to actually work all of the logic of getting an EOM date that is also a BD. 它是更手动的,但是与实际使用获取EOM日期(也是BD)的所有逻辑的实际操作相比,几年手动创建表所花费的时间可能更少。

您的问题可以使用更多数据“尖点”示例来说明将返回值置于四分之一或另一个范围内的边界线,但这似乎可以满足您的两个示例。

=DATE(YEAR(EDATE(A2, -6)), MROUND(MONTH(EDATE(A2, -6)), 3)+1, 0)

Try this formula 试试这个公式

=COUPNCD(EDATE(A1,-6),DATE(9999,12,31),4)

EDATE function gives you the date 6 months back from A1 then COUPNCD function finds the next quarter end date. EDATE函数为您提供距A1六个月的日期,然后COUPNCD函数查找下一个季度结束日期。 That works for your two examples but might need tweaking based on your exact requirements - if today is 31/03/2015 what result do you expect? 这适用于您的两个示例,但可能需要根据您的确切要求进行调整-如果今天是31/03/2015 ,您期望得到什么结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM