Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Mariposa14
New Member

Average occurrences across a date range

I'm new to Power BI and DAX and have found this forum so helpful in getting started - thanks so much for all the great tips! There is one problem that I'm struggling to resolve and wondered if someone could help me. I have data that looks something like this - there are multiple occurrences of each ID on both the same day and spanning several days. 

 

DateID
01-Jan101
01-Jan102
02-Jan101
02-Jan101
02-Jan103
03-Jan102
03-Jan103
04-Jan104
04-Jan101
05-Jan101
06-Jan102
07-Jan104

 

I would like to work out the average frequency of each ID over a period of time. So far I have used a calculated column to find the number of times each ID occurs, using the following formula: 

Frequency = COUNTX(
    FILTER('Test data', EARLIER('Test data'[ID]) = 'Test data'[ID]), 'Test data'[ID]
)
 
Which gives me this result:
 
DateIDFrequency
01-Jan-231015
01-Jan-231023
02-Jan-231015
02-Jan-231015
02-Jan-231032
03-Jan-231023
03-Jan-231032
04-Jan-231042
04-Jan-231015
05-Jan-231015
06-Jan-231023
07-Jan-231042

 

I then tried to find the average number of occurrences for each ID using the following formula:

Average frequency = averagex(values('Test data'[ID]),calculate(AVERAGE('Test data'[Frequency])))
 
YearQuarterMonthDayAverage frequency
2023Qtr 1January14
2023Qtr 1January23.5
2023Qtr 1January32.5
2023Qtr 1January43.5
2023Qtr 1January55
2023Qtr 1January63
2023Qtr 1January72

 

This excludes any times when there is more than one occurrence for an ID on the same day (e.g. ID 101 on 2 January); however, it is including occurrences of IDs on different days - e.g. ID 103 is included on both 2 and 3 January.

 

I would like to include the average number of occurrences for each ID only once, associated with the date of the first occurrence. Is this possible?

 

Many thanks for any suggestions!

1 ACCEPTED SOLUTION
v-tangjie-msft
Community Support
Community Support

Hi @Mariposa14 ,

 

According to your case requirements, you have successfully calculated the count value of the frequency of the deduplicated ID so far, but the problem you are currently experiencing is that you only want to find the first occurrence of each ID into the count, and the rest should be excluded, for your needs, I have done some local tests, I think you can refer to this possible solution

Start by creating a calculated column.

Flag =

 var _firstdaete=CALCULATE(MIN('Test data'[Date]),FILTER('Test data','Test data'[ID]=EARLIER('Test data'[ID])))

 return

 IF('Test data'[Date]=_firstdaete,1,0)

Create another measure to calculate the average value of the count that matches your desired ID, grouped by date.

Frequency1 =

CALCULATE(DISTINCTCOUNT('Test data'[ID]),FILTER(ALLSELECTED('Test data'),'Test data'[ID]=MAX('Test data'[ID])))

Then you can create a visual like this to check if it meets your needs.

vtangjiemsft_0-1673250834158.png

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

View solution in original post

2 REPLIES 2
v-tangjie-msft
Community Support
Community Support

Hi @Mariposa14 ,

 

According to your case requirements, you have successfully calculated the count value of the frequency of the deduplicated ID so far, but the problem you are currently experiencing is that you only want to find the first occurrence of each ID into the count, and the rest should be excluded, for your needs, I have done some local tests, I think you can refer to this possible solution

Start by creating a calculated column.

Flag =

 var _firstdaete=CALCULATE(MIN('Test data'[Date]),FILTER('Test data','Test data'[ID]=EARLIER('Test data'[ID])))

 return

 IF('Test data'[Date]=_firstdaete,1,0)

Create another measure to calculate the average value of the count that matches your desired ID, grouped by date.

Frequency1 =

CALCULATE(DISTINCTCOUNT('Test data'[ID]),FILTER(ALLSELECTED('Test data'),'Test data'[ID]=MAX('Test data'[ID])))

Then you can create a visual like this to check if it meets your needs.

vtangjiemsft_0-1673250834158.png

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

FreemanZ
Super User
Super User

 @Mariposa14 

could you also provide your expected result?

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.