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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Rdata
Advocate I
Advocate I

Table.RowCount all rows in current table for use in a custom column equation

I want a custom column where if a row in another column is blank, then return blank, but if it has ANY value, then return (1/ Total # of Rows in current Table)

 

I am trying this. 

 

= Table.AddColumn(#"Changed Type1", "Custom", each if [Merged_Use] <> null then (1 / ( Table.RowCount(Leadership_Usage_Tracking[TRT_ID]) as number)) else "")

 

The rows that are supposed to remain null are working fine. Anywhere that I am expecting (1/ Total # of rows), I am getting the error "A cyclic reference was encountered during evaluation."

 

Am I using the wrong expression to count the number of rows in this table? I also tried just [TRT_ID] (instead of prefacing the table name), but then the error is "cannot convert value to type Table", which makes me think I am using the wrong expression. 

 

 

EDIT: 

 

I went and tried adding an Index column and then calling the max number using List.Max, but it is BOGGING down PowerBI something bad.... 

 

= Table.AddColumn(#"Added Index", "Custom", each if [Merged_Use] <> null then (1/List.Max(#"Added Index"[Index])) else "")

1 ACCEPTED SOLUTION
tackytechtom
Super User
Super User

Hi @Rdata,

 

Cyclic references are a bit tricky sometimes. One way to solve your issue is to save the number of rows in a step before and then reference that variable:

 

Before:

tomfox_0-1663270009541.png

 

After:

tomfox_1-1663270029497.png

 

And here the code in M:

tomfox_2-1663270080525.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWKsisqkoEs9CIksTk7MqS1OQMpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Column"}),
    #"countrows" = Table.RowCount(#"Replaced Value"),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Custom", each if [Column] <> null then 1 / #"countrows"
 else null)
in
    #"Added Custom"

 

Let me know if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

2 REPLIES 2
tackytechtom
Super User
Super User

Hi @Rdata,

 

Cyclic references are a bit tricky sometimes. One way to solve your issue is to save the number of rows in a step before and then reference that variable:

 

Before:

tomfox_0-1663270009541.png

 

After:

tomfox_1-1663270029497.png

 

And here the code in M:

tomfox_2-1663270080525.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWKsisqkoEs9CIksTk7MqS1OQMpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Column"}),
    #"countrows" = Table.RowCount(#"Replaced Value"),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Custom", each if [Column] <> null then 1 / #"countrows"
 else null)
in
    #"Added Custom"

 

Let me know if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

This makes sense, thank you! 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.