Forum Discussion

kfortenberry's avatar
kfortenberry
Frequent Visitor
1 year ago

INTERSECT between 2 Tables with different Filter Values

Hi Everyone, I would appreciate any assistance you could provide.  I have two tables that share a Many to Many relationship. The first is a record of all activities created by our staff and the second is a list of all policies sold. (The actual tables are considerably larger and contain many more columns.)

 

Activities: need to filter for only TF activities

Client CodeActivity Type Client Name 
A123TF Adam 
B229TF Bob 
C234CT Charles 
E762NO David 
A123TF Adam 
B229NO Bob 
C234CT Charles 
A123CT Adam 
E762TF David 

 

Policies Sold: need distinct policy numbers

Client CodePolicy Number Premium 
A12311111 $          100.00
B22922222 $          300.00
C23433333 $          200.00
E76244444 $          900.00
A12311111 $            50.00
A12355555 $          400.00

 

I need to build a formula that shows a count of all policies sold only for those client codes where a TF activity was entered.  If more than one unique Policy Number exists on a client code I need to count each of these, but I would not want to count duplicates of the same policy number on the same client code. 

6 Replies

  • Hi kfortenberry 

     

    you can write a measure as follows:

     

    measure count_policy := var tbl1= summarize( filter (activities, [activity type] = "TF") , activities [client code])

    var tbl2 = summarize ( filter (policies sold , policies sold [client code] in tbl1) , policies sold [ client code] , policies sold [ policy number]  )

     

    return countrows (tbl2)

     

     
    If this post helps, then I would appreciate a thumbs up  and mark it as the solution to help the other members find it more quickly. 
  • SachinNandanwar's avatar
    SachinNandanwar
    Impactful Individual

    As there exist a many to many relationship across the two tables, create a calcuated column in the Policy table.

    Count Of Policies= COUNTX (
        SELECTCOLUMNS (
            FILTER ( RELATEDTABLE ( 'Activities' ), 'Activities'[Activity Type] = "TF" ),
            "Cnt", 'Policy'[Policy Number]
        ),
        'Policy'[Policy Number]
    )

      

    • kfortenberry's avatar
      kfortenberry
      Frequent Visitor

      Thank you for your assistance. I am receiving the following error when I try to implement this formula:

       

      "A single value for column 'PolicyNumber' in table 'Policy' cannot be determined. Thias can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi kfortenberry ,

     

    Have a good day. Have you resolved the issue?

     

    Best Regards,

    Wearsky

  • Hi kfortenberry ,

     

    Regarding the many-to-many relationship, it appears that you need to create a separate dimension table for Client Code, and create a deta model like below, where many-to-one relatinoships are created for the two fact tables. 

    Then, you can write a measure like below:

    Activity TF =
    CONCATENATEX (
        DISTINCT ( Activities[Activity Type] ),
        IF ( Activities[Activity Type] = "TF", Activities[Activity Type], BLANK () )
    )
    

    Which is then brought to the Client Code dimension table,

    This is brought through the normal relationship path to the other fact table, Policies Sold.

    You can then write a measure like the one below to identify the distinctcount of policy numbers where the client codes have an 'TF' activity type.

    Policy Number distinctcount (TF) =
    CALCULATE (
        DISTINCTCOUNT ( 'Policies Sold'[Policy Number] ),
        KEEPFILTERS ( 'Policies Sold'[TF] = "TF" )
    )
    

    The resulting output is displayed below.

    Please let me know if the result is line with your expectations. 

    I have attached an example pbix file for your reference.