我有一张包含三列的表:lisa、customers 和 activity_type。我想按客户和活动类型计算行数,然后按活动类型对所有客户进行平均。如果这是 sql 中的表,我会这样做
SELECT
lisa,
customer,
activity_type,
average(ct)
FROM
(
SELECT
lisa,
customer,
activity_type,
CASE
WHEN
s.ct IS NULL
THEN
0
ELSE
s.ct
END
ct
FROM
(
SELECT
*
FROM
(
SELECT DISTINCT
lisa,
customer
FROM
TABLE
)
,
(
SELECT DISTINCT
activity_type
)
)
LEFT JOIN
(
SELECT
lisa,
customer,
activity_type,
COUNT(*) ct
FROM
TABLE
GROUP BY
1,
2,
3
)
s
)
s
但它是 Dax,它的难度无限大。我试过了:
=
AVERAGEX(
ADDCOLUMNS(
CROSSJOIN( VALUES( Query1[customer] ), VALUES( Query1[activity_type] ) ),
"C", CALCULATE( COUNTA( Query1[engagio_activity_id] ) + 0 )
),
IF( [C] = BLANK(), 0, [C] )
)
和
=
AVERAGEX(
ADDCOLUMNS(
SUMMARIZE( Query1[lisa], Query1[activity_type] ),
"C", CALCULATE( COUNTA( Query1[engagio_activity_id] ) + 0 )
),
IF( [C] = BLANK(), 0, [C] )
)
但尽我所能,我仍然得到: 在汇总行中,空白不被视为 0,例如上图中的“否”行。在计算平均值时,该汇总金额会忽略空白。当我将交叉连接放入 dax 工作室时,我强制使用 0