cancel
Showing results for
Did you mean:
Regular Visitor

## How to calculate the average count of rows? Persons with dif. start and end dates in dif. groups

I have a table with different persons, all with different start and end dates (of employment).
I want to calculate the count of them for eyery time. But not only for every day, I also need to find the average for time periods month and quarter.
The persons belong to different groups. Here is where I get lost:
If I calculate countrows in a calculated column of the datetable, filtering on the groups is not applied to this calculation.
So any following calculation like average is not influenced by the group. Even filtering with something like
Persons[Group]=SELECTEDVALUE(Persons[Group]) does not work, because measures can not be used inside calculated colums, as I learned.
Any idea how to solve this?

The table is simple like

 Name Group Startdate Enddate A 1 01.01.21 23.03.21 B 2 01.02.21 09.10.21 C 3 01.04.21 20.12.21 D 4 02.05.21 06.09.21 E 1 13.02.21 12.04.21 F 2 02.06.21 23.07.21 G 3 03.04.21 28.10.21 H 4 05.07.21 04.09.21 I 1 12.06.21 13.11.21 J 2 01.01.21 23.08.21 K 3 01.02.21 09.10.21 L 4 01.04.21 20.12.21 M 1 02.05.21 06.09.21

Datetable is simple Calendarauto. No relationship between the two tables.
The (not correctly working) calculated colum was

Count of active Persons per Day =
CALCULATE(COUNTROWS(Persons),
FILTER(Persons,Persons[Startdate]<=(Days[Date])),
FILTER(Persons,Persons[Enddate]>=(Days[Date])),
FILTER(Persons,Persons[Group]=SELECTEDVALUE(Persons[Group]))
)
1 ACCEPTED SOLUTION
Super User

@OliTFD , Try like

averageX(values(Days[Date]), calculate(COUNTROWS(Persons),
FILTER(Persons,Persons[Startdate]<=(Days[Date]) && Persons[Enddate]>=(Days[Date]) && Persons[Group]=SELECTEDVALUE(Persons[Group]))
))

or

averageX(values(Days[Date]), calculate(COUNTROWS(Persons),
FILTER(Persons,Persons[Startdate]<=(Days[Date]) && Persons[Enddate]>=(Days[Date]) )
))

!! Microsoft Fabric !!
Microsoft Power BI Learning Resources, 2023 !!
Learn Power BI - Full Course with Dec-2022, with Window, Index, Offset, 100+ Topics !!
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
2 REPLIES 2
Super User

@OliTFD , Try like

averageX(values(Days[Date]), calculate(COUNTROWS(Persons),
FILTER(Persons,Persons[Startdate]<=(Days[Date]) && Persons[Enddate]>=(Days[Date]) && Persons[Group]=SELECTEDVALUE(Persons[Group]))
))

or

averageX(values(Days[Date]), calculate(COUNTROWS(Persons),
FILTER(Persons,Persons[Startdate]<=(Days[Date]) && Persons[Enddate]>=(Days[Date]) )
))

!! Microsoft Fabric !!
Microsoft Power BI Learning Resources, 2023 !!
Learn Power BI - Full Course with Dec-2022, with Window, Index, Offset, 100+ Topics !!
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Regular Visitor

Great! Works like magic! Thank you!