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

DateDiff Array with a Many to Many join

I have a table, Events, that has an EventId and the EventDate. EventId is unique in the table.

I have another table, AffectedUsers, that has the UserId and an EventId.  A User can be affected by multiple Events, and an Event can affect multiple Users.  However, the user-event combinations are unqiue for this table.  I know already that the max number of Events that affected a user is about 35, and several Users have only 1 event that affected them.  

AffectedUsers

UserIdEventId
abc456123
def321123
def321246
def321789


Events

EventIdEventDate
1235/21/2023
2465/26/2023
7895/29/2023



For each User, I need to know the interval between Events and from there find the median.   Ideally I was going to make an ordered array of event dates per UserID and find the interval between each datetime in the array using EARLIER, but that was throwing some complicated errors I coulnd't find support for (I think because some Users only have one Event), so I thought I'd go another route...


First, I created two Dim tables, UniqueEvents and UniqueUsers, that just have the unique EventIds and UerIds, respectively.  Then I removed the relationships between the two main tables and had them both point to the derived dim tables. 

I was trying to use LOOKUPVALUE to even just get the Event Dates into the AffectedUsers table as a calculated field, but since it is many-to-many relationship, I got a "circular dependency error."  When I tried using RELATED, the error was the column doesn't have a relationship in the current context, although the Affected User table has a relationship to the uniqueEvent table, and the uniqueEvent Table has a relationship to the Events table.  RELATED did not work when the relationships was direct between the two tables, because of the many to many relationship, it would only work with an aggregate such as the min event date per user, which isn't what I'm looking for.

What is a good method in PowerBI Desktop to calculate the datediff array and find this median time interval?

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Share a few more rows in the 2 tables, explain your methodology and show the expected end result.  You may do so in an MS Excel file.  I'll understand your logic from there and translate that into DAX/M code.

Share the download link of the MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish:

Please find below.  

I considered going back to Kusto and using its joining conditions, but I need to be able to use PowerBI's filtering (there are some other elements in the Events table like source, owner, so on), so a calculated table would be better than a static stable from Kusto.
Thank you!

User Events

UserIdEventId
abc456123
def321123
def321246
def321789
def321963
def321369
def321765
ghi777123
ghi777246
ghi777963
kjl888246
kjl888765

 

Event Details

EventIdEventDate
1235/1/23
2465/14/23
7895/15/23
9635/20/23
3695/26/23
7655/31/23

 

Desired Output

 

UserIdEventDateArrayIntervalArrayMedianInterval
abc456<5/1/2023><null>null
def321<5/1/2023, 5/14/2023, 5/15/2023, 5/20/2023, 5/26/2023, 5/31/2023><13,1,5,6,5>5
ghi777<5/1/2023, 5/14/2023, 5/20/2023><13,6>9.5
kjl888<5/14/2023, 5/31/2023><17>17

Hi,

You may download my PBI file from here.  The interval array is wrong in row 2.  A 5 is mssing from there.  I cannot correct that.  Hope you can take it up from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.

Top Solution Authors