如何在 Excel PowerQuery 中计算不包括当前个体的日期与组内平均值的偏差?

问题描述 投票:0回答:1

我有下表,想使用 PowerQuery 计算给定日期的值与相应组剩余部分的平均值的百分比偏差,并添加为新列。

个人ID 团体 日期 价值
A1 1 约会1 0.5
A2 1 约会1 0.6
A3 1 约会1 0.5
A4 2 约会1 0.4
A5 2 约会1 0.3
A6 2 约会1 0.6
A7 2 约会1 0.5

我尝试过在

Table.SelectRows()
中通过嵌套的
Table.SelectColumns()
Filter.Average()
进行过滤,但总是以类型转换错误告终,例如表格无法转换为列表或
null
.

我希望有人能从我现在所在的位置为我指出正确的方向。

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

以第1行为例,应包括以下内容

    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)

希望这对于某些输入来说足够精确。提前致谢!

我的期望:

个人ID 团体 日期 价值 组别偏差%
A1 1 约会1 0.5 -9

-9 结果来自 (值 A1/平均值(值 A2 + A3)-1)* 100

我想在相应组的范围内的任何给定日期为每个人计算这个。 这是为了检查在给定日期我对个人的价值与相应实验组的其他人有多少不同。

excel dax powerquery m
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"
© www.soinside.com 2019 - 2024. All rights reserved.