官术网_书友最值得收藏!

Conditional Columns

Using the Power Query Conditional Columns functionality is a great way to add new columns to your query that follow logical if/then/else statements. This concept of if/then/else is common across many programming languages, including Excel formulas. Let's review a real-world scenario where you would be required to do some data cleansing on a file before it can be used. In this example, you will be provided a file of all the counties in the United States, and you must create a new column that extracts the state name from the county column and places it in its own column:

  1. Start by connecting to the FIPS_CountyName.txt file that is found in the book files using the Text/CSV connector.
  2. Launch the Power Query Editor, and start by changing the data type of Column1 to Text. When you do this, you will be prompted to replace an existing type conversion. You can accept this by clicking Replace current.
  3. Now, on Column2, filter out United States from the field to remove this value from the column.
  4. Remove the state abbreviation from Column2 by right-clicking on the column header and selecting Split Column | By Delimiter. Choose -- Custom -- for the delimiter type, and type ,, then click OK:

  1. Next, rename the column names Column1, Column2.1, and Column 2.2, to County Code, County Name, and State Abbreviation, respectively.
  2. To isolate the full state name into its own column, you will need to implement a Conditional Column. Go to the Add Column button in the ribbon and select Conditional Column.
  3. Change the New column name property to State Name and implement the logic If State Abbreviation equals null Then return County Name Else return null as shown in the following screenshot.  To return the value from another column, you must select the icon below the text Output, then choose Select a column. Once this is complete, click OK:

This results in a new column called State Name, which has the fully spelled-out state name only appearing on rows where the State Abbreviation is null

This is only setting the stage to fully scrub this dataset. To complete the data cleansing process for this file, read on to the next section. However, for the purposes of this example, you have now learned how to leverage the capabilities of the Conditional Column transform in the Power Query Editor.

主站蜘蛛池模板: 丹东市| 通州区| 阜平县| 青海省| 德令哈市| 石柱| 清镇市| 黎平县| 隆昌县| 通辽市| 牟定县| 湘潭市| 阿鲁科尔沁旗| 灵山县| 绥滨县| 甘肃省| 永泰县| 遂宁市| 旬阳县| 肥东县| 德钦县| 本溪| 泰州市| 贡觉县| 南宫市| 普兰店市| 改则县| 涟水县| 宜都市| 玛纳斯县| 宁安市| 荥阳市| 怀化市| 安仁县| 大竹县| 驻马店市| 浠水县| 浦东新区| 布尔津县| 龙里县| 泗阳县|