Power BI中如何将数字转字符串及映射分类值至对应工位类型
Hey there! Let's walk through these two Power BI tasks step by step—they're both really common, so I'll make sure the instructions are straightforward:
You have two solid options here, depending on whether you want to adjust the source data type or create a new calculated column:
Power Query Editor (Source-Level Conversion)
This is great if you want to permanently change the column's data type at the query level:- Head to the Data view, right-click your table, and select Edit Query (or click Transform data from the Home tab).
- Select the numeric column you want to convert.
- In the Transform tab, click Data Type > choose Text.
- Hit Close & Apply to save your changes.
DAX Calculated Column
Use this if you want to keep the original numeric column intact and add a new string version:
The simplest approach is using theFORMAT()function, which converts the number to a string without extra formatting:String Column = FORMAT(YourTableName[Numeric Column], "0")Alternatively, you can use
CONCATENATE()to force a string conversion:String Column = CONCATENATE(YourTableName[Numeric Column], "")Just replace
YourTableNameandNumeric Columnwith your actual table and column names.
Since you're allowed to create a new column, the SWITCH() function in DAX is the most clean and maintainable solution. Here's how to do it:
DAX Calculated Column with SWITCH()
Create a new calculated column using this formula—it handles each mapping explicitly, plus you can add a fallback for unexpected values:Desk Type = SWITCH( YourTableName[Category Column], 1, "hot desk", 2, "fix desk", 3, "private company", "Unknown" // Optional: use this if there might be values outside 1-3 )Swap
YourTableNameandCategory Columnwith your table and column names. The "Unknown" line is optional but helpful for catching any unplanned category values.Power Query Alternative
If you prefer to adjust the data in Power Query instead:- Open the Power Query Editor (same steps as before).
- Either duplicate your category column (right-click > Duplicate Column) or use the original.
- Select the column, go to the Transform tab > click Replace Values.
- For each mapping: enter the numeric value in Value To Find, the corresponding text in Replace With, then click OK. Repeat for 1→hot desk, 2→fix desk, 3→private company.
- Click Close & Apply to save the changes.
内容的提问来源于stack exchange,提问作者user10986705

