Hi,
I have created the attached workflow which is basically building the hierarchical chain for each of the employees.
I wanted to do this using a loop as the number of iterations is not fixed in the real life example.
In a perfect world I want to stop the iteration for each employee once the CEO (nr.10 in the example attached) is already present in the chain.
It can be easily done in Python. So grab the Python Script Node and enter following
import knime.scripting.io as knio
# This example script simply outputs the node's input table.
# Get Data from KNIME
df = knio.input_tables[0].to_pandas()
# Function for Chaining
def find_chain(start_id, id_map):
chain = []
current_id = start_id
while current_id not in chain: # Avoid endless loops
chain.append(current_id)
next_id = id_map.get(current_id)
if next_id is None or next_id == current_id: # End of Chain
break
current_id = next_id
return chain
# Mapping from ID zu MngtID
id_map = dict(zip(df["ID"], df["Mng ID"]))
# Chains for every ID
df["Chain"] = df["ID"].apply(lambda x: find_chain(x, id_map))
# Return to KNIME
knio.output_tables[0] = knio.Table.from_pandas(df)
Looks like a great answer, @ActionAndi! It was definitely an interesting challenge from @Marco_Timperi. I developed an alternative dynamic flow that adapts to changes in the number of employees and chain command without affecting the process. Hope this helps!