 <?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.analytica.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mwright</id>
	<title>Analytica Docs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.analytica.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mwright"/>
	<link rel="alternate" type="text/html" href="https://docs.analytica.com/index.php/Special:Contributions/Mwright"/>
	<updated>2026-05-21T07:02:38Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://docs.analytica.com/index.php?title=MdArrayToTable&amp;diff=17894</id>
		<title>MdArrayToTable</title>
		<link rel="alternate" type="text/html" href="https://docs.analytica.com/index.php?title=MdArrayToTable&amp;diff=17894"/>
		<updated>2010-04-17T19:39:29Z</updated>

		<summary type="html">&lt;p&gt;Mwright: /* Creating a fact table */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[category:Array Flattening Functions]]&lt;br /&gt;
[[Category:Doc Status C]] &amp;lt;!-- For Lumina use, do not change --&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
= MdArrayToTable(A,I,L) =&lt;br /&gt;
&lt;br /&gt;
Transforms a multi-dimensional array, ''A'', into a two-dimensional&lt;br /&gt;
array (i.e., a relational table) indexed by ''I'' and ''L''. The result contains one&lt;br /&gt;
row along ''I'' for each element of ''A''. ''L'' must contain a list of names&lt;br /&gt;
of the indexes of ''A'', followed by one final element. All elements of&lt;br /&gt;
L must be text values. The column corresponding to the final element&lt;br /&gt;
of ''L'' contains the cell value. If L does not contain all the&lt;br /&gt;
indexes of ''A'', array abstraction will create a set of tables indexed&lt;br /&gt;
by the dimensions not listed in ''L''.&lt;br /&gt;
&lt;br /&gt;
== Creating Sparse Relational Tables ==&lt;br /&gt;
&lt;br /&gt;
You can elect to include only the non-null / non-zero elements of «A» in the resulting table.  Normally, when you want all elements, you provide an index «I» with a length equal to the total number of cells in the array.  To include only the non-zero / non-null elements, provide an index with a length equal to the number of non-null / non-zero elements.&lt;br /&gt;
&lt;br /&gt;
''New to [[What's new in Analytica 4.2?|Analytica 4.2]]'': Prior to 4.2, only zero elements were excluded.  The ability to exclude [[Null]] elements is new.  The «omitZero» and «omitNull» parameters are new to 4.2.&lt;br /&gt;
&lt;br /&gt;
If you want to include zeroes, but not [[Null]]s, then size «I» for the number of non-Null values and specify:&lt;br /&gt;
::[[MdArrayToTable]](...,omitZero:false)&lt;br /&gt;
Likewise, if you want to include [[Null]] cells as rows, but not zeros, specify:&lt;br /&gt;
::[[MdArrayToTable]](...,omitNull:false)&lt;br /&gt;
&lt;br /&gt;
== Creating a fact table ==&lt;br /&gt;
&lt;br /&gt;
''New to [[What's new in Analytica 4.2?|Analytica 4.2]]''&lt;br /&gt;
&lt;br /&gt;
::[[MdArrayToTable]](A,I,L'',valueIndex'')&lt;br /&gt;
&lt;br /&gt;
A fact table is a generalization of a simple relational table.  In a fact table, the first ''N'' columns identify cell coordinates, and the last ''K'' columns hold measured values (often called ''measures'', ''facts'', or just ''values'').  This generalized the simple relational table where only the single final row holds a value.&lt;br /&gt;
&lt;br /&gt;
The optional parameter, «valueIndex», specifies an index of «A» that is used for the value columns in the result.  The first [[Size]](L) - [[Size]](valueIndex) columns of «L» are then the coordinates, and the final [[Size]](valueIndex) columns of «L» hold the values.&lt;br /&gt;
&lt;br /&gt;
When omitting cells having no data, all values in «A» along the valueIndex must be zero or null.  If any of the values have data, the corresponding cell (i.e., row in the result) is considered to have data.&lt;br /&gt;
&lt;br /&gt;
== Positional Coordinates ==&lt;br /&gt;
&lt;br /&gt;
''New to [[What's new in Analytica 4.2?|Analytica 4.2]]''&lt;br /&gt;
&lt;br /&gt;
::[[MdArrayToTable]](...,positional:true) &lt;br /&gt;
&lt;br /&gt;
Normally, the first ''N'' columns of the result specify coordinates using index ''labels''.  When the optional parameter «positional» is specified as true, index positions are returned rather than labels.&lt;br /&gt;
&lt;br /&gt;
== Customizing Column Names ==&lt;br /&gt;
&lt;br /&gt;
Normally, the column labels in «L» are just the index identifiers.  However, you may need to use different labels, such as labels to match column names in an external database, or more human-readable names.  To do this, define an index, say ''Column'', with the desired labels.  Then create a [[Table]] indexed by ''Column'', call it ''L'', and fill in the index names in the table.  Pass ''L'' as the third parameter to [[MdArrayToTable]].&lt;br /&gt;
&lt;br /&gt;
= Library =&lt;br /&gt;
&lt;br /&gt;
Array&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
Define these global objects (as indexes or variables):&lt;br /&gt;
 Rows := sequence(1,size(Cost_in_time))&lt;br /&gt;
 Cols := [‘Mpg’,’Time’,’Car_type’,’Cost’]&lt;br /&gt;
&lt;br /&gt;
 MDArrayToTable(Cost_in_time,Rows,Cols) →&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Rows v, Cols &amp;gt;&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
!Mpg !!Time !!Car_type !!Cost&lt;br /&gt;
|-&lt;br /&gt;
!1 &lt;br /&gt;
|26 ||0 ||VW ||2185&lt;br /&gt;
|-&lt;br /&gt;
!2 &lt;br /&gt;
|26 ||0 ||Honda ||2385&lt;br /&gt;
|-&lt;br /&gt;
!3 &lt;br /&gt;
|26 ||0 ||BMW ||3185&lt;br /&gt;
|-&lt;br /&gt;
!4 &lt;br /&gt;
|26 ||1 ||VW ||2294&lt;br /&gt;
|-&lt;br /&gt;
!5 &lt;br /&gt;
|26 ||1 ||Honda ||2314&lt;br /&gt;
|-&lt;br /&gt;
!6 &lt;br /&gt;
|26 ||1 ||BMW ||3294&lt;br /&gt;
|-&lt;br /&gt;
!7 &lt;br /&gt;
|26 ||2 ||VW ||2409&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;5&amp;quot; | ...&lt;br /&gt;
|-&lt;br /&gt;
!45 &lt;br /&gt;
|35 ||4 ||BMW ||5175&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note: The expression for doing the same transformation using local indexes looks like&lt;br /&gt;
 [[Index]] Rows := [[Sequence]](1,[[Size]](Cost_in_time))&lt;br /&gt;
 [[Index]] Cols := [‘Mpg’,’Time’,’Car_type’,’Cost’]&lt;br /&gt;
 [[MdArrayToTable]](Cost_in_time,Rows,Cols)&lt;br /&gt;
&lt;br /&gt;
== Positional example ==&lt;br /&gt;
&lt;br /&gt;
Changing the previous example to:&lt;br /&gt;
 [[MdArrayToTable]](Cost_in_time,Rows,Cols,positional:true)&lt;br /&gt;
causes the coordinates in the first 3 columns to be expressed as index positions, rather than index labels.&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Rows v, Cols &amp;gt;&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
!Mpg !!Time !!Car_type !!Cost&lt;br /&gt;
|-&lt;br /&gt;
!1 &lt;br /&gt;
| 1 ||1 ||1 ||2185&lt;br /&gt;
|-&lt;br /&gt;
!2 &lt;br /&gt;
|1 ||1 ||2 ||2385&lt;br /&gt;
|-&lt;br /&gt;
!3 &lt;br /&gt;
|1 ||1 ||3 ||3185&lt;br /&gt;
|-&lt;br /&gt;
!4 &lt;br /&gt;
|1 ||2 ||1 ||2294&lt;br /&gt;
|-&lt;br /&gt;
!5 &lt;br /&gt;
|1 ||2 ||2 ||2314&lt;br /&gt;
|-&lt;br /&gt;
!6 &lt;br /&gt;
|1 ||2 ||3 ||3294&lt;br /&gt;
|-&lt;br /&gt;
!7 &lt;br /&gt;
|1 ||3 ||1 ||2409&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;5&amp;quot; | ...&lt;br /&gt;
|-&lt;br /&gt;
!45 &lt;br /&gt;
|3 ||5 ||3 ||5175&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Creating a Fact Table ==&lt;br /&gt;
&lt;br /&gt;
In this example, well provide a separate column for each car-type, rather than including that as a relational dimension.  The numbers in the columns are the cost.&lt;br /&gt;
&lt;br /&gt;
 Index Row := 1..[[Sum]](1,Mpg,Time)&lt;br /&gt;
 Index Label := [Mpg,Time,'VW','Honda','BMW'];&lt;br /&gt;
 Variable Cost := [[MdArrayToTable]](Cost_in_time,Row,Label,valueIndex:Car_type)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;0&amp;quot;&lt;br /&gt;
| Cost &amp;amp;rarr;&lt;br /&gt;
|&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
|+ Rows v, Cols &amp;gt;&amp;gt;&lt;br /&gt;
!&lt;br /&gt;
!Mpg !!Time !! VW !! Honda !! BMW&lt;br /&gt;
|-&lt;br /&gt;
!1 &lt;br /&gt;
|26 ||0 ||2185 || 2385 || 3185&lt;br /&gt;
|-&lt;br /&gt;
!2 &lt;br /&gt;
|26 ||1 ||2294 || 2314 ||3294&lt;br /&gt;
|-&lt;br /&gt;
!3&lt;br /&gt;
|26 ||2 ||2409 || 2512 || 3359&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;6&amp;quot; | ...&lt;br /&gt;
|-&lt;br /&gt;
!15 &lt;br /&gt;
|35 ||4 ||3829 || 4230 || 5175&lt;br /&gt;
|}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Tips and Tricks =&lt;br /&gt;
&lt;br /&gt;
== Textual Index Names vs. Handles ==&lt;br /&gt;
&lt;br /&gt;
You can specify indexes for the first ''N'' columns of «L» using textual index names, or using [[handle]]s.  Which is better?&lt;br /&gt;
&lt;br /&gt;
Personally, I like to use [[handle]]s, since this allows Analytica to automatically re-write the definition if I rename one of the indexes later.  If I simply enter a literal textual name, and later change the index identifier, the literal text remains the same.&lt;br /&gt;
&lt;br /&gt;
When defining «L» using [[handle]]s, you should define an index as a List (not a list-of-labels), and then enter the index identifiers into the first ''N'' cells.   For the final cell, you can enter anything, but I often just enter the identifier for the array being transformed.  When using a list-of-identifiers like this, ''it is critical that you set the metaOnly attribute to 1.''  In the attribute panel, click the attribute pulldown and select ''metaOnly'' and enter 1 in the panel.  This prevents the identifiers from being evaluated, ensuring that it is the identifiers themselves ([[handle]]s actually) that comprise the mid value when the index gets passed in as «L».&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
* [[MdTable]]&lt;br /&gt;
* [[ConcatRows]]&lt;/div&gt;</summary>
		<author><name>Mwright</name></author>
	</entry>
</feed>