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
ebhdlt
Frequent Visitor

Calculate average of DISTINCTCOUNT by week number

Dear Power BI community,

 

I have data related to patient admissions in a healthcare facility.

I am trying to calculate the number of patients present each day (i.e. the occupancy) on average per week number (see last column in red):

 

DayWeek numberOccupancyAverage occupancy per week
January 1 20201115141.85
January 2 20201139141.85
January 3 20201158141.85
January 4 20201167141.85
January 5 20201164141.85
January 6 20201145141.85
January 7 20201105141.85
January 8 20202104121.57
January 9 20202120121.57
January 10 20202130121.57
January 11 20202141121.57
January 12 20202141121.57
January 13 20202125121.57
January 14 2020290121.57

 

I have the following tables:

  • Calendar: all information related to dates (day, week number, etc.)
  • Presence: this table contains a column called [PatientsPresent] which itself contains a unique ID for each patient who is present on a given day.

For example:

DatePatientsPresent
January 1 2020Patient_1_ID
January 1 2020Patient_2_ID
January 2 2020Patient_1_ID
January 2 2020Patient_3_ID
January 3 2020Patient_3_ID

 

January 1 2020: Patients 1 & 2 are present; DISTINCTCOUNT = 2

January 2 2020: Patients 1 & 3 are present (patient 2 was discharged); DISTINCTCOUNT = 2

January 3 2020: Patient 3 is present (patient 1 was discharged); DISTINCTCOUNT = 1

 

I am currently using the following measure:

 

 

 

Measure =
VAR _WeekdaysINWeekNum = CALCULATE(COUNTROWS('Calendar'), FILTER(ALLSELECTED('Calendar'), 'Calendar'[WeekNumber] = MAX('Calendar'[WeekNumber])))
VAR _OccupancyPerWeek = CALCULATE(DISTINCTCOUNT('Presence'[PresentPatients]), FILTER(ALLSELECTED('Calendar'), 'Calendar'[WeekNumber] = MAX('Calendar'[WeekNumber])))

RETURN
_OccupancyPerWeek / _WeekdaysINWeekNum

 

 

 

 

The problem is that is provides me with the number of distinct patients per week number (divided by the number of week days).

However, I want the sum of all patients present during each day (divided by the number of week days). This means that patients who are present on more than one day will be counted more than once, which is what it should do. But when I use COUNT instead of DISTINCTCOUNT, I get incorrect numbers.

 

Thank you for your help!

2 REPLIES 2
v-xuding-msft
Community Support
Community Support

Hi @ebhdlt ,

Have resolved this problem? If not, maybe you could try the following formula.

AvgWeeklySales =
VAR _WeekdaysINWeekNum =
    CALCULATE (
        COUNTROWS ( 'Calendar' ),
        FILTER (
            ALLSELECTED ( 'Calendar' ),
            'Calendar'[WeekNumber] = MAX ( 'Calendar'[WeekNumber] )
        )
    )
VAR _OccupancyPerWeek =
    CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Calendar', 'Calendar'[Date] ) )
RETURN
    DIVIDE ( _OccupancyPerWeek, _WeekdaysINWeekNum )

 

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

Seems like you should just have to do COUNTROWS then... Not entirely clear here, something is up because that should be relatively easy. 


@ 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...

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.