Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Walter_Mangy
New Member

Help with DAX Measure to Insert Line Breaks Without Splitting Words (Max 14 Chars per Line)

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!

1 ACCEPTED SOLUTION
techies
Super User
Super User

Hi @Walter_Mangy please try this calculated column

 

Formatted Name =
VAR MaxLen = 14
VAR Words = SUBSTITUTE(TRIM(NamesTable[name]), " ", "|")
VAR WordCount = PATHLENGTH(Words)
VAR WordTable =
    ADDCOLUMNS(
        GENERATESERIES(1, WordCount),
        "Word", PATHITEM(Words, [Value]),
        "Length", LEN(PATHITEM(Words, [Value]))
    )

VAR LineBuilder =
    ADDCOLUMNS(
        WordTable,
        "LineNum",
        VAR ThisIndex = [Value]
        VAR RunningLen =
            SUMX(
                FILTER(WordTable, [Value] < ThisIndex),
                [Length] + 1
            ) + [Length]
        RETURN
            QUOTIENT(RunningLen - 1, MaxLen) + 1
    )

VAR ResultTable =
    ADDCOLUMNS(
        SUMMARIZE(LineBuilder, [LineNum]),
        "LineText",
        CONCATENATEX(
            FILTER(LineBuilder, [LineNum] = EARLIER([LineNum])),
            [Word],
            " "
        )
    )

RETURN
    CONCATENATEX(ResultTable, [LineText], UNICHAR(10))
 
techies_0-1753117652968.png

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

View solution in original post

4 REPLIES 4
techies
Super User
Super User

Hi @Walter_Mangy please try this calculated column

 

Formatted Name =
VAR MaxLen = 14
VAR Words = SUBSTITUTE(TRIM(NamesTable[name]), " ", "|")
VAR WordCount = PATHLENGTH(Words)
VAR WordTable =
    ADDCOLUMNS(
        GENERATESERIES(1, WordCount),
        "Word", PATHITEM(Words, [Value]),
        "Length", LEN(PATHITEM(Words, [Value]))
    )

VAR LineBuilder =
    ADDCOLUMNS(
        WordTable,
        "LineNum",
        VAR ThisIndex = [Value]
        VAR RunningLen =
            SUMX(
                FILTER(WordTable, [Value] < ThisIndex),
                [Length] + 1
            ) + [Length]
        RETURN
            QUOTIENT(RunningLen - 1, MaxLen) + 1
    )

VAR ResultTable =
    ADDCOLUMNS(
        SUMMARIZE(LineBuilder, [LineNum]),
        "LineText",
        CONCATENATEX(
            FILTER(LineBuilder, [LineNum] = EARLIER([LineNum])),
            [Word],
            " "
        )
    )

RETURN
    CONCATENATEX(ResultTable, [LineText], UNICHAR(10))
 
techies_0-1753117652968.png

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

Thanks a lot @techies techies, much appreciated!

bhanu_gautam
Super User
Super User

@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], "")
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






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

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.