Exemplar Adding A Calculated Table and Column
Exemplar Adding A Calculated Table and Column
and column
Overview
In the exercise Adding a calculated table and column, you were asked to create new calculated
tables and columns using DAX within your data model to address specific analytical and
visualization concerns.
Create a calculated table from the existing dataset within your data model.
Add calculated columns to a specific table within the dataset.
Ensure data standardization and consistency.
This reading provides you with a step-by-step guide for completing these tasks. It also includes
screenshots that you can compare against your work.
You can also review the Introduction to calculated tables and Creating calculated columns videos
for guidance on using DAX in Power BI.
3. Once you select and load the data, Power BI opens a Navigator dialog box that lists all the
tables available to load in the Excel file, along with the data preview on the right side of the
Navigator. Select the Sales, Product, Region, Date, and Salesperson tables, then
select Load.
Step 2: Remove all duplicate values and set the
relationships between the tables.
1. To eliminate all duplicate data, access the Power Query editor, right-click on the
SalesOrderNumber columns, and select Remove duplicates from the drop-
down menu.
1. To configure the model relationships, access the Model view of Power BI desktop and
select Manage relationships. From here, you can edit cardinality and cross-filter
direction between the tables.
Step 3: Create a calculated table.
1. Access the Model view in the calculations group to create a new table. Select New
table. Copy and paste the following DAX code into the formula bar:
5
2
3
4
1
"Color", RELATED ( Products[Color]))
ADDCOLUMNS (
Sales,
"Year", RELATED ( 'Date'[Year]),
Yearly Sales by color =
ADDCOLUMNS: Adds calculated columns to the given table or table expression. In this
instance, the Sales table is the main table to which you need to add two more columns,
one from the Date table and one from the Product table.
Year and Color in double quotes are the names of the new columns to be added in the
new calculated table.
RELATED: Returns a related value from another table. In this case, Product color
values from the Product table and Year information from the Date table.
1. Note that the resulting table has 11 columns.
Step 4: Create calculated columns.
1. To create a new column, select the Date table from the Data pane on the right side of
Power BI interface. Access Model view in the Calculations group and select
New column. Copy and paste the following DAX code into the formula bar:
1
Qtr = QUARTER('Date'[Date])
QUARTER: Returns each quarter as a number from the Date column.
Date in single quotes is the table, and Date in square brackets is the column within the
table.
1. Select the Date table from the Data pane on the right side of Power BI interface.
Access Model view in the Calculations group and select New column. Copy
and paste the following DAX code into the formula bar:
1
Month =LEFT ( 'Date'[Month], 3 )
LEFT: Returns the specified number of characters from the start of a text string.
Date in single quotes is the table to be referenced, and Month in square brackets is
the column name. The number 3 specifies the number of characters in the short month
column.
1. To create a new column, select the Product table from the Data pane on the right
side of Power BI interface. Access Model view in the calculations group. Then
select New column to expand the formula bar. Copy and paste the following DAX
code into the formula bar:
1
Product Color = RELATED ( Products[Color] )
RELATED here is the same as referencing a column from another table.
Step 5: Save your Power BI project.
To save the project, open the File menu, select Save As, and provide an appropriate
name for the project along with a path to the folder on your computer.
Conclusion
With these steps, you have successfully created a calculated table by combining data from
multiple datasets and user-defined columns using DAX. You can now analyze Adventure Works
data based on the analytical and business requirements.
Remember that when using DAX formulas, always ensure they are correctly formatted and that
the column names match the actual column names in your data.