0

我有一个有 3 列的表:EffectiveDate, ActualStatus, PredictedStatus.

在此处输入图像描述

我需要分组依据EffectiveDate并计算ActualStatus列和PredictedStatus列的每个状态的数量。

在sql中我会这样做:

 select EffectiveDate,
        ActualStatus,
        SUM(case when ActualStatus = 'Bound' then 1 
                 when ActualStatus = 'Declined' then 1
                 when ActualStatus = 'Quoted' then 1
                 when ActualStatus = 'Not Taken Up' then 1
                 when ActualStatus = 'Indication' then 1
                 when ActualStatus = 'Submitted' then 1
                 when ActualStatus = 'Lost' then 1
                 when ActualStatus = 'Indicated' then 1
        else 0 end) as CountActual,
        PredictedStatus,
        SUM(case when PredictedStatus = 'Bound' then 1
                 when PredictedStatus = 'Not Bound' then 1
                 ELSE NULL end) as CountPredicted

from #Test
group by 
        EffectiveDate,
        ActualStatus,
        PredictedStatus

结果应如下所示:

在此处输入图像描述

4

1 回答 1

1

要遵循相同的逻辑,请创建两个类似于 SQL 案例语句的计算列。

if [ActualStatus] = "Bound" or
   [ActualStatus] = "Declined" or
   [ActualStatus] = "Quoted" or
   [ActualStatus] = "Not Taken Up" or
   [ActualStatus] = "Indication" or
   [ActualStatus] = "Submitted" or
   [ActualStatus] = "Lost" or
   [ActualStatus] = "Indicated"
then 1
else 0

if [PredictedStatus] = "Bound" or
   [PredictedStatus] = "Not Bound"
then 1
else null

然后按前三列分组并对其他列求和,如下所示:

通过...分组

生成的查询在您的高级编辑器中将如下所示:

let
    Test = <Insert Data Source Here>,
    #"Added Custom" = Table.AddColumn(Test, "CountActual", each if [ActualStatus] = "Bound" or [ActualStatus] = "Declined" or [ActualStatus] = "Quoted" or [ActualStatus] = "Not Taken Up" or [ActualStatus] = "Indication" or [ActualStatus] = "Submitted" or [ActualStatus] = "Lost" or [ActualStatus] = "Indicated" then 1 else 0),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "CountPredicted", each if [PredictedStatus] = "Bound" or [PredictedStatus] = "Not Bound" then 1 else null),
    #"Grouped Rows" = Table.Group(#"Added Custom1", {"Day", "ActualStatus", "PredictedStatus"}, {{"CountActual", each List.Sum([CountActual]), type number}, {"CountPredicted", each List.Sum([CountPredicted]), type number}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Grouped Rows",{"Day", "ActualStatus", "CountActual", "PredictedStatus", "CountPredicted"})
in
    #"Reordered Columns"
于 2018-02-01T22:32:47.980 回答