我有以下三个表(表头位于第01行):
表1:基于日期的订阅
| A | B | C | D |
---|-----------|--------------|------------|---------------------|
01 | ID | Type | Start Date | End Date |
02 | ID1 | T1 | 17.4.1 | |
03 | ID2 | T2 | 17.9.1 | 18.5.15 |
04 | ID3 | T1 | 20.6.1 | |
...
表2:每种订阅类型的每月费用
| A | B
---|-------------|---------------
01 | Type | Yearly Cost
02 | T1 | 120
03 | T2 | 144
...
表3:费用预测
| A | B
---|-----------|------------
01 | Year | Expense s
02 | 2017 | 138 (T1 9 months: 90 + T2 4 months: 48)
03 | 2018 | 180 (T1 12 months: 120 + T2 5 months 60)
04 | 2019 | 120 (T1 12 months)
05 | 2020 | 190 (T1 12 months + 7 months)
06 | 2021 | 1240 (T1 24 months)
...
我想自动计算第三张表的B列(不使用helper列,当前是使用helper表完成的...)
我知道不能使用sumif()完成此操作,但是可以使用数组公式来实现吗?
我将下面的公式的伪代码放入表3中:
for each line of table 2
'
'
' case 1: start date in year, end date not in year
'
if and(table3[year]=year(table1[startdate]), or(isblank(year(table1[enddate]),table3[year] < year(table1[enddate]))) then
'
' count months from startdate to end of year then multiply with monthlyrate
'
addtosum (12-month(table1[startdate]))*index(Table2,match(Table1[@[Type]],Table2[Type],0),2)
'
'
' case 2: startdate in year, enddate in year
'
elseif and(table3[year]=year(table1[startdate]),table3[year] = year(table1[enddate])) then
'
' count months between startdate and enddate then multiply with monthlyrate
'
addtosum (month(table1[enddate])-month(table1[startdate]))*index(Table2,match(Table1[@[Type]],Table2[Type],0),2)
'
'
' case 3: year larger than startdate, enddate is not reached
'
elseif and(table3[year]>year(table1[startdate]), or(isblank(year(table1[enddate]),table3[year]<year(table1[enddate]))) then
'
' multiply 12 months with monthlyrate
'
addtosum 12*index(Table2,match(Table1[@[Type]],Table2[Type],0),2)
'
'
' case 4: year larger than startdate, enddate is reached
'
elseif and(table3[year]>year(table1[startdate]),table3[year]=year(table1[enddate])) then
'
' count months from begining of year to enddate then multiply with monthlyrate
'
addtosum (month(table1[enddate])) * index(Table2,match(Table1[@[Type]],Table2[Type],0),2)
end if
end for each
这是我创建的帮助表:
表3:费用预测
| J | K | L | M | N ...
---|-------|----|------|-----| ----- ...
01 | Year | | ID1 | ID2 | ID3
02 | 2017 | | 90 | 48 |
03 | 2018 | | 120 | 60 |
04 | 2019 | | 120 | 0 |
05 | 2020 | | 190 | |
...
这是我当前在单元格L2中拥有的公式(将其复制到L2:U5):
' Note: when calling index, column 4 is the Type, 5 is startdate, 6 is enddate)
=IF(NOT(ISBLANK(INDEX(Table1,MATCH(L$1,Table1[ID],0),5))),
IF(AND($J2 = YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),5)), OR(ISBLANK(INDEX(Table1,MATCH(L$1,Table1[ID],0),6)),$J2 < YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),6)))),
(12+1-MONTH(INDEX(Table1,MATCH(L$1,Table1[ID],0),5))),
IF(AND($J2 = YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),5)), $J2 = YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),6))),
(MONTH(INDEX(Table1,MATCH(L$1,Table1[ID],0),6))-MONTH(INDEX(Table1,MATCH(L$1,Table1[ID],0),5))),
IF(AND($J2 > YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),5)), OR(ISBLANK(INDEX(Table1,MATCH(L$1,Table1[ID],0),6)),$J2 < YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),6)))),
12,
IF(AND($J2 > YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),5)), $J2 = YEAR(INDEX(Table1,MATCH(L$1,Table1[ID],0),6))),
(MONTH(INDEX(Table1,MATCH(L$1,Table1[ID],0),6))),
0
)
)
)
)*INDEX(Table2,MATCH(INDEX(Table1,MATCH(L$1,Table1[ID],0),4),Table2[Type],0),2)/12,
0
)
在此先感谢您的帮助,
答案 0 :(得分:2)
答案 1 :(得分:0)
这是另一种解决方案:
=SUM(IF(ISBLANK(Table1[End Date]),IF(YEAR(Table1[Start Date])<Table3[@Year],12,IF(YEAR(Table1[Start Date])=Table3[@Year],13-MONTH(Table1[Start Date]),0)),IF(Table3[@Year]<YEAR(Table1[Start Date]),0,IF(Table3[@Year]=YEAR(Table1[Start Date]),IF(Table3[@Year]<YEAR(Table1[End Date]),13-MONTH(Table1[Start Date]),IF(Table3[@Year]=YEAR(Table1[End Date]),MONTH(Table1[End Date])-MONTH(Table1[Start Date])+1,0)),IF(Table3[@Year]<YEAR(Table1[End Date]),12,IF(Table3[@Year]=YEAR(Table1[End Date]),MONTH(Table1[End Date]),0)))))*SUMIFS(Table2[Monthly Cost],Table2[Type],Table1[Type]))
这是一个数组公式CTRL-SHIFT-ENTER。
希望有帮助