0

我在 T-SQL 中有以下代码,它根据某些条件计算 [C-Services] 表的不同 [ClientID],并在多个字段上加入 [C_Episode] 表:

SELECT 
   cs.[ProgramName] AS [Program]
  ,COUNT(DISTINCT cs.[ClientID]) AS [# Clients Served]

FROM 
  [C_Services] cs
  INNER JOIN [C_Episodes] epi ON (

        (epi.[ProgramID] = cs.[ProgramID])
    AND (epi.[ClientID] = cs.[ClientID])
    AND (epi.[AdmissionDate] <= cs.[ServiceMonth])
    AND (epi.[DischargeDate] >= cs.[ServiceMonth])
                                        )
WHERE
       (cs.[ClientID] > 0)
   AND (cs.[IsNoShow] = 0) 
   AND (cs.[IsCancellation] = 0)
   AND (cs.[IsNonClaimable] = 0)

GROUP BY
     cs.[ProgramName]

此外,我也有与上述代码关联的 DAX Measure,但没有 INNER JOIN [C-Episodes] 表:

VAR countClients =
    CALCULATE (
       DISTINCTCOUNT ( 'C_Services'[ClientID] ),
               FILTER (
                       'C_Services',
                              [ClientID] > 0
                           && [Is Non-Claimable] = FALSE ()
                           && [Is No-show] = FALSE ()
                           && [Is Cancellation] = FALSE ()
                      )
                 )
                 RETURN
                      countClients

我的问题是 - 我必须如何修改 DAX 度量才能添加 INNER JOIN [C_Episodes] 表(在 T-SQL 代码中说明的三个条件下加入)?

我试图为第一个 JOIN ON ProgramID 列添加

NATURALINNERJOIN('C_Services'[ProgramID], 'C_Episodes'[ProgramID])

但它给出了一个错误 - “需要分组”,并且在我尝试将我的计算更改为 Summarize 或 Summarizecolumns 之后 - 它仍然给出了一个错误。

4

1 回答 1

1

SQL 可以这样重写:

SELECT 
   cs.[ProgramName] AS [Program]
  ,COUNT(
       DISTINCT 
   CASE WHEN EXISTS (
       SELECT * 
       FROM [C_Episodes] epi
       WHERE (epi.[ProgramID] = cs.[ProgramID])
       AND (epi.[ClientID] = cs.[ClientID])
       AND (epi.[AdmissionDate] <= cs.[ServiceMonth])
       AND (epi.[DischargeDate] >= cs.[ServiceMonth])
  ) THEN cs.[ClientID] ELSE NULL END
) AS [# Clients Served]

FROM 
  [C_Services] cs
WHERE
       (cs.[ClientID] > 0)
   AND (cs.[IsNoShow] = 0) 
   AND (cs.[IsCancellation] = 0)
   AND (cs.[IsNonClaimable] = 0)

GROUP BY
     cs.[ProgramName]

而这更符合 DAX 以测量为中心的世界观。

要是我们

  • 专注于[# Clients Served]措施
  • 假设 ProgramName group by 将由可视化处理
  • 假设您现有的过滤器可以重复使用

然后您应该能够将其用于表中的计算C_Episodes

ClientMatched = 
IF(
    ISEMPTY(
        FILTER(
        'C_Episodes', 
        'C_Episodes'[ProgramID] = 'C_Services'[ProgramID] &&
        'C_Episodes'[ClientID] = 'C_Services'[ClientID] &&
        'C_Episodes'[AdmissionDate] <= 'C_Services'[ServiceMonth] &&
        'C_Episodes'[DischargeDate] >= 'C_Services'[ServiceMonth]
        )
    ),
    BLANK(),
    [ClientID]
)

如果 C_Episodes 中有匹配的记录,则该列将包含 ClientID,否则将为空。

这也可以使用您的其他过滤器进行扩展。

现在您可以使用此度量:

DistinctClientCount = DISTINCTCOUNTNOBLANK(Services[ClientMatched])

我不确定这有多有效,但试试看

为了完整起见,这些是用于创建我用来测试的表的 DAX 表达式:

C_服务

C_Services = UNION(
    ROW("ProgramId", 1, "ClientId", 1,"ServiceMonth",DATE(2021,2,1)),
    ROW("ProgramId", 1, "ClientId", 1,"ServiceMonth",DATE(2021,3,1)),
    ROW("ProgramId", 1, "ClientId", 1,"ServiceMonth",DATE(2021,4,1)),
    ROW("ProgramId", 1, "ClientId", 2,"ServiceMonth",DATE(2021,2,1)),
    ROW("ProgramId", 1, "ClientId", 2,"ServiceMonth",DATE(2021,3,1)),
    ROW("ProgramId", 1, "ClientId", 2,"ServiceMonth",DATE(2021,4,1)),
    ROW("ProgramId", 2, "ClientId", 1,"ServiceMonth",DATE(2021,2,1)),
    ROW("ProgramId", 2, "ClientId", 1,"ServiceMonth",DATE(2021,3,1)),
    ROW("ProgramId", 2, "ClientId", 1,"ServiceMonth",DATE(2021,4,1))
)

C_剧集

C_Episodes = UNION(
    ROW("ProgramId", 1, "ClientId", 1,"AdmissionDate",DATE(2021,1,15),"DischargeDate",DATE(2021,2,15)),
    ROW("ProgramId", 1, "ClientId", 2,"AdmissionDate",DATE(2022,5,15),"DischargeDate",DATE(2022,6,15)),
    ROW("ProgramId", 4, "ClientId", 1,"AdmissionDate",DATE(2021,2,15),"DischargeDate",DATE(2021,3,15)),
    ROW("ProgramId", 1, "ClientId", 2,"AdmissionDate",DATE(2021,2,15),"DischargeDate",DATE(2021,3,15))    
)
于 2021-07-01T13:07:15.740 回答