# 创建计算列Create calculated columns

## 创建计算列Create calculated columns

#### 在 DimDate 表中创建 MonthCalendar 计算列Create a MonthCalendar calculated column in the DimDate table

1. 单击“模型”菜单 >“模型视图” > “视图”。Click the Model menu > Model View > Data View.

只能使用模型设计器在“数据视图”中创建计算列。Calculated columns can only be created by using the model designer in Data View.

2. 在模型设计器中，单击“DimDate”表（选项卡）。In the model designer, click the DimDate table (tab).

3. 右键单击“CalendarQuarter”列标题，并单击“插入列”。Right-click the CalendarQuarter column header, and then click Insert Column.

一个名为“Calculated Column 1”的新列将插入到“Calendar Quarter”列的左侧。A new column named Calculated Column 1 is inserted to the left of the Calendar Quarter column.

4. 在表上方的公式栏中，键入以下 DAX 公式：“自动完成”可帮助你键入列和表的完全限定名称，并将列出可用的函数。In the formula bar above the table, type the following DAX formula: AutoComplete helps you type the fully qualified names of columns and tables, and lists the functions that are available.

``````=RIGHT(" " & FORMAT([MonthNumberOfYear],"#0"), 2) & " - " & [EnglishMonthName]
``````

然后，会在计算列中为所有行填充值。Values are then populated for all the rows in the calculated column. 如果在表中向下滚动，可看到，根据每行中的数据，各行中的此列可能具有不同的值。If you scroll down through the table, you see rows can have different values for this column, based on the data in each row.

5. 将此列重命名为 MonthCalendarRename this column to MonthCalendar.

MonthCalendar 计算列提供月份的可排序名称。The MonthCalendar calculated column provides a sortable name for Month.

#### 在 DimDate 表中创建 DayOfWeek 计算列Create a DayOfWeek calculated column in the DimDate table

1. DimDate 表仍然处于活动状态的情况下，单击“列”菜单，并单击“添加列”。With the DimDate table still active, click the Column menu, and then click Add Column.

2. 在公式栏中，键入以下公式：In the formula bar, type the following formula:

``````=RIGHT(" " & FORMAT([DayNumberOfWeek],"#0"), 2) & " - " & [EnglishDayNameOfWeek]
``````

完成公式构建后，按 ENTER。When you've finished building the formula, press ENTER. 新列将添加到表的最右侧。The new column is added to the far right of the table.

3. 将此列重命名为 DayOfWeekRename the column to DayOfWeek.

4. 单击列标题，并将该列拖动到 EnglishDayNameOfWeek 列和 DayNumberOfMonth 列之间。Click the column heading, and then drag the column between the EnglishDayNameOfWeek column and the DayNumberOfMonth column.

Tip

移动表中的列可使其更容易导航。Moving columns in your table makes it easier to navigate.

DayOfWeek 计算列提供周几的可排序名称。The DayOfWeek calculated column provides a sortable name for the day of week.

#### 在 DimProduct 表中创建 ProductSubcategoryName 计算列Create a ProductSubcategoryName calculated column in the DimProduct table

1. 在“DimProduct”表中，滚动到表的最右侧。In the DimProduct table, scroll to the far right of the table. 请注意，最右侧的列名为“添加列”（斜体），单击列标题。Notice the right-most column is named Add Column (italicized), click the column heading.

2. 在公式栏中，键入以下公式：In the formula bar, type the following formula:

``````=RELATED('DimProductSubcategory'[EnglishProductSubcategoryName])
``````
3. 将该列重命名为“ProductSubcategoryName”。Rename the column to ProductSubcategoryName.

ProductSubcategoryName 计算列用于在 DimProduct 表中创建层次结构，该层次结构将包括 DimProductSubcategory 表的 EnglishProductSubcategoryName 列中的数据。The ProductSubcategoryName calculated column is used to create a hierarchy in the DimProduct table, which includes data from the EnglishProductSubcategoryName column in the DimProductSubcategory table. 层次结构不能跨多个表。Hierarchies cannot span more than one table. 稍后将在第 9 课中创建层次结构。You create hierarchies later in Lesson 9.

#### 在 DimProduct 表中创建 ProductCategoryName 计算列Create a ProductCategoryName calculated column in the DimProduct table

1. DimProduct 表仍然处于活动状态的情况下，单击“列”菜单，并单击“添加列”。With the DimProduct table still active, click the Column menu, and then click Add Column.

2. 在公式栏中，键入以下公式：In the formula bar, type the following formula:

``````=RELATED('DimProductCategory'[EnglishProductCategoryName])
``````
3. 将该列重命名为“ProductCategoryName”。Rename the column to ProductCategoryName.

ProductCategoryName 计算列用于在 DimProduct 表中创建层次结构，该层次结构包括 DimProductCategory 表的 EnglishProductCategoryName 列中的数据。The ProductCategoryName calculated column is used to create a hierarchy in the DimProduct table, which includes data from the EnglishProductCategoryName column in the DimProductCategory table. 层次结构不能跨多个表。Hierarchies cannot span more than one table.

#### 在 FactInternetSales 表中创建 Margin 计算列Create a Margin calculated column in the FactInternetSales table

1. 在模型设计器中，选择 FactInternetSales 表。In the model designer, select the FactInternetSales table.

2. SalesAmount 列与 TaxAmt 列之间创建一个新的计算列。Create a new calculated column between the SalesAmount column and the TaxAmt column.

3. 在公式栏中，键入以下公式：In the formula bar, type the following formula:

``````=[SalesAmount]-[TotalProductCost]
``````
4. 将此列重命名为 MarginRename the column to Margin.

Margin 计算列用来分析每次销售的利润率。The Margin calculated column is used to analyze profit margins for each sale.