0

我有一张包含三列的表: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

0s 所以对我来说,0 的去向是个谜。

4

1 回答 1

0

我认为你很复杂

Average=
VAR totalCustomers = COUNTROWS(ALL(Query1[customer])) //this gives you total # of customers

RETURN 
  DIVIDE(COUNT(Query1[engagio_activity_id]) + 0, //the +0 forces the count to always return something
         totalCostumers)
于 2020-11-03T12:08:37.897 回答