计算 parent table 中符合条件的行数,并且在 child table 中至少有一个符合条件的记录

Count rows that match criteria in the parent table, and have at least one qualifying record in a child table

我有 2 个 table:

同意

ConsentID Consent Date Inspection Required
1 1 Aug 2021 Yes
2 10 Sept 2021 No

同意审核

ConsentID Review Date
1 10 Aug 2021
1 15 Aug 2021
1 2 Sept 2021
2 5 Sept 2021

我想创建一个 DAX 度量来计算同意 Inspection Required = Yes and ( Consent Date 介于 [Period Start] 和 [Period End] 措施之间或 Consent 在同一时期内有 Consent Review 记录)。注意方括号的使用 - 我有一些条件必须在 parent table 中匹配,而其他条件在 either 中匹配47=] table child table.

中的匹配项

看看我的示例,如果时间段等于八月或九月,我们将获得 1 份符合条件的同意书。如果是 10 月,我们将得到零。

如果你有以下两个表格

|                   Consents                     |
|-----------|--------------|---------------------|
| ConsentID | Consent Date | Inspection Required |
|-----------|--------------|---------------------|
| 1         | 2021-08-01   | Yes                 |
| 2         | 2021-09-10   | No                  |
| 3         | 2021-10-01   | Yes                 |
| 4         | 2021-12-01   | No                  |
| 5         | 2021-04-05   | Yes                 |
| 8         | 2021-08-01   | Yes                 |

|         Consent Reviews       |
|-----------------|-------------|
| ConsentID       | Review Date |
|-----------------|-------------|
| 1               | 2021-08-10  |
| 1               | 2021-08-15  |
| 1               | 2021-09-02  |
| 2               | 2021-09-05  |
| 4               | 2021-08-22  |
| 4               | 2021-08-23  |
| 5               | 2021-04-05  |
| 5               | 2021-08-31  |

并且您有两个度量 periodStartperiodEnd

以下措施 returns 计入 periodStart=2021-08-01periodEnd=2021-09-30

_count = 
CALCULATE (
    DISTINCTCOUNT ( 'Consent Reviews'[ConsentID] ),
    CALCULATETABLE (
        'Consent Reviews',
        CALCULATETABLE (
            VALUES ( Consents[ConsentID] ),
            Consents[Inspection Required] = "Yes"
        )
    ),
    FILTER (
        'Consent Reviews',
        'Consent Reviews'[Review Date] >= [periodStart]
            && 'Consent Reviews'[Review Date] <= [periodEnd]
    )
)
    + COUNTROWS (
        EXCEPT (
            SUMMARIZE (
                FILTER (
                    'Consents',
                    Consents[Inspection Required] = "Yes"
                        && ( Consents[Consent Date] >= [periodStart]
                        && Consents[Consent Date] <= [periodEnd] )
                ),
                Consents[ConsentID]
            ),
            SUMMARIZE (
                CALCULATETABLE (
                    'Consent Reviews',
                    CALCULATETABLE (
                        'Consent Reviews',
                        CALCULATETABLE (
                            VALUES ( Consents[ConsentID] ),
                            Consents[Inspection Required] = "Yes"
                        )
                    ),
                    FILTER (
                        'Consent Reviews',
                        'Consent Reviews'[Review Date] >= [periodStart]
                            && 'Consent Reviews'[Review Date] <= [periodEnd]
                    )
                ),
                'Consent Reviews'[ConsentID]
            )
        )
    )