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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
RudyL
Helper I
Helper I

Absence streak

Hi,

The request may look familiar but I can't figure it out with the examples in the community. So I put in a new one. 

I have a table like the one below (first five columns). All employees are crossjoined with (almost) all dates of two years. For each date per employee the absflag = 1 if the employee was away, 0 if the employee was available. 

 

I want to count streaks and streak lengths. Two measures seem to be useful.

- newmeasure1: when an absence streak starts i want to see the date the streak ends

- newmeasure2: when an absence streak starts i want to see the count of dates the streak lasts (not counting the dates not in the table)

 

So when an employee is available on a date, newmeasure1 is blank and newmeasure2 is 0. When an employee is absent, newmeasure1 is the last date of the current absence streak, but only when it's the date the streak starts. Dates after the first absent date in a an absence streak return blank for newmeasure1. 

The same goes for newmeasure2. The length of an absence streak is only greater than 0 for the startdate of a streak.

 

Is this a boring question or can someone help me 🙂

 

absence.jpg

2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

Hi @RudyL 
Please refer tao attached sample file with the proposed solution
1.png2.png3.png

View solution in original post

Hi @RudyL 
You are right the ABSSTREAK calculated column. However I could not generate the other problem. Looks like the engine treats the blank as a zero anyway. I have a added a COALESCE statement as an additional layer to confirm the result.

1.png2.png 

View solution in original post

9 REPLIES 9
RudyL
Helper I
Helper I

@tamerj1 can I add an extra request to the solution above. The result now is a list that calculates what I need per dept / employee / date. 

But suppose I want a more general clustered or stacked bar chart that shows per dept the total number of absence streaks that lasted less than 3 days and the total number of absence streaks that lasted 3 days or more. 

 

I noticed changing this context had some strange effects on your calculations. 

RudyL
Helper I
Helper I

Hi @tamerj1,

your last update with the coalesce did the trick. Forget about my third remark. I thought that absences that started on the first date of the data set were not correctly handled, but they are. It turns out I used filters per year and had to add the absence year as an extra partition to make it work. That would split absence streaks from one year to the other as two seperate ones. 

So thanks again !!! 

tamerj1
Super User
Super User

Hi @RudyL 
Please refer tao attached sample file with the proposed solution
1.png2.png3.png

Hi @tamerj1 ,

First of all i'm amazed, surprised, in awe for your quick response and effort in trying to explain me how this works. First take-away on my side: There's a lot to learn.

 

But... it doesn't work for my data. After looking at your code and matching our data I found a couple of things:

1. I think you missed some checks on having multiple employees. My data has 350 while your example only has one. So I think I need to change your ABSSTREAK to the one below where an extra check on EMPID is included  

ABSSTREAK =
IF (
    'Table'[ABSFLAG] = 1,
    SUMX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[ABSDATE] <= EARLIER ( 'Table'[ABSDATE] )
            && 'Table'[EMPID] = EARLIER ( 'Table'[EMPID] )    
         ),
        'Table'[ABSSTARTFLAG]
    )
)
 
2. The change from (1) required another change in ABSDAYS.
ABSDAYS =
IF (
    MAX ( 'Table'[ABSSTARTFLAG] ) = 1,
    CALCULATE (
        SUM ( 'Table'[ABSFLAG] ),
        ALL ( 'Table'[ABSDATE] ),
        VALUES ( 'Table'[ABSSTREAK] )
       ,'Table'[EMPID]=MAX('Table'[EMPID])
    ),
    0
)
 
3. And another issue I couldn't resolve myself. When the first date in a dataset for an employee is an absence day (e.g. 1-1-2024 for a year overview), the ABSSTARTFLAG is not as it should be. It seems the offset -1 results in an error for that first date, and therefore for the complete first streak. 
 
It would be great if you would have a look at that issue (3).  

Hi @RudyL 

Looks like somehow I missed your reply. 
ABSENDDATE and ABSDAYS are measures. That means in the table visual the EMPID is already available in the fillter context as part of the visual. 

Hi @tamerj1 , please see my example data that i gave in reply to another contributor. With this sample data your solution doesn't seem to work.

Hi @RudyL 
You are right the ABSSTREAK calculated column. However I could not generate the other problem. Looks like the engine treats the blank as a zero anyway. I have a added a COALESCE statement as an additional layer to confirm the result.

1.png2.png 

Hi, @RudyL 

Thanks for tamerj1 reply. Can I ask if you have currently resolved the third issue? I used tamerj1's pbix file and then added DEPT_B with an ABSDATE of 1/1/2023, and the line corresponding to ABSSTARTFLAG seems to be the expected behavior. I don't understand what's wrong and if you can describe the effect you're looking for.

vyaningymsft_0-1724924285315.png

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Hi @v-yaningy-msft ,

Thanks for your curiosity. But try the table below in which I inserted a second employee (513020) from a different department in the same data that @tamerj1 used (just the 513019). When you take this dataset you will see that the calculations will be completely different, and wrong.

 

DEPTEMPIDABSDATEABSFLAG
DEPT_B51301902/01/20231
DEPT_B51301904/01/20231
DEPT_B51301905/01/20230
DEPT_B51301909/01/20231
DEPT_B51301911/01/20230
DEPT_B51301912/01/20230
DEPT_B51301916/01/20230
DEPT_A51302018/01/20231
DEPT_B51301918/01/20231
DEPT_A51302019/01/20230
DEPT_B51301919/01/20230
DEPT_B51301923/01/20230
DEPT_A51302023/01/20231
DEPT_B51301925/01/20231
DEPT_B51301926/01/20231
DEPT_B51301927/01/20231
DEPT_A51302027/01/20230
DEPT_B51301930/01/20231
DEPT_B51301931/01/20231
DEPT_B51301901/02/20231
DEPT_B51301902/02/20231
DEPT_A51302002/02/20231
DEPT_A51302003/02/20231
DEPT_B51301903/02/20231
DEPT_B51301906/02/20230
DEPT_B51301908/02/20230

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.