如何将 SQL 转换为 DAX 并在 Power BI Desktop 中创建计算 table
How to translate SQL to DAX and create calculated table in Power BI Desktop
我有一个包含 3 列的 table: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
结果应如下所示:
要遵循相同的逻辑,请创建两个类似于您的 SQL case 语句的计算列。
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"
我有一个包含 3 列的 table: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
结果应如下所示:
要遵循相同的逻辑,请创建两个类似于您的 SQL case 语句的计算列。
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"