如何根据可用性 table 计算 next/same 天?
How to calculate next/same day based on availability table?
我正在研究 excel 公式,以根据 excel table 查找下一个可用日期。我有两个 tables,A 和 B,table A 有日期和结果列,table B 有日期和计数,我想从 table B 获取日期根据 table A 和 B 的日期计数进入 table A 的结果列。
请注意 table A 的 "result" 列中的日期是预期输出,它始终与 table A 的日期列中的日期相同或更高。日期格式为 month/date/year
table A
Date Result
01-02-2018 01-02-2018
01-02-2018 01-02-2018
01-05-2018 01-05-2018
01-05-2018 01-05-2018
01-05-2018 01-07-2018
01-05-2018 01-07-2018
01-06-2018 01-07-2018
01-07-2018 01-10-2018
01-09-2018 01-10-2018
01-11-2018 01-12-2018
01-12-2018 01-12-2018
table B
Date Count
01-02-2018 3
01-03-2018 1
01-04-2018 3
01-05-2018 2
01-07-2018 3
01-10-2018 3
01-12-2018 2
假设 "table A" 放入 A1:A9,包括 A1
: "table A", A2:B2
(header) : "Date"和 "Result"
"table B" 放入 A11:A1,包括 A11
: "table B", A12:B12
(header) : "Date" 和"Count"
在"Result"列B3
,公式复制下来:
=IF(COUNTIF(A:A3,A3)<=IFERROR(VLOOKUP(A3,$A:$B,2,0),0),A3,AGGREGATE(15,6,$A:$A/($A:$A>=MAX(A3,INDEX(A3:A,MATCH(1,INDEX(0+(A3:A<>A3),0),0)))),1))
这个任务花了我比预期更长的时间来解决,最后我在几个 帮助列 的帮助下想出了一个解决方案(不确定它是否是一个选项? ).
假设您有以下命名范围:
- TblA_Date 是您
Table A
中的所有日期(这是可选的,因为我没有在我的解决方案中使用它...);
- TblB_Date 是您
Table B
. 中的所有日期
首先,我 "expanded" 你的 Table B
到列出所有日期的列表结构:
我的示例中 Cell J2
中第一个辅助列的公式是:
=SUM($I:I2)
which is to find the running total of the counts in Table B
.
单元格 M2
中第二个辅助列的公式为:
=INDEX(TblB_Date,MATCH(1,--(ROW(A1)<=$J:$J),0))
which is "laying" out all the dates from Table B
using row position as a reference and comparing it with the running total of counts and returning the corresponding date. Given that it is an array formula you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise it will not function correctly. Then you can simply drag the formula down to apply across.
Please note I have given the second helper column M2:M18
(excluding M19
which is an error) a name TblB_Date_Expanded.
下一步是根据您的条件 TblB_Date_Expanded 找到所需日期的位置
- "for every date in table A, the result date is either same date or the next higher date" AND
- 来自
Table B
的日期最多只能使用其对应的计数。
E2
单元格中的公式为:
=MAX(MATCH(AGGREGATE(15,6,TblB_Date_Expanded/(A2<=TblB_Date_Expanded),1),TblB_Date_Expanded,0),E1+1)
其中 A2
是 Table A
中的第一个日期,您将在我的下一张屏幕截图中看到。
The logic is to use AGGREGATE function to return the closest starting date in TblB_Date_Expanded
base on the given date in Table A
, then use MATCH function to return the row position, and lastly use MAX function to increase the position by 1
if the same position has been used already (in the row above).
有了已知的行位置,我们可以使用以下公式轻松地从 TblB_Date_Expanded
中 return 相应的日期:
=INDEX(TblB_Date_Expanded,E2)
Thoughts, the difficult part that I cannot overcome is the use of "circular reference" or "self-reference" in the helper columns. In other words, I am not sure how to use a single formula to return an array of values whose calculation is depending on its precedent value. I hope someone may come up a more elegant way of solving this question :)
如果您有任何问题,请告诉我。干杯:)
我正在研究 excel 公式,以根据 excel table 查找下一个可用日期。我有两个 tables,A 和 B,table A 有日期和结果列,table B 有日期和计数,我想从 table B 获取日期根据 table A 和 B 的日期计数进入 table A 的结果列。 请注意 table A 的 "result" 列中的日期是预期输出,它始终与 table A 的日期列中的日期相同或更高。日期格式为 month/date/year
table A
Date Result
01-02-2018 01-02-2018
01-02-2018 01-02-2018
01-05-2018 01-05-2018
01-05-2018 01-05-2018
01-05-2018 01-07-2018
01-05-2018 01-07-2018
01-06-2018 01-07-2018
01-07-2018 01-10-2018
01-09-2018 01-10-2018
01-11-2018 01-12-2018
01-12-2018 01-12-2018
table B
Date Count
01-02-2018 3
01-03-2018 1
01-04-2018 3
01-05-2018 2
01-07-2018 3
01-10-2018 3
01-12-2018 2
假设 "table A" 放入 A1:A9,包括 A1
: "table A", A2:B2
(header) : "Date"和 "Result"
"table B" 放入 A11:A1,包括 A11
: "table B", A12:B12
(header) : "Date" 和"Count"
在"Result"列B3
,公式复制下来:
=IF(COUNTIF(A:A3,A3)<=IFERROR(VLOOKUP(A3,$A:$B,2,0),0),A3,AGGREGATE(15,6,$A:$A/($A:$A>=MAX(A3,INDEX(A3:A,MATCH(1,INDEX(0+(A3:A<>A3),0),0)))),1))
这个任务花了我比预期更长的时间来解决,最后我在几个 帮助列 的帮助下想出了一个解决方案(不确定它是否是一个选项? ).
假设您有以下命名范围:
- TblA_Date 是您
Table A
中的所有日期(这是可选的,因为我没有在我的解决方案中使用它...); - TblB_Date 是您
Table B
. 中的所有日期
首先,我 "expanded" 你的 Table B
到列出所有日期的列表结构:
我的示例中 Cell J2
中第一个辅助列的公式是:
=SUM($I:I2)
which is to find the running total of the counts in
Table B
.
单元格 M2
中第二个辅助列的公式为:
=INDEX(TblB_Date,MATCH(1,--(ROW(A1)<=$J:$J),0))
which is "laying" out all the dates from
Table B
using row position as a reference and comparing it with the running total of counts and returning the corresponding date. Given that it is an array formula you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise it will not function correctly. Then you can simply drag the formula down to apply across.Please note I have given the second helper column
M2:M18
(excludingM19
which is an error) a name TblB_Date_Expanded.
下一步是根据您的条件 TblB_Date_Expanded 找到所需日期的位置
- "for every date in table A, the result date is either same date or the next higher date" AND
- 来自
Table B
的日期最多只能使用其对应的计数。
E2
单元格中的公式为:
=MAX(MATCH(AGGREGATE(15,6,TblB_Date_Expanded/(A2<=TblB_Date_Expanded),1),TblB_Date_Expanded,0),E1+1)
其中 A2
是 Table A
中的第一个日期,您将在我的下一张屏幕截图中看到。
The logic is to use AGGREGATE function to return the closest starting date in
TblB_Date_Expanded
base on the given date inTable A
, then use MATCH function to return the row position, and lastly use MAX function to increase the position by1
if the same position has been used already (in the row above).
有了已知的行位置,我们可以使用以下公式轻松地从 TblB_Date_Expanded
中 return 相应的日期:
=INDEX(TblB_Date_Expanded,E2)
Thoughts, the difficult part that I cannot overcome is the use of "circular reference" or "self-reference" in the helper columns. In other words, I am not sure how to use a single formula to return an array of values whose calculation is depending on its precedent value. I hope someone may come up a more elegant way of solving this question :)
如果您有任何问题,请告诉我。干杯:)