Display rows where foreign key is present in PowerBI

拈花ヽ惹草 提交于 2019-12-24 10:46:40

问题


I have two tables. The first table is called Capability:

The second table is called Information Group:

Some ID's in the Capability table have been referenced in the Information Group table (i.e. in the Capability column)

At present, the query that powers the dashboard for the Capability view is:

= Source{[Schema="dbo",Item="Capability"]}[Data]

I want to show only rows in the Capability table where there is at least one reference in the Information Group table.

In other words, since Capability ID 10 is not referenced in Information Group, the Capability table should exclude that row in the output (i.e. shows only 1 to 9).

|------------------------------------------------------------------|

Effort made so far:

I have managed to select distinct Capability ID's in the Information Group table via this query: = Table.SelectColumns(Table.Distinct(Source{[Schema="dbo",Item="Information_Group"]}[Data], "Capability"), "Capability"), which produces this:

The next step that I would like to do is to check the ID column of the Capability table against the list above and display only rows where the values are contained in the table. Something like this:

= Table.SelectRows(Table.Contains(Source{[Schema="dbo",Item="Capability"]}[Data], each[ID] is contained in the list))

I have read up MSDN on using Table.Contains, but I cannot bridge the gap to get to where I want.

Please let me know if clarification is needed. Thank you.

[Edit] Answer that I am satisfied with:

let
    Source = Sql.Database("server\database", "Mclaren"),
    dbo_Capability = Table.NestedJoin(
        Source{[Schema="dbo",Item="Capability"]}[Data],{"ID"},
        Source{[Schema="dbo",Item="Information_Group"]}[Data],{"Capability"},        
        "NewColumn",
        JoinKind.RightOuter
    ),
    #"Removed Columns" = Table.RemoveColumns(dbo_Capability,{"NewColumn"})
in
    #"Removed Columns"

回答1:


You simply have to perform a merge of your "Capability"-table on "ID" with the "Information Group"-table on "Capability" with JoinKind.Inner: http://radacad.com/how-to-change-joining-types-in-power-bi-and-power-query This acts as a filter.




回答2:


A way to achieve so in DAX is to create a table using FILTER and CONTAINS:

Filtered Capability = 
FILTER(
    Capability,
    CONTAINS(
        'Information Group',
        'Information Group'[Capability],
        Capability[ID]
    )
)

Or with a even simpler syntax using IN which is introduced since Nov 2016:

Filtered Capability = 
FILTER(
    Capability,
    Capability[ID] IN VALUES('Information Group'[Capability])
)

Result will be as follows:



来源:https://stackoverflow.com/questions/42498084/display-rows-where-foreign-key-is-present-in-powerbi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!