如何仅用DAX实现文本列转Proper Case?不使用Power BI内置函数
Pure DAX Solution for Proper Case (No Built-in Functions)
Got it, since you can't use Power BI's built-in PROPER function and need a pure DAX implementation for Proper Case (capitalizing the first letter of every word, no exceptions like Title Case), here's a reliable approach using a recursive helper function:
Step-by-Step Implementation
First, we'll create a recursive helper function to process each word in the text, then apply it to your column after normalizing all characters to lowercase.
1. Define the Recursive Helper Function
This function will iterate through the text, capitalizing the first letter of each word separated by spaces:
DEFINE FUNCTION ProperCaseHelper(InputText AS STRING) AS STRING RETURN VAR LowercaseText = LOWER(InputText) VAR FirstSpacePosition = FIND(" ", LowercaseText, 1, BLANK()) RETURN IF( ISBLANK(FirstSpacePosition), -- No more spaces: capitalize first letter of the final word UPPER(LEFT(LowercaseText, 1)) & RIGHT(LowercaseText, LEN(LowercaseText) - 1), -- Capitalize first word, then recurse on the remaining text UPPER(LEFT(LowercaseText, 1)) & RIGHT(LEFT(LowercaseText, FirstSpacePosition), FirstSpacePosition - 1) & ProperCaseHelper(RIGHT(LowercaseText, LEN(LowercaseText) - FirstSpacePosition)) ) -- Example usage with your table EVALUATE ADDCOLUMNS( YourTableName, "ProperCase_Result", ProperCaseHelper([text_column]) )
2. Alternative: Calculated Column (No DEFINE Block)
If you prefer a calculated column without defining a separate function, you can embed the logic directly:
ProperCase_Result = VAR OriginalLower = LOWER([text_column]) VAR RecursiveProcess = VAR CurrentText = IF(ISBLANK([@CurrentText]), OriginalLower, [@CurrentText]) VAR SpacePos = FIND(" ", CurrentText, 1, BLANK()) RETURN IF( ISBLANK(SpacePos), UPPER(LEFT(CurrentText, 1)) & RIGHT(CurrentText, LEN(CurrentText) - 1), UPPER(LEFT(CurrentText, 1)) & RIGHT(LEFT(CurrentText, SpacePos), SpacePos - 1) & RecursiveProcess(CurrentText = RIGHT(CurrentText, LEN(CurrentText) - SpacePos)) ) RETURN RecursiveProcess(CurrentText = OriginalLower)
How It Works
- Normalization: First, we convert the entire input text to lowercase to ensure consistent handling of any existing uppercase characters.
- Recursive Word Processing: The function finds the first space in the text, capitalizes the first letter of the preceding word, then repeats the process on the text after the space.
- Final Word Handling: When no more spaces are found, it capitalizes the first letter of the last word and returns the full processed string.
Example Outputs
- Input:
"apple bat cab"→ Output:"Apple Bat Cab" - Input:
"text_column is an example"→ Output:"Text_Column Is An Example" - Input:
"hello world"(multiple spaces) → Output:"Hello World"(preserves original spacing)
Key Notes
- This strictly follows Proper Case rules: every word gets its first letter capitalized, unlike Title Case which skips small words like "an", "is", etc.
- It preserves original spacing (including multiple consecutive spaces).
- For non-space separators (like underscores or hyphens), the text will be treated as a single word (e.g.,
"my-test-string"becomes"My-Test-String"). If you need to handle other separators, modify theFINDfunction to target those characters instead of spaces.
内容的提问来源于stack exchange,提问作者user2543622
相关产品推荐
相关产品推荐

