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
jcecil2
Helper I
Helper I

How to find an average row count of one table for each date category in a related table using DAX?

Hi folks, 

I am attempting to create a measure that calculates average submissions per pay period where submissions are stored in one table and pay periods are stored in a related table. 

 

Here is the relationship between the two tables. It is a 1:Many on Date and Created Date.

PayPeriodAverage.png

 

 

Dates Table Sample:

DatePay Period
6/30/20196/23/2019 - 7/06/2019
7/1/20196/23/2019 - 7/06/2019
7/3/20196/23/2019 - 7/06/2019
7/14/20197/07/2019 - 7/20/2019
7/17/20197/07/2019 - 7/20/2019
7/19/20197/07/2019 - 7/20/2019
7/21/20197/21/2019 - 8/03/2019
7/24/20197/21/2019 - 8/03/2019

 

 

Service Desk Table Sample:

idCreated DateEmployee
16/23/2019Smith, Bob
26/23/2019Roberts, Laurie
36/24/2019Samuel, Tyler
47/01/2019Smith, Bob
57/05/2019Roberts, Laurie
67/07/2019Smith, Bob
77/09/2019Collins, Gabi
87/18/2019Collins, Gabi
97/19/2019Samuel, Tyler
107/20/2019Smith, Bob
117/25/2019Roberts, Laurie
127/28/2019Smith, Bob
138/01/2019Samuel, Tyler

 

For this sample data, the measure would return 4.333333. Pay period 6/23/2019-7/06/2019 had 5 requests, 7/07/2019 - 7/20/2019 had 5 requests, and 7/21/2019 - 8/03/2019 had 3 requests. 13 total requests/3 total pay periods = 4.33333.

Been stuck on this one for a while and would appreciate any help you all can offer. 

Thanks

-Jacob

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @jcecil2 ,

this measure should work: 

Average over Pay Periods = AVERAGEX(VALUES(Dates[Pay Period]), COUNTROWS(RELATEDTABLE('Service Desk')))

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
v-lid-msft
Community Support
Community Support

Hi @jcecil2 ,

 

If date column in your date table have all the value in create column, the @ImkeF 's formula should work fine, but if the relation is not complete, such as some value did not in Dates table, for example, the sample table you have shared, we can use the following formula to meet your requirement:

 

AverageMeasure = 
AVERAGEX (
    ADDCOLUMNS (
        DISTINCT ( 'DatesTable'[Pay Period] ),
        "Start", DATEVALUE ( LEFT ( [Pay Period], FIND ( " - ", [Pay Period] ) ) ),
        "End", DATEVALUE (
            RIGHT ( [Pay Period], LEN ( [Pay Period] ) - FIND ( " - ", [Pay Period] ) - 2 )
        )
    ),
    COUNTROWS (
        FILTER (
            'ServiceDeskTable',
            AND ( [Created Date] >= [Start], [Created Date] <= [End] )
        )
    )
)

2.PNG

 


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-lid-msft,

I apologize for the confusion my sample data caused. My date table does contain every date, but I excluded a lot of dates as I wanted to have a few pay periods in order to best exemplify what I was trying to achieve.  I accepted @ImkeF 's answer as it is simple and most likely applicable to others who are trying to perform similar operations. However, your answer is most correct given the sample data I provided.

Thank you for your help 🙂

ImkeF
Super User
Super User

Hi @jcecil2 ,

this measure should work: 

Average over Pay Periods = AVERAGEX(VALUES(Dates[Pay Period]), COUNTROWS(RELATEDTABLE('Service Desk')))

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.