Search code examples
powerbidaxdaxstudio

DAX query for tables not directly related


I've 4 tables, all in relations one to many. Table1(one) to (many)Table2(one) to (many)Table3 etc.

I need to sum data in column value from Table4 for every record in Table1.

Table1 and Table4 are not directly related.

Any ideas?

I'm new to DAX and can't find a solution...


Solution

  • Based on what you provided I assume that you have a chain of relationships :

    Table1 -> Table2 -> Table3 -> Table4

    SumValueForTable1Record = 
    SUMX(
        FILTER(
            Table4,
            COUNTROWS(
                FILTER(
                    RELATEDTABLE(Table2),
                    COUNTROWS(
                        FILTER(
                            RELATEDTABLE(Table3),
                            Table3[Table3ForeignKeyToTable4] = Table4[Table4PrimaryKey]
                        )
                    ) > 0
                )
            ) > 0
        ),
        Table4[value]
    )