Introduction
The Financial Statement Version (FSV) is one of the most critical structures in SAP for representing how financial data rolls up across accounts and reporting nodes. This article walks through how to use Python within Datasphere Dataflows to flatten the hierarchical FSV structure into a simple, analysis-ready table.
Use case A: Often flattened structures are required by a downstream system (e.g. Onestream, Snowflake or Azure) from SAP Datasphere.
Use case B: Reporting requirements on flattened structure in SAC (SAP Analytics Cloud) stories.
*Figure : Example of a Financial Statement Version hierarchy with multiple reporting levels.*
Python Transform
The FSV in SAP stores hierarchical information – each node represents a reporting group, which may have sub-nodes or leaf nodes pointing to G/L accounts.
Source Data:
Using a Python Transform node inside a Datasphere Dataflow, we can achieve this through a recursive traversal. The logic builds two key lookup dictionaries:
One mapping each parent node to its childrenAnother mapping each node ID to its node name
Then, a recursive build_paths() function walks through the hierarchy using Depth-First Search (DFS) – visiting each node, building its full path (e.g., Financial Statement Version > Assets > Current Assets), and recording its level details (LEVEL1, LEVEL2, etc.).
# Ensure no missing values in parent IDs
data[“PARENT_ID”] = data[“PARENT_ID”].fillna(“ROOT”)
# Build lookup dictionary: parent → children
children_map = data.groupby(“PARENT_ID”)[“NODE_ID”].apply(list).to_dict()
names_map = data.set_index(“NODE_ID”)[“NODE_NAME”].to_dict()
# Recursive function to build paths
def build_paths(node, path):
name = names_map.get(node, “”)
current_path = path + [name] if name else path
results = [{“NODE_ID”: node,
“NODE_NAME”: name,
“FULL_PATH”: ” > “.join(current_path),
“LEVEL1”: current_path[0] if len(current_path) > 0 else None,
“LEVEL2”: current_path[1] if len(current_path) > 1 else None,
“LEVEL3”: current_path[2] if len(current_path) > 2 else None,
“LEVEL4”: current_path[3] if len(current_path) > 3 else None,
“LEVEL5”: current_path[4] if len(current_path) > 4 else None,
“LEVEL6”: current_path[5] if len(current_path) > 5 else None,
“LEVEL7”: current_path[6] if len(current_path) > 6 else None,
“LEVEL8”: current_path[7] if len(current_path) > 7 else None}]
for child in children_map.get(node, []):
results.extend(build_paths(child, current_path))
return results
# Start from ROOT (or null parent)
flattened = []
for root in children_map.get(“ROOT”, []):
flattened.extend(build_paths(root, []))
# Convert back to DataFrame
data = pd.DataFrame(flattened)
return data
The final output is a flat table showing node ID, name, full path, and level columns – ready for SAC visualizations or direct joins with GL balances.
Conclusion
This approach ensures that the FSV – a cornerstone of SAP Financials remains a powerful, adaptable asset for unified financial reporting and planning. This method simplifies downstream modelling and Python gives developers precise control over custom FSV structures or hybrid hierarchies that span SAP and non-SAP sources.
(To know more about FSV- refer the SAP learning journey)
IntroductionThe Financial Statement Version (FSV) is one of the most critical structures in SAP for representing how financial data rolls up across accounts and reporting nodes. This article walks through how to use Python within Datasphere Dataflows to flatten the hierarchical FSV structure into a simple, analysis-ready table.Use case A: Often flattened structures are required by a downstream system (e.g. Onestream, Snowflake or Azure) from SAP Datasphere.Use case B: Reporting requirements on flattened structure in SAC (SAP Analytics Cloud) stories. *Figure : Example of a Financial Statement Version hierarchy with multiple reporting levels.* Python TransformThe FSV in SAP stores hierarchical information – each node represents a reporting group, which may have sub-nodes or leaf nodes pointing to G/L accounts.Source Data:Using a Python Transform node inside a Datasphere Dataflow, we can achieve this through a recursive traversal. The logic builds two key lookup dictionaries:One mapping each parent node to its childrenAnother mapping each node ID to its node nameThen, a recursive build_paths() function walks through the hierarchy using Depth-First Search (DFS) – visiting each node, building its full path (e.g., Financial Statement Version > Assets > Current Assets), and recording its level details (LEVEL1, LEVEL2, etc.).# Ensure no missing values in parent IDs
data[“PARENT_ID”] = data[“PARENT_ID”].fillna(“ROOT”)
# Build lookup dictionary: parent → children
children_map = data.groupby(“PARENT_ID”)[“NODE_ID”].apply(list).to_dict()
names_map = data.set_index(“NODE_ID”)[“NODE_NAME”].to_dict()
# Recursive function to build paths
def build_paths(node, path):
name = names_map.get(node, “”)
current_path = path + [name] if name else path
results = [{“NODE_ID”: node,
“NODE_NAME”: name,
“FULL_PATH”: ” > “.join(current_path),
“LEVEL1”: current_path[0] if len(current_path) > 0 else None,
“LEVEL2”: current_path[1] if len(current_path) > 1 else None,
“LEVEL3”: current_path[2] if len(current_path) > 2 else None,
“LEVEL4”: current_path[3] if len(current_path) > 3 else None,
“LEVEL5”: current_path[4] if len(current_path) > 4 else None,
“LEVEL6”: current_path[5] if len(current_path) > 5 else None,
“LEVEL7”: current_path[6] if len(current_path) > 6 else None,
“LEVEL8”: current_path[7] if len(current_path) > 7 else None}]
for child in children_map.get(node, []):
results.extend(build_paths(child, current_path))
return results
# Start from ROOT (or null parent)
flattened = []
for root in children_map.get(“ROOT”, []):
flattened.extend(build_paths(root, []))
# Convert back to DataFrame
data = pd.DataFrame(flattened)
return dataThe final output is a flat table showing node ID, name, full path, and level columns – ready for SAC visualizations or direct joins with GL balances. Conclusion This approach ensures that the FSV – a cornerstone of SAP Financials remains a powerful, adaptable asset for unified financial reporting and planning. This method simplifies downstream modelling and Python gives developers precise control over custom FSV structures or hybrid hierarchies that span SAP and non-SAP sources. (To know more about FSV- refer the SAP learning journey) Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog