如何在Excel PowerQuery中计算组内日期与均值的偏差,但排除当前个体?

huangapple go评论67阅读模式
英文:

How can I calculate deviation from a mean within group for a date excluding the present individual in Excel PowerQuery?

问题

fx*=Table.AddColumn(#"Prior Step Name", "组平均值偏差百分比", 
each [Value]/List.Average(->这里应该是我失败的地方<-))
英文:

I have the following table and want to calculate the percent deviation from average of a value on a given date within the remains of the corresponding group using PowerQuery and add as a new column.

IndividualID Group Date Value
A1 1 date1 0.5
A2 1 date1 0.6
A3 1 date1 0.5
A4 2 date1 0.4
A5 2 date1 0.3
A6 2 date1 0.6
A7 2 date1 0.5

I have tried filtering by nested Table.SelectRows() and Table.SelectColumns() within Filter.Average(), but always ended up in typecasting errors like table cannot be casted to list or null.

I hope that someone can point me in the right direction from where I am now.

fx*=Table.AddColumn(#"Prior Step Name", "deviation of value from group average", 
each [Value]/List.Average(->I think here is where I failed<-))

Row 1 as an example should include the following

    Filter 1: IndividualID != A1
    Filter 2: Group == 1
    Filter 3: Date == date1
    -->Calculate Average of [Value] for rows matching all Filters above (-> Individuals A2 and A3)
    -->Calculate percent difference of Value for A1 from the calculated average (of A2 and A3)

Hope this is precise enough for some input. And thanks in advance!

What I would expect:

IndividualID Group Date Value Deviation from group %
A1 1 date1 0.5 -9

The -9 results from
(Value A1/average(Value A2 + A3) -1) * 100

I would like to calculate this for each indiviual on any given date within the range of the corresponding group.
This is to check how much my Value for an individual differs from the rest of the corresponding experimental group on the given date.

答案1

得分: 1

以下是代码的翻译部分:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IndividualID", type text}, {"Group", Int64.Type}, {"Date", type text}, {"Value", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Group", "Date"}, {{"All", each _, type table [IndividualID=nullable text, Group=nullable number, Date=nullable text, Value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", (x)=> Table.AddColumn(x[All], "Deviation from group %",(y)=>
        let a = y[Value],
        b = (List.Sum( x[All][Value]) - y[Value])/ (List.Count(x[All][Value])-1),
        c = (1- (a/b) )* 100
        in c
        )),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Group", "Date", "All"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"IndividualID", "Group", "Date", "Value", "Deviation from group %"}, {"Custom.IndividualID", "Custom.Group", "Custom.Date", "Custom.Value", "Custom.Deviation from group %"})
in
    #"Expanded Custom"
英文:

Here you go.

如何在Excel PowerQuery中计算组内日期与均值的偏差,但排除当前个体?

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IndividualID", type text}, {"Group", Int64.Type}, {"Date", type text}, {"Value", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Group", "Date"}, {{"All", each _, type table [IndividualID=nullable text, Group=nullable number, Date=nullable text, Value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", (x)=> Table.AddColumn(x[All], "Deviation from group %",(y)=> 
        let a = y[Value],
        b = (List.Sum( x[All][Value]) - y[Value])/ (List.Count(x[All][Value])-1),
        c = (1- (a/b) )* 100
        in c
        )),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Group", "Date", "All"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"IndividualID", "Group", "Date", "Value", "Deviation from group %"}, {"Custom.IndividualID", "Custom.Group", "Custom.Date", "Custom.Value", "Custom.Deviation from group %"})
in
    #"Expanded Custom"

答案2

得分: 1

以下是代码的中文翻译:

你也可以这样做:

    let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Group", "Date"}, {{"sum", each List.Sum([Value]), type nullable number}, {"count", each Table.RowCount(_), Int64.Type}, {"data", each _, type table }}),
    #"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"IndividualID", "Value"}, {"IndividualID", "Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded data", "Variance", try each ([Value]/(([sum]-[Value])/([count]-1))-1)*100 otherwise null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"sum","count"})
    in #"Removed Columns"
英文:

You could also do

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Group", "Date"}, {{"sum", each List.Sum([Value]), type nullable number}, {"count", each Table.RowCount(_), Int64.Type}, {"data", each _, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"IndividualID", "Value"}, {"IndividualID", "Value"}),
#"Added Custom" = Table.AddColumn(#"Expanded data", "Variance", try each ([Value]/(([sum]-[Value])/([count]-1))-1)*100 otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"sum","count"})
in #"Removed Columns"

huangapple
  • 本文由 发表于 2023年2月24日 16:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定