Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Power BI, DAX--How do I count rows in one table based on values in another table?

Tags:

count

powerbi

dax

I have two tables, lets call them Table1 and Table2. Table1 has a column of unique values, Table2 has a column with the same values but repeated.

What I am trying to accomplish is to calculate the number of times that value appears in Table2 as a new column in Table1.

like image 236
Bad_Mama_Jama Avatar asked May 25 '17 18:05

Bad_Mama_Jama


Video Answer


1 Answers

If the tables are related, this is very simple:

Number of Table2 rows = COUNTROWS(RELATEDTABLE(Table2))

Here is an example:
Relationship between Table1 and Table2

Your Table2 contains multiple rows per Table1 key:
Table2

Then you can add a Calculated Column to Table1 which counts the times each item appears in Table2:
Table1 with the calculated column

If the tables are not related, you can use CALCULATE and FILTER:

Number of Table2 rows =
CALCULATE(
    COUNTROWS(Table2),
    FILTER(
        Table2,
        Table2[Column1] = Table1[Column1]
    )
)
like image 152
David Avatar answered Sep 29 '22 06:09

David