Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hi,
So I'm trying to show the average of a datediff between two tables.
Hi @smparnon ,
The issue you are experiencing is likely due to the many-to-many relationship between your tables, which is affecting the aggregation when calculating the average. Since there are multiple provider event times but only one pharmacist event time per MRN, it's crucial to ensure the DATEDIFF function is operating on the correct row context.
First, modify the DATEDIFF calculation to ensure it operates on single values instead of multiple rows. You can achieve this using SELECTEDVALUE, which helps to extract a single event time per context.
Time between provider completed and pharmacist initiated =
VAR ProviderTime = SELECTEDVALUE(provider[Latest_Event_Time])
VAR PharmacistTime = SELECTEDVALUE('pharmacist dc initiated'[Test Latest Pharmacist Event Time2])
RETURN IF(NOT(ISBLANK(ProviderTime)) && NOT(ISBLANK(PharmacistTime)), DATEDIFF(ProviderTime, PharmacistTime, MINUTE))
Now, for the average calculation, your current approach is summing the values instead of computing a true average in the total row. Instead of using SUMX inside the DIVIDE function, using AVERAGEX on VALUES() ensures that the aggregation happens correctly.
Average Time Between Provider Completed and Pharmacist Initiated =
AVERAGEX(
VALUES('pharmacist dc initiated'[Clean_MRN_Pharmacy]),
[Time between provider completed and pharmacist initiated]
)
If the issue persists due to the many-to-many relationship, another approach is using SUMMARIZE to explicitly reshape the table before performing the average calculation. This method ensures that the calculation is performed at the correct granularity.
Average Time Between Provider Completed and Pharmacist Initiated =
AVERAGEX(
SUMMARIZE(
'pharmacist dc initiated',
'pharmacist dc initiated'[Clean_MRN_Pharmacy],
"Time_Diff", [Time between provider completed and pharmacist initiated]
),
[Time_Diff]
)
Since your tables have a many-to-many relationship, an alternative fix is to use DISTINCT() inside SUMX to avoid double-counting MRN entries.
Average Time Between Provider Completed and Pharmacist Initiated =
DIVIDE(
SUMX(
DISTINCT('pharmacist dc initiated'[Clean_MRN_Pharmacy]),
[Time between provider completed and pharmacist initiated]
),
COUNTROWS(DISTINCT('pharmacist dc initiated'[Clean_MRN_Pharmacy]))
)
This ensures that each MRN is only counted once when computing the average. If none of these approaches fully resolve the issue, consider introducing a bridge table to mediate the many-to-many relationship and structure the data in a more controlled manner. Let me know if you need further refinements.
Best regards,
Those didn't seem to give correct averages. If I create a bridge table as
Hi @smparnon ,
Thank you for reaching out to Microsoft Fabric Community Forum.
@DataNinja777 Thank you for your quick reply.
@smparnon Thanks for the update! You're absolutely right , the main challenge here is the many-to-many relationship between the provider and pharmacist tables, combined with the absence of a reliable 1:1 mapping. This makes it difficult to perform accurate row-level calculations such as DATEDIFF.
Please try a few workarounds mentioned below:
If you are able to create a unique identifier (e.g., a combination of MRN + timestamp, or an encounter ID), that would be the ideal solution. You could generate this either upstream in the source system or within Power Query using a row number per MRN or similar logic.
Time between provider completed and pharmacist initiated =
VAR MRN = SELECTEDVALUE('pharmacist dc initiated'[Clean_MRN_Pharmacy])
VAR PharmacistTime = SELECTEDVALUE('pharmacist dc initiated'[Test Latest Pharmacist Event Time2])
VAR ClosestProviderTime =
CALCULATE(
MAX(provider[Latest_Event_Time]),
FILTER(
provider,
provider[Clean_MRN_Provider] = MRN &&
provider[Latest_Event_Time] <= PharmacistTime
)
)
RETURN
IF(
NOT(ISBLANK(ClosestProviderTime)) && NOT(ISBLANK(PharmacistTime)),
DATEDIFF(ClosestProviderTime, PharmacistTime, MINUTE)
)
3.Then try to calculate the average:
Average Time Between Provider Completed and Pharmacist Initiated =
AVERAGEX(
VALUES('pharmacist dc initiated'[Clean_MRN_Pharmacy]),
[Time between provider completed and pharmacist initiated]
)
If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @smparnon ,
We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @smparnon
As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @smparnon ,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
9 | |
8 | |
6 | |
5 |
User | Count |
---|---|
17 | |
14 | |
10 | |
9 | |
6 |