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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

How to get a simple column of text length as wordcount

Hi!

I am trying to add a column to my data to give a wordcount of the text string on each row.

 

Eg:

TextWordcount
Brief comment2
Very long rambling paragraphs of text6

 

I currently do this in excel with the formula '=LEN(C2)-LEN(SUBSTITUTE(C2," ",""))+1' [text string being in C2] but ideally want to make this part of the data transformation to avoid having to constantly mess with editing in multiple places.

 

My online searching for an existing answer is turning up all sorts of complicated code analysing frequencies of unique words etc, but all I need is a simple count per row.

2 ACCEPTED SOLUTIONS
adudani
Super User
Super User

hi @Anonymous ,

 

create a blank query in the power query editor

copy and paste the below code into the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcirKTE1TSM7PzU3NK1HSUTJSitWJVgpLLapUyMnPS1coSsxNyskEMgoSixLTixILMooV8tMUSlIrQKrNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t, Wordcount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}, {"Wordcount", Int64.Type}}),
    Textwithspace = Table.AddColumn(#"Changed Type", "Textwithspace", each Text.Length([Text]), Int64.Type),
    Textwithoutspace = Table.ReplaceValue(Table.AddColumn(Textwithspace, "Textwithoutspace", each [Text])," ","",Replacer.ReplaceText,{"Textwithoutspace"}),
    Lengthoftextwithoutspace = Table.TransformColumns(Textwithoutspace,{{"Textwithoutspace", Text.Length, Int64.Type}}),
    #"Added Custom" = Table.AddColumn(Lengthoftextwithoutspace, "WORDCOUNT.1", each ([Textwithspace]-[Textwithoutspace])+1),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Text", "WORDCOUNT.1"})
in
    #"Removed Other Columns"

 

Steps taken

1. Extracted the length of the [text] column

2. created a new column = text. Replaced  " " with  "". Extracted the text length.

3. Subtracted the step1 and step2 output plus 1 in "Wordcount.1"

4. removed all other rows.

 

This can be done in 2 steps IMO. Have showed all steps for understanding.

 

adudani_0-1677938976864.png

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

ppm1
Solution Sage
Solution Sage

This is easier to do in the query editor with a custom column with the expression below.

 

= List.Count(Text.Split([TextColumn], " "))

 

Pat

Microsoft Employee

View solution in original post

2 REPLIES 2
ppm1
Solution Sage
Solution Sage

This is easier to do in the query editor with a custom column with the expression below.

 

= List.Count(Text.Split([TextColumn], " "))

 

Pat

Microsoft Employee
adudani
Super User
Super User

hi @Anonymous ,

 

create a blank query in the power query editor

copy and paste the below code into the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcirKTE1TSM7PzU3NK1HSUTJSitWJVgpLLapUyMnPS1coSsxNyskEMgoSixLTixILMooV8tMUSlIrQKrNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t, Wordcount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}, {"Wordcount", Int64.Type}}),
    Textwithspace = Table.AddColumn(#"Changed Type", "Textwithspace", each Text.Length([Text]), Int64.Type),
    Textwithoutspace = Table.ReplaceValue(Table.AddColumn(Textwithspace, "Textwithoutspace", each [Text])," ","",Replacer.ReplaceText,{"Textwithoutspace"}),
    Lengthoftextwithoutspace = Table.TransformColumns(Textwithoutspace,{{"Textwithoutspace", Text.Length, Int64.Type}}),
    #"Added Custom" = Table.AddColumn(Lengthoftextwithoutspace, "WORDCOUNT.1", each ([Textwithspace]-[Textwithoutspace])+1),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Text", "WORDCOUNT.1"})
in
    #"Removed Other Columns"

 

Steps taken

1. Extracted the length of the [text] column

2. created a new column = text. Replaced  " " with  "". Extracted the text length.

3. Subtracted the step1 and step2 output plus 1 in "Wordcount.1"

4. removed all other rows.

 

This can be done in 2 steps IMO. Have showed all steps for understanding.

 

adudani_0-1677938976864.png

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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