Forum Discussion
JuggernautProf
Helper I
3 years agoAdding space between texts using Uppercase as an indicator of where the space should be
Hi PowerBI community, I am keen to understand if it is possible to add space between texts using Uppercase as an indicator of where the space should be: For instance: "IronmanMarvelSuperhero" t...
OwenAuger
Super User
3 years agoThis would probably be best handled further upstream, in data source or Power Query.
However, something like this is possible with DAX.
Here is the code for a calculated column (PBIX attached):
Text With Spaces =
VAR CurrentText = 'Text'[Text]
VAR SpaceCode = UNICODE ( " " )
VAR UpperMin = UNICODE ( "A" )
VAR UpperMax = UNICODE ( "Z" )
VAR NumChars = LEN ( CurrentText )
VAR PositionCode =
SELECTCOLUMNS (
GENERATESERIES ( 1, NumChars ),
"@Position", [Value],
"@Code", UNICODE(MID ( CurrentText, [Value], 1 ))
)
VAR UpperPositionCode =
FILTER (
PositionCode,
VAR IsUpper =
[@Code] >= UpperMin
&& [@Code] <= UpperMax
&& [@Position] > 1
RETURN
IsUpper
)
VAR Spaces =
SELECTCOLUMNS (
UpperPositionCode,
"@Position", [@Position] - 0.5,
"@Code", SpaceCode
)
VAR NewPositionCode =
UNION ( PositionCode, Spaces )
VAR Result =
CONCATENATEX (
NewPositionCode,
UNICHAR ( [@Code] ),
"",
[@Position]
)
RETURN
Result
The basic idea is to find the uppercase characters in all positions after the first, then insert a space before each of them and reassemble.
Regards
- JuggernautProf3 years ago
Helper I
Hey OwenAuger Thanks. I tried this, however I keep getting an error "DAX comparison pperations do not support comparing values of type Text with values of type integer. Consider using VALUE or FORMAT function to conver one of the values.
Also in addition to the above, I keep seeing an error with [@code], [@position], "cannot find name [@code]"