Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for a $400 discount.
Register nowGet inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.
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
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!
Check out the February 2025 Power BI update to learn about new features.
User | Count |
---|---|
19 | |
12 | |
10 | |
9 | |
9 |
User | Count |
---|---|
17 | |
14 | |
12 | |
10 | |
9 |