The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Hi everyone,
I'm working on a DAX measure in Power BI that aims to format a string column ([name]) by inserting a line break (\n or UNICHAR(10)) every 14 characters without breaking words.
Here’s the current version of my measure:
nameindeneb =
VAR RawName = [name]
VAR CleanName = TRIM(RawName)
VAR Words = SUBSTITUTE(CleanName, " ", "|")
VAR WordCount =
IF(
NOT ISBLANK(Words),
PATHLENGTH(Words),
0
)
VAR WordList =
ADDCOLUMNS(
GENERATESERIES(1, WordCount),
"Word", PATHITEM(Words, [Value]),
"Length", LEN(PATHITEM(Words, [Value]))
)
VAR MaxChars = 15
VAR Result =
GENERATE(
WordList,
VAR Index = [Value]
VAR Word = [Word]
VAR PrevWords = FILTER(WordList, [Value] < Index)
VAR RunningLength =
SUMX(PrevWords, [Length] + 1) + [Length]
VAR Break = IF(RunningLength > MaxChars, UNICHAR(10), " ")
RETURN ROW("TextOut", Word & Break)
)
RETURN
IF(
WordCount = 0,
BLANK(),
CONCATENATEX(Result, [TextOut], "")
)
However, this doesn’t produce the desired output. Instead of properly inserting line breaks to avoid going over the 14-character limit, it seems to add breaks inconsistently — often mid-line or too early/late. I suspect the logic around RunningLength and Break isn't tracking line lengths correctly as words accumulate.
Goal:
I want to format the [name] string such that:
Each line has at most 14 characters.
Words are never broken.
Words should wrap to the next line if they don’t fit on the current line.
Example Input:
I am cooking pasta for me and my wife
Expected output:
I am cooking
pasta for me
and my wife
Each line should not exceed 14 characters.
Thanks to anyone who will be able to pick this up!
Solved! Go to Solution.
Hi @Walter_Mangy please try this calculated column
Hi @Walter_Mangy please try this calculated column
@Walter_Mangy , Try using
DAX
nameindeneb =
VAR RawName = [name]
VAR CleanName = TRIM(RawName)
VAR Words = SUBSTITUTE(CleanName, " ", "|")
VAR WordCount =
IF(
NOT ISBLANK(Words),
PATHLENGTH(Words),
0
)
VAR WordList =
ADDCOLUMNS(
GENERATESERIES(1, WordCount),
"Word", PATHITEM(Words, [Value]),
"Length", LEN(PATHITEM(Words, [Value]))
)
VAR MaxChars = 14
VAR Result =
GENERATE(
WordList,
VAR Index = [Value]
VAR Word = [Word]
VAR PrevWords = FILTER(WordList, [Value] < Index)
VAR RunningLength =
SUMX(PrevWords, [Length] + 1) + [Length]
VAR Break = IF(RunningLength + [Length] > MaxChars, UNICHAR(10), " ")
RETURN ROW("TextOut", Word & Break)
)
RETURN
IF(
WordCount = 0,
BLANK(),
CONCATENATEX(Result, [TextOut], "")
)
Proud to be a Super User! |
|
Hi @bhanu_gautam thanks for picking this one up. Unfortunately your query doesn't seem like working. Did you try it on some specific text column? Thanks
User | Count |
---|---|
26 | |
10 | |
8 | |
6 | |
6 |
User | Count |
---|---|
33 | |
14 | |
11 | |
10 | |
8 |