Forum Discussion

mccollough's avatar
mccollough
Helper I
5 years ago
Solved

Average Frequency by Hour Calculation

Hello Everyone!

 

Penny for your thoughts

 

Fact Table

Exam Id                Report Date           Report Hour            Employee                 Location                   Task Type                     
1A6/1/202010:00 AMJohn DoeOfficeAB
1B6/1/20205:00 AMJane DoeHomeAB
1C6/1/202010:00 AMBatmanBat CaveCD
1D6/1/20203:00 PMJohn DoeOfficeCD
2A6/1/20205:00 PMJane DoeHomeAB
2B6/1/20207:00 AMBatmanBat CaveEF
2C6/2/20208:00 AMJohn DoeHomeAB
2D6/2/20201:00 AMJane DoeBat CaveAB
3A6/2/20203:00 PMBatmanOfficeCD
3B6/2/20204:00 PMJohn DoeHomeEF
3C6/2/20206:00 PMJane DoeBat CaveAB
3D6/2/20209:00 AMBatmanOfficeCD
5A6/3/20203:00 AMJohn DoeBat CaveCD
4B6/3/20205:00 AMJane DoeOfficeEF
5C6/3/20207:00 AMBatmanHomeAB
4C6/3/202010:00 AMBatmanHomeAB
4A6/3/202011:00 AMJohn DoeBat CaveAB
5B6/3/20205:00 PMJane DoeOfficeEF
6A6/4/20208:00 PMJohn DoeOfficeCD
6B6/4/20201:00 AMJane DoeHomeEF
6C6/4/20202:00 AMBatmanBat CaveAB
7A6/4/20204:00 AMJohn DoeOfficeCD
7B6/4/20207:00 PMJane DoeHomeCD
7C6/4/20209:00 AMBatmanBat CaveUS
8A6/5/202010:00 AMNed FlandersHomeUS
8B6/5/20203:00 PMBart SimpsonOfficeAB
9A6/6/202011:00 AMJohn DoeHomeEF
9B6/6/20205:00 AMJane DoeBat CaveCD
9C6/6/202010:00 AMBatmanOfficeCD
10A6/7/20203:00 PMJohn DoeHomeAB
11A6/8/202012:00 PMJane DoeBat CaveEF
11B6/8/20201:00 PMBatmanOfficeAB
11C6/8/20208:00 AMJohn DoeBat CaveCD
11D6/8/20201:00 AMJane DoeOfficeAB
11E6/8/20203:00 PMBatmanHomeEF
11F6/8/20204:00 PMJohn DoeBat CaveCD
12A6/9/20206:00 PMJane DoeOfficeUS
12B6/9/20202:00 PMBatmanHomeCD
12C6/9/202011:00 AMJohn DoeOfficeAB



Goal: 
Visualize the average number of exams and task types by hour

Current Approach:

Calculate the number the of 'Task Types' filtered  by 'Report Hour' and visualize that on a stacked column chart as follows
Axis: Report Hour
Legend: Report Type
Values: Average TaskType by Report Hour

DAX for Average TaskType by Report Hour

Average TaskType by ReportHour =
 
AVERAGEX
(
   'Fact Table',
    CALCULATE
    (
         COUNT('Fact Table'[Task Type]),
         FILTER('Fact Table', 'Fact Table'[Report Hour])
    )
)


Result:

 

 

I still get a result that shows me the number of Exam Types by Hour.

 

Where did I go wrong? How Can I do this correctly?

Data Validation Value:

These are the exam frequencies by hour and Task Type over the 9 days covered in the data set
I want to know what the average number of exam types by hour.
 

So over 9 days there were 9 different 10:00 AM time windows
5 exams occured in that time window that time period


That means on average there were 0.55 exams in the 10:00 AM time window (5 exams/ 9 days)
    - 0.22 of them were of type AB (2 exams/9 days)

    - 0.22 of them were of type CD (2 exams/9 days)
    - 0.11 of them were of type US (1 exam/9 days)

0.22 (AB) + 0.22 (CD) + 0.11 (US) = 0.55 (Total)


I would like to visualize there number for each time slot!
(EX. What does this look like for 11:00 AM, 12:00 PM, 1:00 PM, etc.)


Thank you so much for all your help!!!!

 

  • mccollough here are 3 measures to get this going. these are self-explanatory but if you need further details, let me know.

     

    Count Exams = COUNTROWS ( Exam1 )  //exam1 is a name of the table in my model
    
    Number of Days = CALCULATE ( DISTINCTCOUNT ( Exam1[Report Date] ), ALLSELECTED () )
    
    Avg per Hour = 
    VAR __examDays = Exam1[Number of Days]
    RETURN
    SUMX (  
        SUMMARIZE ( 
            Exam1, 
            Exam1[Report Hour], 
            Exam1[Task Type], 
            "@AVG",  DIVIDE ( [Count Exams], __examDays ) 
        ), 
        [@AVG] 
    )

     

    Follow us on LinkedIn

     

    Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

     

     

     

6 Replies

    • mccollough's avatar
      mccollough
      Helper I

      parry2k 

      Aha! There's part of the problem! 
      I was thinking about it the wrong way.
      I updated the Data validation segment of my post to better reflect what I'm looking for.

      Unfortunately I still don't know how to go about making DAX perform that calculation.

       

      PS 
      I also modified the fact table slightly to reflect how the data actually appears in my real dataset

    • mccollough's avatar
      mccollough
      Helper I

      parry2k That's exactly what I was looking for! Would you mind breaking down how you accomplished that?

  • mccollough here are 3 measures to get this going. these are self-explanatory but if you need further details, let me know.

     

    Count Exams = COUNTROWS ( Exam1 )  //exam1 is a name of the table in my model
    
    Number of Days = CALCULATE ( DISTINCTCOUNT ( Exam1[Report Date] ), ALLSELECTED () )
    
    Avg per Hour = 
    VAR __examDays = Exam1[Number of Days]
    RETURN
    SUMX (  
        SUMMARIZE ( 
            Exam1, 
            Exam1[Report Hour], 
            Exam1[Task Type], 
            "@AVG",  DIVIDE ( [Count Exams], __examDays ) 
        ), 
        [@AVG] 
    )

     

    Follow us on LinkedIn

     

    Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.