Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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
Check out the July 2025 Power BI update to learn about new features.
User | Count |
---|---|
24 | |
9 | |
7 | |
6 | |
6 |
User | Count |
---|---|
28 | |
11 | |
11 | |
10 | |
6 |