Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Create custom columns by counting specific rows in different table based on multiple filters

Hi all, I am quite new into powerbi and I am struggling with a project I am trying to build.

 

I have 2 tables:

Table1 - AllOpps

opp-idopp-nameopp-value
1opp1value1
2opp2value2
3opp3value3
4opp4value4

 

Table2 - AllOppsWithContactRoles

opp-idopp-nameopp-valueContactRole
1opp1value1SalesContactRole1
1opp1value1TechnicalContactRole1
1opp1value1OtherContactRole1
2opp2value2TechnicalContactRole1
2opp2value2TechnicalContactRole2
3opp3value3SalesContactRole1
3opp3value3SalesContactRole2
3opp3value3SalesContactRole3
4opp4value4OtherContactRole1

 

The relationship between table1 and table2 is 1:many based on the opp-id field

 

What I am trying to achieve is the following - add 3 custom columns and count the info from table2 and represent it like this:

opp-idopp-nameopp-valueSalesContactRoleTechContactRoleOtherContactRole
1opp1value1111
2opp2value2020
3opp3value3300
4opp4value4001

 

I have been testing with countrows and filter but then it counts for each item all rows he found (not filtering by opp-id) and I cannot figure it out.

 

Any suggestions?

 

Thank you,

DB

  • Hi, Anonymous 

    According to your description, I think you can use calculated columns in Power BI to achieve this requirement easily, you can follow my steps:

    Create these calculated columns in the table ‘AllOpps’:

    SalesContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"SalesContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)
    TechContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"TechnicalContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)
    OtherContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"OtherContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)

    Then you can get what you want, like this:

     

    You can go to the report page and create a table chart like this:

     

    You can download my test pbix file here

     

    Best Regards,

    Community Support Team _Robert Qin

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

2 Replies

  • v-robertq-msft's avatar
    v-robertq-msft
    Community Support

    Hi, Anonymous 

    According to your description, I think you can use calculated columns in Power BI to achieve this requirement easily, you can follow my steps:

    Create these calculated columns in the table ‘AllOpps’:

    SalesContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"SalesContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)
    TechContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"TechnicalContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)
    OtherContactRole =
    
    var _count=
    
    COUNTX(
    
        FILTER(
    
            RELATEDTABLE(AllOppsWithContactRoles),CONTAINSSTRING([ContactRole],"OtherContactRole")),
    
            'AllOppsWithContactRoles'[ContactRole])
    
    return
    
    IF(_count=BLANK(),0,_count)

    Then you can get what you want, like this:

     

    You can go to the report page and create a table chart like this:

     

    You can download my test pbix file here

     

    Best Regards,

    Community Support Team _Robert Qin

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous's avatar
    Anonymous
    Not applicable

    this works fine, thank you!