在 Power BI Desktop 中新增自訂資料行Add a custom column in Power BI Desktop

您可以使用 Power BI Desktop 中的查詢編輯器,輕鬆將資料的新自訂資料行新增到模型。You can easily add a new custom column of data to your model using Query Editor in Power BI Desktop. 您可以使用簡單的按鈕建立定義自訂資料行的 M 公式,建立自訂資料行並為其重新命名。You can create and rename your custom column using easy buttons to create M formulas that define your custom column. M 公式有完整的函式參考內容集The M formula has a comprehensive function reference content set.

建立自訂資料行是您在查詢編輯器中建立的另一個套用的步驟,表示其可以隨時變更、稍早或稍後移動及修改。Creating a custom column is another Applied Step to the query you create in Query Editor, which means it can be changed, moved earlier or later, or modified at any time.

使用查詢編輯器新增自訂資料行Use Query Editor to add a new custom column

若要建立新的自訂資料行,請啟動查詢編輯器To create a new custom column, launch Query Editor. 您也可以從 Power BI Desktop 的 [常用] 功能區選取 [編輯查詢] 以完成這個動作。You can do this by selecting Edit Queries from the Home ribbon in Power BI Desktop.

查詢編輯器已啟動,而您也載入一些資料後,就可以依序選取功能區的 [新增資料行] 索引標籤和 [自訂資料行],以新增自訂資料行。Once Query Editor is launched and you have some data loaded, you can add a custom column by selecting the Add Column tab on the ribbon, and then selecting Custom Column.

當您這樣做時,[新增自訂資料行] 隨即顯示,我們會在下一節加以討論。When you do so, the Add Custom Column window appears, which we discuss in the following section.

[新增自訂資料行] 視窗The Add Custom Column window

在 [新增自訂資料行] 視窗中,您會在右方窗格中看到可用欄位的清單,您的自訂資料行名稱位於頂端 (只要在文字方塊中鍵入新名稱,即可為其重新命名),以及您依據從右側插入的欄位、新增運算子或建立公式而建立 (或撰寫) 的 M 公式,新的自訂資料行會依此定義。In the Add Custom Column window, you see the list of available fields in the pane on the right, the name of your custom column on the top (you can rename it just by typing a new name in that text box), and the M formula that you create (or write) based on inserting fields from the right, adding operators, and otherwise building the formula on which your new custom column will be defined.

為您的自訂資料行建立公式Create formulas for your custom column

您可以從右側 [可用的資料行:] 清單中選取欄位,然後選取 [<< 插入] 將其新增到自訂資料行公式。You can select a field from the Available columns: list on the right, and select << Insert to add them to the custom column formula. 只要按兩下清單中的資料行,也可以加以新增。You can simply double-click on a column in the list to add it, too.

在您鍵入公式及建立資料行的同時,會在視窗底部看到指標,即時 (在您鍵入時) 讓您知道是否偵測到任何語法錯誤。As you type the formula and build your column, in the bottom of the window you'll see an indicator telling you, in real time (as you type) whether any syntax errors are detected. 如果一切正常,您會看到綠色勾號。If all is good, you'll see a green checkmark.

但如果您的語法中有某種錯誤,會看到黃色的警告圖示以及偵測到的錯誤,還有會在偵測到錯誤的位置放上游標 (在公式中) 的連結。But if you have some sort of error in your syntax, you get a yellow warning icon, along with the error detected, and a link that puts the cursor (in your formula) where the error is detected.

當您選取 [確定] 時,自訂資料行即新增到模型,而 [已新增自訂] 步驟會新增到查詢的 [套用的步驟]。When you select OK, your custom column is added to the model, and the Added Custom step is added to your query's Applied Steps.

如果您在 [套用的步驟] 窗格中按兩下 [已新增自訂] 步驟,[新增自訂資料行] 最會再次顯示,並已載入您建立的自訂資料行公式,準備好讓您在必要時修改。If you double-click the Added Custom step in the Applied Steps pane, the Add Custom Column window appears again, with the custom column formula you created already loaded, and ready for you to modify if necessary.

在自訂資料行使用進階編輯器Using the Advanced Editor for Custom Columns

您也可以使用進階編輯器建立自訂資料行 (也可以修改任何查詢步驟)。You can also create a custom column (and modify any step of your query, for that matter) using the Advanced Editor. 查詢編輯器中選取 [檢視] 索引標籤,然後選取 [進階編輯器] 以顯示進階編輯器In Query Editor select the View tab and then select Advanced Editor to display the Advanced Editor.

進階編輯器可讓您完整控制查詢。The Advanced Editor gives you full control over your query.

後續步驟Next steps

建立自訂資料行還有其他多種方式,包括依據您提供給查詢編輯器的範例建立資料行。There are other ways to create a custom column, including creating a column based on examples you provide to Query Editor. 如需從範例建立自訂資料行的詳細資訊,請參閱下列文章:See the following article for more information on creating custom columns from examples: