简体   繁体   English

如何在涉及第三个表的条件下连接 Power BI 中的 2 个表

[英]How to join 2 tables in Power BI with condition involving a 3rd table

I have 3 tables, Company, Address and C_detail.我有 3 个表,公司、地址和 C_detail。 I need to count how many company with a certain C_ID from the C_detail table.我需要从 C_detail 表中计算有多少家具有某个 C_ID 的公司。 But the relation existed are Company(CompanyID)=Address(CompanyID), Address(CountryID)=C_detail(CountryID).但是存在的关系是 Company(CompanyID)=Address(CompanyID), Address(CountryID)=C_detail(CountryID)。 To be exact I need to translate this sql into Power BI.确切地说,我需要将此 sql 转换为 Power BI。

SELECT COUNT (*)
FROM Company a
JOIN Address b ON a.CompanyID = b.CompanyID
WHERE b.CountryID IN ( SELECT CountryID from C_detail WHERE C_ID = '1')

How I join the tables with applying condition from different table?我如何使用来自不同表的应用条件连接表? Thanks and note that I am actually quite new with DAX so really appreciate a very clear explanation on this.感谢并注意,我实际上对 DAX 很陌生,因此非常感谢对此的非常明确的解释。

The best way is to create the relationships between those 3 tables in powerBI (set the filter to both directions):最好的方法是在 powerBI 中创建这 3 个表之间的关系(将过滤器设置为双向):

Company to Address on CompanyID
Address to C_detail on CountryID

Then you can create a simple measure COUNT(Company[CompanyID]) and use a slicer with the values of C_ID然后您可以创建一个简单的度量COUNT(Company[CompanyID])并使用带有C_ID值的切片器

If you don't want to or can't create the relationships, you can probably achieve a similar result with the following measure:如果您不想或无法创建关系,您可能可以通过以下措施获得类似的结果:

CompanyCount= 
VAR tbl =
    SELECTCOLUMNS (
        FILTER (
            SUMMARIZE (
                'Address',
                'Address'[CompanyID],
                "valid",
                    CALCULATE (
                        COUNTROWS ( 'Address' ),
                        TREATAS ( VALUES ( 'Detail'[CountryID] ), 'Address'[CountryID] )
                    )
            ),
            [valid] > 0
        ),
        "CompanyID", 'Address'[CompanyID]
    )
RETURN
    CALCULATE ( COUNTROWS ( 'Company' ), TREATAS ( tbl, 'Company'[CompanyID] ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM