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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Connor888
Frequent Visitor

Help with a custom function to calculate Z-score

Hi,

 

I'm trying to use a custom function to add a column which contains a Z-score for another column to a table, something I have to do fairly often. I'm not all that experienced with custom functions however, so I'm going wrong. My code is as follows:

 

 

let ZScore = (datatable as table, column as list) =>
    let 
        average = List.Average(column),
        sdev = List.StandardDeviation(column)
    in    
        Table.AddColumn(datatable, "Z-Score", each Number.Abs(average - column)/sdev)
in
    ZScore

 

 

I'm fairly sure I can see that the error is in my Table.AddColumn, where I refer to the column - ordinarily I could use [column], but as in this case my column variable is a list I get an error.

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Hi @Connor888 

 

see the attached file with a solution.

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @Connor888 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

 

Table:

c1.png

 

Query1(custom function):

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

 

Here are the codes for 'Table' query in 'Advanced editor'.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyVIrViVYyAjFNwUxjENMczDQBMQ2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Data", Int64.Type}}),
    #"Custom" = Query1(#"Changed Type",Table.Column(#"Changed Type","Data"))
in
    Custom

 

Result:

c2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Mariusz
Community Champion
Community Champion

Hi @Connor888 

 

Can you provide a data sample ( past a table from excel into the body of this post ) and expected outcome?

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi @Mariusz , 

 

Given this input table:

IndexData
121
225
327
420

 

I'd like the result to be:

 

IndexDataZscore
1210.786334
2250.611593
3271.310556
4201.135815

 

For each line Zscore = ABS(average of the data column - that line of the data column)/standard deviation of the data column

Hi @Connor888 

 

see the attached file with a solution.

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors