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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Syndicate_Admin
Administrator
Administrator

Excel Power Query custom function zscore

Using simple table

MeasurementValue
132.5
230.4
331.6

 

I want to add a new column called ZSCORE which would be ( [Measurement] - average([Value]) ) / standard_deviation([Value])

 

I created a new query (custom function) called fnZScore

 

let ZScore = (column as list) =>
     let
     average = List.Average(column),
     sdev = List.StandardDeviation(column)
in
     Table.AddColumn( "Z-Score", each (average - [Value])/sdev)
in
ZScore

 

I then tried to add an Invoke Custom Function Step

 

 

However, this returns the following error:   Expression.Error: A cyclic reference was encountered during evaluation.

 

Any suggestions on how to alter this custom function?

 

 

 

 

6 REPLIES 6
wdx223_Daniel
Super User
Super User

(tbl as table,columnName as text)=>

let
average = List.Average(Table.Column(tbl,column)),
sdev = List.StandardDeviation(Table.Column(tbl,column))
in
Table.AddColumn( tbl,"Z-Score", each (average - Record.Field(_,column))/sdev)

I took the table reference out so I could just use the Invoke Custom column step in my query

 

(columnName as text)=>

let
average = List.Average(Table.Column(columnName)),
sdev = List.StandardDeviation(Table.Column(columnName))
in
Table.AddColumn("Z-Score", each (average - Record.Field(_,columnName))/sdev)

 

This allowed me to proceed as shown below

 

jhenke6229_1-1667970698864.png

 

However when I go to run this, I get this error, where 52.8 is the first record (value) in the table.

 

Expression.Error: We cannot convert the value 52.8 to type Text.
Details:
Value=52.8
Type=[Type]

 

This certainly works when invoke this function by itself so thanks again for that.

 

However when I try to invoke this function as a step in the query, the current table where I am trying to use it does not show up in the dropdown.  So, I am looking for the syntax on how to make the function accept the current table.

 

Thanks again for your initial reply!

 

jhenke6229_0-1667968550627.png

 

try to input the name of preivous step directly

Here is my function with a hard coded reference to the last step in the query where I want to run this (that step is called main)

 

(columnName as text)=>

let
average = List.Average(Table.Column(columnName)),
sdev = List.StandardDeviation(Table.Column(columnName))
in
Table.AddColumn("main", "Z-Score", each (average - Record.Field(_,columnName))/sdev)

// main is the last step in the query I want to call this from

 

 

Then:  Add Column -> Invoke Custom Function

jhenke6229_0-1667975918065.png

This is what is returned (sorry I have to obfuscate a few sensitive parts)

jhenke6229_1-1667976210541.png

 

When I click the error this is the message:

jhenke6229_2-1667976277696.png

 

I don't know what to make of it trying to convert that number (the first number in the list) to text.  I have tried a few variations, playing around with coercing (or casting) parts of the function to number, but it always comes back to this.


Thanks again for your help.

 

Oh my gosh!  You did it!  Thank you, thank you, thank you.  I spent the better part of two days searching through so many different examples for custom functions and z-score in particular.  This is the first one that worked!  I appreciate your taking the time to teach me the subtle things I needed to change

Helpful resources

Announcements
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.

June 2025 community update carousel

Fabric Community Update - June 2025

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