Forum Discussion

pawelk3's avatar
pawelk3
Helper I
3 years ago
Solved

Count rows from related table (M2M)

Hi,

I have two tables: 'Lead' and "Contact' with relation many to many using keys Lead[ParentContactId] and Contact[ContactId]. I want to create a measure in leads table to count number of matching contact records for each lead based on few criteria:

 

  1. Contact[ContractDate] > Lead[CreatedOn] AND Contact[ContractDate] <= Lead[CreatedOn] + 90
  2. if there is no matching contact record or there is no contract for particular contact record (Contact[ContractId] = BLANK()) then show 0

Then I want to split this count equally for each matching lead, so if there are two contact records meeting the criteria for particular lead and two leads with the same 'ParentContactId' then show 1 for each lead.

Each 'Contact' record represents one purchase so that is why contact ids are duplicated. I can group them to get unique records and many to one relation between the tables. I tried it but I have problem with referencing columns that have been grouped as table structured column.

 

I got to that point and got stuck:

 

 

Conversions = 
CALCULATE(
    COUNTROWS(Contact),
    FILTER(
        ALL(Contact),
        COUNTROWS(
            FILTER(
                RELATEDTABLE(Contact),
                Contact[ContractDate] >= Lead[CreatedOn] &&
                Contact[ContractDate] <= Lead[CreatedOn] + 90
            )
        ) > 0
    )
)

 

 

I am getting an error: "A single value for column 'CreatedOn' in table 'Lead' cannot be determined. This 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." probably because of many to many relation.

 

I would be very grateful if someone would like to guide me on how to solve this

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi pawelk3 ,

    It seems that what you are trying to create is a measure, please update the formula of measure[Conversions] as below. Later check if it can return your expected result...

    Conversions =
    VAR _seldate =
        SELECTEDVALUE ( Lead[CreatedOn] )
    RETURN
        CALCULATE (
            COUNT ( Contact[ContactId] ),
            FILTER (
                Contact,
                Contact[ContractDate] >= _seldate
                    && Contact[ContractDate] <= _seldate + 90
            )
        )

    In addition, you can review the following blog to know the common DAX errors...

    DAX error messages in Power BI

     

    If the above one can't help you, please provide some raw data in your table (exclude sensitive data) with Text format, your treemap Fields settings and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

    And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

    Best Regards

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi pawelk3 ,

    It seems that what you are trying to create is a measure, please update the formula of measure[Conversions] as below. Later check if it can return your expected result...

    Conversions =
    VAR _seldate =
        SELECTEDVALUE ( Lead[CreatedOn] )
    RETURN
        CALCULATE (
            COUNT ( Contact[ContactId] ),
            FILTER (
                Contact,
                Contact[ContractDate] >= _seldate
                    && Contact[ContractDate] <= _seldate + 90
            )
        )

    In addition, you can review the following blog to know the common DAX errors...

    DAX error messages in Power BI

     

    If the above one can't help you, please provide some raw data in your table (exclude sensitive data) with Text format, your treemap Fields settings and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

    And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

    Best Regards