I think PavoGa is on the right track. The best way I know of to be able to re-create a completely unstructured hierarchy will be to store all the parent-child relationships in attributes in a flat file and use that file to rebuild the dimension.
The tricky part is going to be populating those attributes. Instead of working your way up the tree you'll need to work your way down which we all know is more difficult because there may be multiple branches you'll have to keep track of while in the process of stepping down. So, what would basically be involved is looping through the dimension, checking to see if the element has a parent. If no parent is found then you know that element is the start of a tree. If it's the first one like that then it is the first tree. You then step down this tree looking for the first child (with ELCOMPN and ELCOMP) . The child is the element and the parent is the value. Write this out to the file. Now, using the child, see if it also has children the child is the element and you are looking to see if it has children. If it does then the first child is the element you store the parent attribute against the parent is the value. Keep going until no more children found and then you go back up to the innermost loop and look for it's next child and move down. Once you are at the end of all the branches for an element in the original loop then you look for the next element that has no parent. Once found that becomes your next tree and so forth.
You should be able to use this file to re-create your hierarchies.
I had a colleague who created VBA code a long time ago that does that step-down. I know it's not exactly what you need but it might be of help because with just a little bit of modification, and converting to TI code. it should be able to create the file I am talking about. Here posted is the entire VBA module
Code: Select all
Option Explicit
Public MyServer As String
Public MyDim As String
'******************************************************************************************
'*** Description: These macros will create a list all children and their parents ***
'*** for a selected parent element from a tm1 dimension. ***
'*** The alternate long names as well as a path code are also ***
'*** provided. These columns can be imported into another database. ***
'******************************************************************************************
Sub DigDown()
Dim MyLevel As Integer
Dim MyStructure(15) As Integer
Dim TotChildren As Integer
Dim counter As Integer, Layer As Integer, MaxLayer As Integer
Dim MyChild As String, MyParent As String
Dim MyPad As String, LstChar As Integer
Dim MyRow As Long
MyServer = Range("MyServer").Value
MyDim = MyServer & ":" & Range("MyDimension").Value
MyParent = Range("MyRollup").Value
'*** Make sure user is connected to TM1 server
If CheckConnection(MyServer, MyDim) = False Then
Exit Sub
End If
MyRow = 7 'If the starting row changes, then the one up calculation in DigParent needs to change.
'*** Erase previous results
Call Cleanup(MyRow)
MyLevel = Run("Ellev", MyDim, MyParent)
If MyLevel <> 0 Then
Cells(MyRow, 1).Value = UCase(Trim(MyParent)) 'Child element
Cells(MyRow, 2).Value = UCase(Trim(MyParent)) 'Parent element
Cells(MyRow, 3).Value = LongName(MyServer, Range("MyDimension").Value, MyParent) 'Child long name
Cells(MyRow, 4).Value = Cells(MyRow, 3).Value 'Parent long name
Cells(MyRow, 5).Value = MyLevel 'TM1 level
Cells(MyRow, 6).Value = Run("Dimix", MyDim, MyParent) 'Child TM1 index
Cells(MyRow, 7).Value = Cells(MyRow, 6).Value 'Parent Tm1 index
Cells(MyRow, 8).Value = 1 'One-up index
Cells(MyRow, 9).NumberFormat = "@"
Cells(MyRow, 9).Value = 1 'Code
Cells(MyRow, 10).Value = 1 'Depth
'*** Start drill down
Call DigParent(MyParent, MyDim, MyRow)
Else
MsgBox "This element has no children or does not exist."
End If
Cells(7, 9).Value = "1.1" 'Add a .1 to the code for the very top (first) node
Cells(7, 10).Value = 2
End Sub
Sub DigParent(MyParent, MyDim, MyRow)
Dim MyLevel As Integer
Dim TotChildren As Integer
Dim counter As Integer
Dim MyChild As String, ParentName As String, ChildName As String, Dimension As String
Dim ParentIndex As Long, ChildIndex As Long
Dimension = Range("MyDimension").Value
MyLevel = Run("Ellev", MyDim, MyParent)
If MyLevel <> 0 Then
TotChildren = Run("Elcompn", MyDim, MyParent) 'Total number of children
ParentName = LongName(MyServer, Dimension, MyParent) 'Long name of parent
ParentIndex = Run("Dimix", MyDim, MyParent) 'Tm1 index number of element
For counter = 1 To TotChildren
MyChild = Run("Elcomp", MyDim, MyParent, counter) 'Pull child elements
MyLevel = Run("Ellev", MyDim, MyChild) 'Get TM1 level of child
ChildName = LongName(MyServer, Dimension, MyChild) 'Long name of child
'*** If the child is a detail center add the center number to the name (except for the rapAccounts dim)
If IsNumeric(MyChild) And UCase(Dimension) <> "RAPACCOUNTS" Then
ChildName = MyChild & "-" & ChildName
End If
ChildIndex = Run("Dimix", MyDim, MyChild) 'Get the TM1 index number of the child
'*** Go to the next row and write out the data for the child
MyRow = MyRow + 1
Cells(MyRow, 1).NumberFormat = "@"
Cells(MyRow, 1).Value = MyChild 'Child element
Cells(MyRow, 2).Value = MyParent 'Parent element
Cells(MyRow, 3).Value = ChildName 'Long name of child
Cells(MyRow, 4).Value = ParentName 'Long name of parent
Cells(MyRow, 5).Value = MyLevel 'TM1 level of child
Cells(MyRow, 6).Value = ChildIndex 'TM1 index number of child
Cells(MyRow, 7).Value = ParentIndex 'TM1 index number of parent
Cells(MyRow, 8).Value = MyRow - 6 'One-up # - The number subtracted must be one less than the starting row
Cells(MyRow, 9).NumberFormat = "@"
'*** Call routine to determine ID code and depth
Call IDandDepth(MyRow)
'*** Call this routine to get the children of the children
Call DigParent(MyChild, MyDim, MyRow)
Next counter
End If
End Sub
Sub Cleanup(StartRow)
Dim LastCell As String
LastCell = Range("a1").SpecialCells(xlCellTypeLastCell).Offset(0, 1).Address
Range("a" & StartRow & ":" & LastCell).Clear
End Sub
Sub IDandDepth(MyRow)
'*** This routine will find the parent's code and depth and determine the child's code and depth ***
Dim LUVal As Long, LookRow As Long
LUVal = Cells(MyRow, 7) 'Grab TM1 index number of parent
LookRow = MyRow 'Separate the lookup row from the row you're actually on
'*** Keep looking up the child TM1 index column until you find the parent number
Do Until Cells(LookRow, 6) = LUVal Or LookRow = 0
LookRow = LookRow - 1
Loop
'*** Once you find it...
Cells(MyRow, 9).Value = Cells(LookRow, 9) & "." & Cells(MyRow, 8) 'Get the path code of the parent and add this child's code
Cells(MyRow, 10).Value = Cells(LookRow, 10) + 1 'Get the depth of the parent and add 1
End Sub
Function CheckConnection(MyServer, MyDimension)
'******************************************************************************************************
'*** CheckConnection insures that a TM1 connection has been made by attempting to retrieve ***
'*** some data. ***
'*** ***
'******************************************************************************************************
Dim x As Long, y As String
x = Run("DimSiz", MyDimension)
If x <> 0 Then
CheckConnection = True
Else
y = MsgBox("Error: You must connect to " & MyServer, vbOKOnly)
CheckConnection = False
End If
End Function
Sub ExtractMe()
Dim ULFile As String 'Upload file path and name
Dim MyPath As String 'Path of this workbook
Dim MyRow As Long
Dim Child_Short As String
Dim Child_Long As String
Dim Parent_Short As String
Dim Parent_Long As String
Dim ChildNum As Long
Dim ChildLevel As Integer
Dim ChildDepth As Integer
Dim ChildCode As String
Dim Header As String
MyPath = Range("MyPath").Value
ULFile = Range("MyRollup").Value & ".txt"
ULFile = MyPath & "\" & ULFile
Open ULFile For Output As #1
MyRow = 7
Header = """Child_Short"", ""Child_Long"", ""Parent_Short"",""Parent_Long"", ""ChildNum"", ""ChildLevel"", ""ChildDepth"", ""ChildCode"""
Print #1, Header
Do Until Cells(MyRow, 1) = ""
Child_Short = Cells(MyRow, 1)
Child_Long = Cells(MyRow, 3)
Parent_Short = Cells(MyRow, 2)
Parent_Long = Cells(MyRow, 4)
ChildLevel = Cells(MyRow, 5)
ChildNum = Cells(MyRow, 8)
ChildCode = Cells(MyRow, 9)
ChildDepth = Cells(MyRow, 10)
Write #1, Child_Short, Child_Long, Parent_Short, Parent_Long, ChildNum, ChildLevel, ChildDepth, ChildCode
MyRow = MyRow + 1
Loop
Close #1
End Sub
Function LongName(Server, Dimension, Element)
Select Case UCase(Dimension)
Case "RAPLOB"
LongName = Run("DBRW", Server & ":rapAt_LOBGeog", Element, "LongName")
Case "RAPGEOG"
LongName = Run("DBRW", Server & ":rapAt_LOBGeog", Element, "LongName")
Case "RAPACCOUNTS"
LongName = Run("DBRW", Server & ":rapAt_Accts", Element, "Base", "LongName")
End Select
End Function