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
trinitykala
Frequent Visitor

Distinct count of employees in a DateTime matrix

Hello,
I’m currently working on "the time and attendance" tabular model (the bellow info is based on sample data). I have to say that working with Date and Time it makes me cry sometimes.

I have been recently asked to produce a new report which will show how many people, work in a store during the day.

Counting how many employees we have on the shop floor on every full hour. 

 

I would like to see something like that:

 

matrix.JPG

 

My sample data in my Fact table looks like that:

BranchNoEmpKeyEventDatePHP_StartDatePHP_StartTimePHP_EndDatePHP_EndTimePunchStartReasonPunchWorkedHrs
1234Ala12/07/201812/07/201821:00:0013/07/201802:30:00shift6.17
1234Ala12/07/201813/07/201804:00:0013/07/201806:30:00shift3.12
1234Ala13/07/201813/07/201821:00:0014/07/201802:30:00shift6.2
1234Ala13/07/201814/07/201804:00:0014/07/201806:30:00shift3.2
1234Mat12/07/201812/07/201822:00:0013/07/201801:30:00shift4.12
1234Mat12/07/201813/07/201803:00:0013/07/201806:00:00shift3.6
1234Mat13/07/201813/07/201822:00:0014/07/201801:30:00shift4.08
1234Mat13/07/201814/07/201803:00:0014/07/201806:00:00shift3.6
1234Emma12/07/201812/07/201812:30:0012/07/201816:30:00shift4.55
1234Emma12/07/201812/07/201818:00:0012/07/201822:00:00shift4.28


From here you can see that Ala and Mat are working nightshifts. And Emma is on Dayshift.

 

This is how my model looks like at the moment:

sample model.JPG

I did not linked my DimTime table to a Fact Table as I use DAX to creat a matrix. I do have a tendency to over complicate stuff so I hope there is an easier and better solution to my pronlem.

 

So if my shift is on the same day (StartDate = EndDate) then my measure works as I only comapare time. But it doesn't work with employees who works nightshift as StartTime is greater then EndTime which in that case does not have sense.

 

emp.JPG

Could you please help me? How can i compare Date and Time to show employees who did nightshift.

 

my measure so far looks like that: 

 

HeadCount =
SUMX (
    CROSSJOIN (
        SUMMARIZE (
            PunchEvent,
            DimDate[Date],
            PunchEvent[PHP_StartTime],
            PunchEvent[PHP_EndTime]
        ),
        SUMMARIZE ( DimTime, DimTime[Time24] )
    ),
    IF (
        AND (
            ( FORMAT ( DimTime[Time24], "HH:mm:ss" ) )
                >= ( FORMAT ( PunchEvent[PHP_StartTime], "HH:mm:ss" ) ),
            ( FORMAT ( DimTime[Time24], "HH:mm:ss" ) )
                <= ( FORMAT ( PunchEvent[PHP_EndTime], "HH:mm:ss" ) )
        ),
        1,
        0
    )
)

 

thanks!

 

 

3 REPLIES 3
v-yulgu-msft
Employee
Employee

Hi @trinitykala,

 

Which field did you add to matrix "column" to show Hours? Which field did you add t Matirx "rows" to show Date values? Based on my assumption, it's "EventDate" from Fact table, right? If so, I have one concern, for the second row, the PunchWorkedHrs should be calculated on 12/07/2018 or 13/07/2018?

1.PNG

 

Regards,

Yuliana Gu

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

Hi Yuliana,

Thank you for your reply. Probably would be easier if I could attache a pbix file. 🙂

To show Hours in a column I used: DimTime[Hour24] and for Date Value I used DimDate[Date] which is linked via active relationship with PunchEvents[Event Date], and in active relationships to PHP_StartDate and PHP_EndDate for the shift.

 

I can see your concern and you are correct it should be PHP_StartDate instead Event Date.

 

If I change dates I will get something like that:

newmatrix.JPG

My biggest issue is to get Orange part working correctly. 

I was thinking to create virtual table where I will split my over midnight shift in to two shifts.

 

Original shift segment:

EventDate

PHP_StartDate

PHP_StartTime

PHP_EndDate

PHP_EndTime

09-Jul-18

09-Jul-18

21:00:00

10-Jul-18

02:30:00

 

Split:

EventDate

PHP_StartDate

PHP_StartTime

PHP_EndDate

PHP_EndTime

09-Jul-18

09-Jul-18

21:00:00

10-Jul-18

00:00:00

09-Jul-18

10-Jul

00:00:00

10-Jul-18

02:30:00

 

But I got stuck on how to insert new row.

newmatrix2.JPG

 

I hope that helps 🙂 

Does anyone have any idea on how to help me out with this issue, please? 

Any links to examples of time (not date) related articles or videos would be much appriciated. 

How do you, build your models to handle Date and time? 

 

thanks!  

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.