Export ElementAttributes to ASCII file

Post Reply
wypwong
Posts: 22
Joined: Tue Aug 13, 2013 2:35 pm
OLAP Product: TM1
Version: TM1 10.1
Excel Version: 2007

Export ElementAttributes to ASCII file

Post by wypwong »

I have about 20 }ElementAttributes_xx cubes like below that I want to export to Text File, but they all have different numbers of attributes. Instead of having 20 different TI, I want to have just one. Can someone help me on how I can approach it please?

Dim1 Attribute1 Attribute2 Attribute3
A A1 A2 A3
B B1 B2 B3
C C1 C2 C3

Instead of having the ASCIIOUTPUT as
A A1
A A2
A A3
B B1
B B2
....

And I want my output just look like the cube.
A A1 A2 A3
B B1 B2 B3
C C1 C2 C3
declanr
MVP
Posts: 1828
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: Export ElementAttributes to ASCII file

Post by declanr »

Parameter:
psDimName

Prolog:

Code: Select all


cAttrDim = '}ElementAttributes_' | psDimName;
cExportFile = <MyFolder> | 'AttributeExport_' | psDimName | '.txt';

sExportString = 'Element Name';
iCount = 1;
iMax = DimSiz ( cAttrDim );
While ( iCount <= iMax );
	sAttrName = DimNm ( cAttrDim, iCount );
	sExportString = sExportString | ', ' | sAttrName;
	iCount = iCount + 1;
End;

AsciiOutput ( cExportFile, sExportString );

iCount = 1;
iMax = DimSiz ( psDimName );
While ( iCount <= iMax );
	sElementName = DimNm ( psDimName, iCount );
	sExportString = sElementName;
	iSubCount = 1;
	iSubMax = DimSiz ( cAttrDim );
	While ( iSubCount <= iSubMax );
		sAttrName = DimNm ( cAttrDim, iSubCount );
		sAttrType = DType ( cAttrDim, sAttrName );
		If ( sAttrType @= 'AN' );
			sAttrValue = NumberToString ( AttrN ( psDimName, sElementName, sAttrName ) );
		Else;
			sAttrValue = AttrS ( psDimName, sElementName, sAttrName );
		EndIf;
		sExportString = sExportString | ', ' | sAttrValue;
		iSubCount = iSubCount + 1;
	End;
	AsciiOutput ( cExportFile, sExportString );
	iCount = iCount + 1;
End;
	
I have only written this code in the reply box here so there may be typos that you need to correct as I haven't tested it in an editor.
It would also be sensible for you to expand the code to put quotes around each individual string; as otherwise if your attributes contain commas then it would mess up the export file (likewise you could change the delimiter to a pipe etc etc etc.)
Whatever changes you want to make it should be a good starting point.
Declan Rodger
wypwong
Posts: 22
Joined: Tue Aug 13, 2013 2:35 pm
OLAP Product: TM1
Version: TM1 10.1
Excel Version: 2007

Re: Export ElementAttributes to ASCII file

Post by wypwong »

Declan

Thank you very much, it's very useful.

I have changed the code a little bit, as I already have a view with level 0 on the dimension.

Phyllis
declanr
MVP
Posts: 1828
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: Export ElementAttributes to ASCII file

Post by declanr »

Phyllis,

Thats good; it would be great if you could post your reworked solution here so that other users could benefit from it in the future.

Thanks,
Declan
Declan Rodger
wypwong
Posts: 22
Joined: Tue Aug 13, 2013 2:35 pm
OLAP Product: TM1
Version: TM1 10.1
Excel Version: 2007

Re: Export ElementAttributes to ASCII file

Post by wypwong »

I have a view to look at level 0, so main code are done in data tab. I need to put in char(34) / double quotation mark, as I need to use the export in Qlikview, by doing the string, the system thinks it is one long text.

Code: Select all

  Data
  
## To print Header
#####################

IF( vExported @= '' );

   vExportString = pDim | char(34); 

   vHCount = 1;

   tHCount = DIMSIZ (vDim2); 

   WHILE ( vHCount <= tHCount);

      vAttrName = DIMNM (vDim2, vHCount);

      IF ( vHCount = tHCount);
         vExportString = vExportString | ', ' | char(34) | vAttrName  ;
      ELSE;
          vExportString = vExportString | ', ' | char(34) | vAttrName | char(34)  ;
      ENDIF;

      vHCount = vHCount + 1;
 
   END;

   ASCIIOutput( cExportFile, vExportString); 
  vExported ='X';
ENDIF;

#====================================#

IF( vExported @<> '' );

## To print data
#####################

vCount = 1;

tCount = DIMSIZ (vDim2); 

vExportString = V1 | char(34) ; 

WHILE ( vCount <= tCount);

   vAttrName = DIMNM (vDim2, vCount);

   vAttrType = DTYPE (vDim2, vAttrName);

   IF (vAttrType @= 'AN');
      vAttrData = NumberToString (ATTRN (pDim, V1, vAttrName)); 
   ELSE;
      vAttrData = Attrs(pDim, V1, vAttrName); 
   ENDIF;   
   

   IF ( vCount = tCount);
      vExportString = vExportString | ', ' | char(34) | vAttrData  ;
   ELSE;
      vExportString = vExportString | ', ' | char(34) | vAttrData | char(34)  ;
   ENDIF;


   vCount = vCount + 1;

END;

 ASCIIOutput( cExportFile, vExportString); 

ENDIF;

Post Reply