Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

DAX for DISTINCT Count ID on Many to many relationship table

Hi All,

 

I have two tables with Many to Many relation, I am trying to create a column to find the distinct count of "Id" and group it by start and end time. I don't want it as a measure because I am going to use the value from the new column to compare it with another column.

 

TABLE A

ID ||  Start_Time ||  End_Time ||  15_Min_Time
1 ||   2020-01-02 10:01:00 ||  2020-01-02 10:08:00  ||  2020-01-02 10:00:00
2 ||  2020-01-02 10:01:00 ||  2020-01-02 10:08:00 ||  2020-01-02 10:00:00
3 ||  2020-01-02 10:06:00 ||  2020-01-02 10:12:00 ||  2020-01-02 10:00:00
4 ||  2020-01-02 10:18:00 ||  2020-01-02 10:30:00 ||  2020-01-02 10:15:00

 

TABLE B

Time  ||  Info_Value ||   15_Min_Time
2020-01-02 10:00:00 ||  2  || 2020-01-02 10:00:00
2020-01-02 10:01:00 ||  1  || 2020-01-02 10:00:00
2020-01-02 10:02:00 ||  3  || 2020-01-02 10:00:00
2020-01-02 10:03:00 ||  4  || 2020-01-02 10:00:00
2020-01-02 10:04:00 ||  5  || 2020-01-02 10:00:00
2020-01-02 10:05:00 ||  1  || 2020-01-02 10:00:00

 

TABLE A and TABLE B are having M:M relation with 15_Min_Time

 

 Expected Output:

I want a column with distinct count of id but grouped by Start_Time

ID ||  Start_Time ||  End_Time ||   15_Min_Time ||   New_Column
1  ||   2020-01-02 10:01:00 ||  2020-01-02 10:08:00  ||  2020-01-02 10:00:00  || 2
2 ||  2020-01-02 10:01:00 ||  2020-01-02 10:08:00 ||  2020-01-02 10:00:00  || 2
3 ||  2020-01-02 10:06:00 ||  2020-01-02 10:12:00 || 2020-01-02 10:00:00  || 1

 

I tried the following formula

New_Column = CALCULATE(DISTINCTCOUNT(Table A[id]), GroupBy(Table A[Start_Time], Table A[End_Time]))

Error --- Circular Dependency Error

 

New_Column = SUMMARIZE(Table A,Table A[start_time],Table A[End_time],"Distinct ID",DISTINCTCOUNT(Table A[id]))

Error ---- The expression refers to multiple columns.

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I resolved this issue by going through the article https://www.sqlbi.com/articles/understanding-circular-dependencies/ I used the ALLExcept function to overcome the issue.

New_Col =  CALCULATE(DISTINCTCOUNT(Table A[id]),GROUPBY(Table A,Table A[start_time], Table B[End_time]),ALLEXCEPT(Table A,Table A[Calculated_Col]))

NOTE:
Calculated_Col is a column that I created to get the average of Table B(Info_Value), power bi considers all the columns in the table while creating "New_Col", so I was getting a Circular Dependency error, ALLExcept resolves that issue.

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@Anonymous First, I would highly recommend trying to get rid of any direct many-to-many relationships. Use a bridge table to connect those two tables and your life will be easier.

 

As for your errors. Your second one is because SUMMARIZE returns a table, not a single scalar value.

 

The first error, circular dependencies are difficult to resolve without more information. 


Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler  Thanks for sharing the information, I will work on resolving the M to M cardinality, I have updated my question with more information, hope this will help in resolving the issue.

Anonymous
Not applicable

I resolved this issue by going through the article https://www.sqlbi.com/articles/understanding-circular-dependencies/ I used the ALLExcept function to overcome the issue.

New_Col =  CALCULATE(DISTINCTCOUNT(Table A[id]),GROUPBY(Table A,Table A[start_time], Table B[End_time]),ALLEXCEPT(Table A,Table A[Calculated_Col]))

NOTE:
Calculated_Col is a column that I created to get the average of Table B(Info_Value), power bi considers all the columns in the table while creating "New_Col", so I was getting a Circular Dependency error, ALLExcept resolves that issue.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.