-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfxPath.pq
57 lines (54 loc) · 2.07 KB
/
fxPath.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// fxPath
// --------------------------- Function ------------------------------
let
// --------------------------- Function segment -----------------------------------
fxPath = (
PathTable as table,
EntityKeyColumn as text,
ParentEntityKeyColumn as text,
KeyToCalculate as number
) as text =>
let
// Helper function to recursively get the path
GetPath = (currentKey as number, currentPath as list) as list =>
let
currentRow = Table.First(Table.SelectRows(PathTable, each Record.Field(_, EntityKeyColumn) = currentKey), null),
parentKey = if currentRow = null then null else Record.Field(currentRow, ParentEntityKeyColumn),
newPath = List.Combine({currentPath, {Text.From(currentKey)}})
in
if parentKey = null or parentKey = currentKey then
newPath
else
@GetPath(parentKey, newPath),
// Start path construction from the key to calculate
PathList = GetPath(KeyToCalculate, {}),
// Combine the list into a pipe-delimited string
finalPath = Text.Combine(List.Reverse(PathList), "|")
in
finalPath,
// --------------------------- Documentation segment ------------------------------
documentation = [
Documentation.Name = "fxPath",
Documentation.Description = "This function calculates the hierarchical path for a given entity within a parent-child structure, similar to the DAX PATH function.",
Documentation.Source = "Custom Function",
Documentation.Version = "1.1",
Documentation.Author = "",
Documentation.Examples =
{
[
Description = "Calculate the path for entity with EntityKey = 4",
Code = "fxPath(EntityTable, ""EntityKey"", ""ParentEntityKey"", 4 )",
Result = "1|4"
]
}
]
// --------------------------- Output ----------------------------------------------
in
Value.ReplaceType(
fxPath,
Value.ReplaceMetadata(
Value.Type(fxPath),
documentation
)
)
// ------------------------------------------------------------------------------------