This script is designed to generate a customized view table in Excel by joining data from a fact table and one or more dimension tables. It allows you to define the output columns, including calculated fields, and add slicers for interactive data exploration.
Follow the instructions below to configure the script according to your data and requirements.
The main configuration object is visualizationConfig
, which you need to customize:
const visualizationConfig: VisualizationConfig = {
outputSheetName: "Your Output Sheet Name",
factTable: { /* Fact Table Configuration */ },
dimensionTables: [ /* Dimension Tables Configuration */ ],
outputColumns: [ /* Output Columns Configuration */ ],
slicers: [ /* Slicers Configuration */ ],
};
Each section of the configuration object is explained in detail below.
Purpose: Specifies the name of the worksheet where the output data will be displayed.
-
Property:
outputSheetName
-
Example:
outputSheetName: "Project Display",
Purpose: Defines the main data table containing the key metrics or facts.
-
Property:
factTable
-
Properties:
sheetName
: The name of the worksheet containing the fact table.keyColumns
: An array of column names to include from the fact table.
-
Example:
factTable: { sheetName: "PROJECT_ITEMS", keyColumns: ["Line Item ID", "Quantity", "Project ID"], },
Purpose: Specifies additional tables that provide context or attributes (dimensions) to the fact data.
-
Property:
dimensionTables
-
Each Dimension Table Configuration Includes:
sheetName
: The name of the worksheet containing the dimension table.joinColumnFact
: The column name in the fact table to join on.joinColumnDim
: The column name in the dimension table to join on.selectColumns
: An array of column names to include from the dimension table.
-
Example:
dimensionTables: [ { sheetName: "PROJECTS", joinColumnFact: "Project ID", joinColumnDim: "Project ID", selectColumns: ["Project ID", "Project Name"], }, { sheetName: "LINE_ITEMS", joinColumnFact: "Line Item ID", joinColumnDim: "Line Item ID", selectColumns: ["Line Item ID", "Line Item", "Description", "Price Act"], }, ],
Purpose: Defines which columns will appear in the output sheet and how they are derived.
-
Property:
outputColumns
-
Each Output Column Configuration Includes:
header
: The column header in the output sheet.source
: Where the data comes from. Options:'fact'
: From the fact table.'dimension'
: From a dimension table.'calculated'
: Computed using a formula.
sheetName
: (Required for'dimension'
) The name of the dimension sheet.columnName
: The name of the column in the source sheet.formula
: (Required for'calculated'
) A function to compute the value.type
: The data type for formatting. Options:'STRING'
,'NUMBER'
,'CURRENCY'
,'DATE'
.
-
Example:
outputColumns: [ { header: "Project Name", source: 'dimension', sheetName: "PROJECTS", columnName: "Project Name", type: "STRING", }, { header: "Quantity", source: 'fact', sheetName: "PROJECT_ITEMS", columnName: "Quantity", type: "NUMBER", }, { header: "Total", source: 'calculated', formula: (row) => { const quantity = Number(row["Quantity"]); const price = Number(row["Price Act"]); return quantity * price; }, type: "CURRENCY", }, ],
Purpose: Adds interactive filters (slicers) to the output sheet for specified columns.
-
Property:
slicers
-
Each Slicer Configuration Includes:
columnName
: The name of the column to which the slicer will apply.
-
Example:
slicers: [ { columnName: "Project Name" }, { columnName: "Line Item" }, ],
Decide on a name for the output sheet and set the outputSheetName
property.
outputSheetName: "Your Output Sheet Name",
Ensure this name does not conflict with existing sheet names unless you intend to overwrite.
Identify your main data table and specify:
sheetName
: The worksheet name containing the fact table.keyColumns
: The columns from the fact table that you want to include.
factTable: {
sheetName: "Your Fact Table Sheet Name",
keyColumns: ["Column1", "Column2", "Column3"],
},
Ensure all specified columns exist in the fact table sheet.
For each dimension table:
- Identify the table and determine how it joins to the fact table.
- Specify:
sheetName
: The worksheet name containing the dimension table.joinColumnFact
: The column in the fact table used for joining.joinColumnDim
: The column in the dimension table used for joining.selectColumns
: The columns from the dimension table to include.
dimensionTables: [
{
sheetName: "Dimension Sheet Name",
joinColumnFact: "Fact Table Join Column",
joinColumnDim: "Dimension Table Join Column",
selectColumns: ["DimColumn1", "DimColumn2"],
},
// Add more dimension tables as needed
],
Ensure the join columns exist in both the fact and dimension tables.
Specify the columns to appear in the output sheet.
For each output column:
header
: The column header in the output sheet.source
:'fact'
,'dimension'
, or'calculated'
.- If
source
is'fact'
or'dimension'
:sheetName
(for'dimension'
): The sheet name.columnName
: The column name in the source sheet.
- If
source
is'calculated'
:formula
: A function that calculates the value.
type
: The data type ('STRING'
,'NUMBER'
,'CURRENCY'
,'DATE'
).
outputColumns: [
// For columns from the dimension table
{
header: "Column Header",
source: 'dimension',
sheetName: "Dimension Sheet Name",
columnName: "Dimension Column Name",
type: "STRING",
},
// For columns from the fact table
{
header: "Column Header",
source: 'fact',
columnName: "Fact Column Name",
type: "NUMBER",
},
// For calculated columns
{
header: "Calculated Column",
source: 'calculated',
formula: (row) => {
// Your calculation logic
},
type: "CURRENCY",
},
],
Ensure that all referenced columns exist and are correctly named.
To add interactive filters:
- Specify the columns you want to use as slicers.
slicers: [
{ columnName: "Column Name for Slicer" },
// Add more slicers as needed
],
Slicers are optional and can enhance data exploration.
- Column Names: Match column names exactly as they appear in your worksheets, including case and spaces.
- Data Types:
- Use
'STRING'
for text. - Use
'NUMBER'
for integers. - Use
'CURRENCY'
for monetary values (formatted with two decimal places). - Use
'DATE'
for date values (formatted asmm/dd/yyyy
).
- Use
- Calculated Columns:
- The
formula
function receives an objectrow
, containing all joined data. - Handle any potential
undefined
ornull
values to prevent errors.
- The
- Error Handling:
- The script will throw an error if a specified column is not found. Double-check all names.
- Testing:
- Start with a simple configuration and gradually add complexity.
- Test the script after each change to ensure it works as expected.
const visualizationConfig: VisualizationConfig = {
outputSheetName: "Project Display",
factTable: {
sheetName: "PROJECT_ITEMS",
keyColumns: ["Line Item ID", "Quantity", "Project ID"],
},
dimensionTables: [
{
sheetName: "PROJECTS",
joinColumnFact: "Project ID",
joinColumnDim: "Project ID",
selectColumns: ["Project ID", "Project Name"],
},
{
sheetName: "LINE_ITEMS",
joinColumnFact: "Line Item ID",
joinColumnDim: "Line Item ID",
selectColumns: ["Line Item ID", "Line Item", "Description", "Price Act"],
},
],
outputColumns: [
{
header: "Project Name",
source: 'dimension',
sheetName: "PROJECTS",
columnName: "Project Name",
type: "STRING",
},
{
header: "Item ID",
source: 'dimension',
sheetName: "LINE_ITEMS",
columnName: "Line Item ID",
type: "STRING",
},
{
header: "Line Item",
source: 'dimension',
sheetName: "LINE_ITEMS",
columnName: "Line Item",
type: "STRING",
},
{
header: "Description",
source: 'dimension',
sheetName: "LINE_ITEMS",
columnName: "Description",
type: "STRING",
},
{
header: "Quantity",
source: 'fact',
columnName: "Quantity",
type: "NUMBER",
},
{
header: "Price",
source: 'dimension',
sheetName: "LINE_ITEMS",
columnName: "Price Act",
type: "CURRENCY",
},
{
header: "Total",
source: 'calculated',
formula: (row) => {
const quantity = Number(row["Quantity"]);
const price = Number(row["Price Act"]);
return quantity * price;
},
type: "CURRENCY",
},
],
slicers: [
{ columnName: "Project Name" },
{ columnName: "Line Item" },
],
};
- Insert Your Configuration: Replace the default
visualizationConfig
in the script with your customized configuration. - Test the Script: Run the script in Excel to generate the view table.
- Verify the Output: Check the output sheet for accuracy.
- Adjust as Needed: If you encounter errors or unexpected results, revisit your configuration.