Power Query 自定义列公式

Power Query Custom Column Formula

我正在尝试汇总通过高级查询引入的数据并添加自定义列。本质上,我想按组计算数据馈送中所有第一个非零月份,并在单独的列中报告该计数。

例如,输入数据如下所示:

Index UNIQUEID PRMS Reserves Category PRMS Reserves Sub Category Date Gross Oil Well Head Volume (MBBL)
36 INPTegrxW4xbF6 probable undeveloped 6/1/2027 0
36 INPTegrxW4xbF6 probable undeveloped 7/1/2027 0
36 INPTegrxW4xbF6 probable undeveloped 8/1/2027 0
36 INPTegrxW4xbF6 probable undeveloped 9/1/2027 14
36 INPTegrxW4xbF6 probable undeveloped 10/1/2027 24
36 INPTegrxW4xbF6 probable undeveloped 11/1/2027 27
37 INPTegrxW4xbF7 probable undeveloped 6/1/2027 0
37 INPTegrxW4xbF7 probable undeveloped 7/1/2027 0
37 INPTegrxW4xbF7 probable undeveloped 8/1/2027 0
37 INPTegrxW4xbF7 probable undeveloped 9/1/2027 14
37 INPTegrxW4xbF7 probable undeveloped 10/1/2027 24
37 INPTegrxW4xbF7 probable undeveloped 11/1/2027 27
38 INPTegrxW4xbF8 probable undeveloped 6/1/2027 0
38 INPTegrxW4xbF8 probable undeveloped 7/1/2027 0
38 INPTegrxW4xbF8 probable undeveloped 8/1/2027 0
38 INPTegrxW4xbF8 probable undeveloped 9/1/2027 14
38 INPTegrxW4xbF8 probable undeveloped 10/1/2027 24
38 INPTegrxW4xbF8 probable undeveloped 11/1/2027 27
39 INPTegrxW4xbF9 proved undeveloped 5/1/2027 0
39 INPTegrxW4xbF9 proved undeveloped 6/1/2027 0
39 INPTegrxW4xbF9 proved undeveloped 7/1/2027 0
39 INPTegrxW4xbF9 proved undeveloped 8/1/2027 14
39 INPTegrxW4xbF9 proved undeveloped 9/1/2027 24
39 INPTegrxW4xbF9 proved undeveloped 10/1/2027 27

我希望最终查询如下所示:

PRMS Reserves Category PRMS Reserves Sub Category Date Sum of Gross Oil Count
probable undeveloped 6/1/2027 0 0
probable undeveloped 7/1/2027 0 0
probable undeveloped 8/1/2027 0 0
probable undeveloped 9/1/2027 42 3
probable undeveloped 10/1/2027 72 0
probable undeveloped 11/1/2027 81 0
proved undeveloped 5/1/2027 0 0
proved undeveloped 6/1/2027 0 0
proved undeveloped 7/1/2027 0 0
proved undeveloped 8/1/2027 14 1
proved undeveloped 9/1/2027 24 0
proved undeveloped 10/1/2027 27 0

有人对如何在 power query 的自定义列中编写函数有建议吗?

您似乎需要计算每个类别组合的最小日期 MinDate,然后检查是否 Date = MinDate

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZGxCoMwFEX/JbNgklqT/EChS+lQ6CAODYYuUkWo+PlN62DNe8vV7SVwznBPVYlDKTJxvlxv4TlM92Lyp+9HP3T+4dsQz/erCWNouz408VXmKtdSm3hKUWcwb3bydifvFl4VWwRKLga9zaD+DGY2mNRgsAgYTyNgPI2A8UwETMBFAA1MBJsaLBYB42kEjKcRMJ6JgAm4CKCBieBSg5sN449Y80cyAULTgAhN8yG0peMjuKPLI/iqXNy9/gA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, UNIQUEID = _t, #"PRMS Reserves Category" = _t, #"PRMS Reserves Sub Category" = _t, Date = _t, #"Gross Oil Well Head Volume (MBBL)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"UNIQUEID", type text}, {"PRMS Reserves Category", type text}, {"PRMS Reserves Sub Category", type text}, {"Date", type date}, {"Gross Oil Well Head Volume (MBBL)", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([#"Gross Oil Well Head Volume (MBBL)"] <> 0)),
    #"Grouped Category" = Table.Group(#"Filtered Rows", {"PRMS Reserves Category", "PRMS Reserves Sub Category"}, {{"MinDate", each List.Min([Date]), type nullable date}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"PRMS Reserves Category", "PRMS Reserves Sub Category"}, #"Grouped Category", {"PRMS Reserves Category", "PRMS Reserves Sub Category"}, "Grouped Rows", JoinKind.LeftOuter),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"MinDate"}, {"MinDate"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows", "Custom", each if [Date] = [MinDate] then 1 else 0, Int64.Type),
    #"Grouped Category and Date" = Table.Group(#"Added Custom", {"PRMS Reserves Category", "PRMS Reserves Sub Category", "Date"}, {{"Gross", each List.Sum([#"Gross Oil Well Head Volume (MBBL)"]), type nullable number}, {"Count", each List.Sum([Custom]), type number}})
in
    #"Grouped Category and Date"

在过滤掉 0 值后,通过在 Date 列上取最小值来计算 #"Grouped Category" 步骤中的 MinDate

然后它将此 MinDate 列合并回原始 table 并使用逻辑 if [Date] = [MinDate] then 1 else 0.

添加自定义列

上面显示的前三列的最后一步分组,总油和自定义。