我有3列的Excel。
OrgunitCode ParentOrgunitCode OrgunitName
1500 Nan Head_Org
3200 1500 2nd_level_Org
3201 1500 other_2nd_lever_Org
..............................................................
971248197 827484 n_level_Org
103048197 513834 n2_level_Org
我需要把它转换为完整的层次结构行(用Excel或Python)。
OrgunitName OrgunitCode OrgunitName(lvl_2) OrgunitCode(lvl_2) .. OrgunitName(lvl_5) OrgunitCode(lvl_5)
Head_Org 1500 2nd_level_Org 3200 n_level_Org 971248197
Head_Org 1500 2nd_level_Org 3200 n2_level_Org 513834
..............................................................
所以我想使用PivotTables,或在excel中查找,但如果我从 "Parent "开始搜索,我不知道有多少 "Childrens",所以我很困惑。而且我不知道如何从最低点开始,因为如何找到他们。
哦......使用层次结构的乐趣。总是很有趣。
首先,在处理父子关系时,我总是建议建立一个组织节点,比如''代表第1级;'1'、'2'......'n'代表第2级;'11'、'21'、'22'......。'nn'代表第3级等等。这将使你的层次结构工作变得更容易(如排序、寻找不同部门的后代、兄弟姐妹和祖先等)。
在你的情况下,你并不真的需要一个组织节点,但我已经把它包括在D列中供将来参考。
假设第1行是个头,数据的组织形式是:
A: OrgunitCode
B: ParentOrgunitCode
C: OrgunitName
然后在第2行添加这些公式。
D: OrgNode (not used but useful in future)
=IF(COUNTIFS($A:$A,B2)=0,"/"&A2&"/",VLOOKUP(B2,$A:$D,4,FALSE)&$A2&"/")
E: Hierarchy level
=IF(COUNTIFS($A:$A,$B2)=0,1,VLOOKUP($B2,$A:$E,5,FALSE)+1)
接下来是你要求的层次结构。将这个公式分别粘贴到单元格E2和F2中,拖动它们覆盖所有相关行和层次结构即可。
F: OrgunitName
=IF($E2=(COLUMN()-4)/2,$C2,IF($E2<(COLUMN()-4)/2,"",VLOOKUP($B2,$A:F,COLUMN(),FALSE)))
G: OrgunitCode
=IF($E2=(COLUMN()-5)/2,$A2,IF($E2<(COLUMN()-5)/2,"",VLOOKUP($B2,$A:G,COLUMN(),FALSE)))
需要注意的是,G和F中的查找列是相对的,避免在向右扩展公式时,改变数字或增加数字行来参考。但是,如果列的分割不完全如上,可能要用数字或数字引用来代替'COLUMN()'部分。
还有一点:VLOOKUP在这里也可以用,但要注意的是,对于Excel来说,这是一个很繁重的操作,所以如果你有一个非常大的层次结构,你可以考虑使用SQL中的层次结构函数、MySQL中的递归函数或类似的函数。
EDIT:按要求进行公式解释(我觉得很乱,但不知道有什么更好的办法)。
D: OrgNode (not used but useful in future)
=IF(COUNTIFS($A:$A,B2)=0,"/"&A2&"/",VLOOKUP(B2,$A:$D,4,FALSE)&$A2&"/")
Builds a hierarchy node with the parent/child relationship seperated by '/':
'COUNTIFS($A:$A,B2)=0' <- checks to see if there is a parent.
"/"&A2&"/" <- ...If no, then this is the/a top node, so take the OrgCode and wrap it in '/'
VLOOKUP(B2,$A:$D,4,FALSE)&$A2&"/") <- Else, look up the parent's orgnode and add this department's OrgCode and '/' to the parent's.
E: Hierarchy level
=IF(COUNTIFS($A:$A,$B2)=0,1,VLOOKUP($B2,$A:$E,5,FALSE)+1)
Just determines the hierarchy level of the dept in question.
=IF(COUNTIFS($A:$A,$B2)=0 <- checks to see if there is a parent.
1 <- if not, then make this hierarchy level 1
VLOOKUP($B2,$A:$E,5,FALSE)+1) <- Else, lookup the hierarchy level of the parent and add 1
F: OrgunitName
=IF($E2=(COLUMN()-4)/2,$C2,IF($E2<(COLUMN()-4)/2,"",VLOOKUP($B2,$A:F,COLUMN(),FALSE)))
=IF($E2=(COLUMN()-4)/2, <- checks to see if the current hierarchy column is equal to the hierarchy level of the current department
$C2 <- if so, return the dept name
IF($E2<(COLUMN()-4)/2 <- else check if the current hierarchy column is lower than the hierarchy level of the current department
"" <- if so, remain blank
VLOOKUP($B2,$A:F,COLUMN(),FALSE))) <- else lookup the value in this column of the parent department.
G: OrgunitCode
=IF($E2=(COLUMN()-5)/2,$A2,IF($E2<(COLUMN()-5)/2,"",VLOOKUP($B2,$A:G,COLUMN(),FALSE)))
- Same explanation as above, just a different column reference and returns department code instead
=IF($E2=(COLUMN()-5)/2
$A2
IF($E2<(COLUMN()-5)/2
""
VLOOKUP($B2,$A:G,COLUMN(),FALSE)))
并将其刻画出来。
(COLUMN()-4)/2 <- Instead of hardcoding the hierarhy levels, this uses the column index to calculate the relevant hierarchy level in the given column. Column F is index 6: (6-4)/2 = 1. Column H is index 8: (8-4)/2 = 2. etc.
COLUMN() <- In the VLOOKUP reference: We all know VLOOKUP is annoying beacause we have to manually change the number in the col_index_num parameter when dragging across columns. But similar to above, we can use the column index number as a relative reference. Hence her, column F is index 6 and will look up the 6th column. Column G will return the 7th, H the 8th... you get the point.