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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
domleps
Regular Visitor

7 day count and distinct count by worker in a calculated column

Hi there,

 

I have the dataset shown below.  This shows a list of workers (worker_id) and the days they are due to work (day_worked) - where a worker has not worked due to absence, they will have an absence_period_id.

 

I need to create two calculated columns in power bi that calculate the columns with red text below.

 

Please note this must be done in a calculated column and not in a measure, and I am not able to change the structure of this dataset.  The dates may also have gaps (e.g. may not be a row for each date of the year).

 

worker_idday workedabsence_period_idrolling 7 day absence_period COUNT by worker_idrolling 7 day absence_period DISTINCTCOUNT by worker_id
A01/04/2021 00
A02/04/2021 00
A03/04/2021 00
A04/04/2021 00
A05/04/2021 00
A06/04/2021111
A07/04/2021121
A08/04/2021131
A09/04/2021 31
A10/04/2021 31
A11/04/2021242
A12/04/2021252
A13/04/2021 42
A14/04/2021 32
A15/04/2021532
A16/04/2021 32
A17/04/2021343
A18/04/2021 33
A19/04/2021 22
A20/04/2021433
A21/04/2021443
A22/04/2021 32
A23/04/2021 32
A24/04/2021 21
A25/04/2021 21
A26/04/2021 21
A27/04/2021 11
A28/04/2021711
A29/04/2021 11
B01/04/2021 00
B02/04/2021611
B03/04/2021621
B04/04/2021631
B05/04/2021 31
B06/04/2021 31
B07/04/2021842
B08/04/2021852
B09/04/2021852
B10/04/2021852
B11/04/2021851
B12/04/2021861
B13/04/2021 61
B14/04/2021 51
B15/04/2021 41
B16/04/2021942
B17/04/2021 32
B18/04/2021 22
B19/04/20211022
B20/04/20211032
B21/04/20211143
B22/04/2021 43
B23/04/2021 32
B24/04/2021 32
B25/04/20211243
B26/04/20211243
B27/04/20211242
B28/04/20211241
B29/04/20211251

 

e.g. so if we look at date 25/04/2021 for worker B:

 

rolling 7 day absence_period COUNT by worker_id = count where absence_period_id <> blank between 19/04/2021 and 25/04/2021 (7 day period) where worker_id = B.   This is 4  (absence_period_id = two 10s,  one 11, one 12)

 

rolling 7 day absence_period DISTINCTCOUNT by worker_id = distinct count where absence_period_id <> blank between 19/04/2021 and 25/04/2021 (7 day period) where worker_id = B.   This is 3  (absence_period_id = 10,  11, 12)

 

I hope that makes sense and thank you for your help.

 

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Link to the sample pbix file 

 

rolling 7 day absence_period COUNT by worker_id CC =
VAR _currentworker = Data[worker_id]
VAR _currentdate = Data[day worked]
VAR _sevendaystable =
FILTER (
Data,
Data[worker_id] = _currentworker
&& Data[day worked] <= _currentdate
&& Data[day worked] > _currentdate - 7
)
VAR _absensecolumnnoblank =
FILTER (
SELECTCOLUMNS ( _sevendaystable, "@absense", Data[absence_period_id] ),
[@absense] <> BLANK ()
)
RETURN
COUNTROWS ( _absensecolumnnoblank ) + 0
 
 
rolling 7 day absence_period DISTINCTCOUNT by worker_id CC =
VAR _currentworker = Data[worker_id]
VAR _currentdate = Data[day worked]
VAR _sevendaystable =
FILTER (
Data,
Data[worker_id] = _currentworker
&& Data[day worked] <= _currentdate
&& Data[day worked] > _currentdate - 7
)
VAR _absensecolumnnoblank =
SUMMARIZE (
FILTER (
SELECTCOLUMNS ( _sevendaystable, "@absense", Data[absence_period_id] ),
[@absense] <> BLANK ()
),
[@absense]
)
RETURN
COUNTROWS ( _absensecolumnnoblank ) + 0
 
 
 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
domleps
Regular Visitor

this is amazing, thank you so much

Jihwan_Kim
Super User
Super User

Link to the sample pbix file 

 

rolling 7 day absence_period COUNT by worker_id CC =
VAR _currentworker = Data[worker_id]
VAR _currentdate = Data[day worked]
VAR _sevendaystable =
FILTER (
Data,
Data[worker_id] = _currentworker
&& Data[day worked] <= _currentdate
&& Data[day worked] > _currentdate - 7
)
VAR _absensecolumnnoblank =
FILTER (
SELECTCOLUMNS ( _sevendaystable, "@absense", Data[absence_period_id] ),
[@absense] <> BLANK ()
)
RETURN
COUNTROWS ( _absensecolumnnoblank ) + 0
 
 
rolling 7 day absence_period DISTINCTCOUNT by worker_id CC =
VAR _currentworker = Data[worker_id]
VAR _currentdate = Data[day worked]
VAR _sevendaystable =
FILTER (
Data,
Data[worker_id] = _currentworker
&& Data[day worked] <= _currentdate
&& Data[day worked] > _currentdate - 7
)
VAR _absensecolumnnoblank =
SUMMARIZE (
FILTER (
SELECTCOLUMNS ( _sevendaystable, "@absense", Data[absence_period_id] ),
[@absense] <> BLANK ()
),
[@absense]
)
RETURN
COUNTROWS ( _absensecolumnnoblank ) + 0
 
 
 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.