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
RvdHeijden
Post Prodigy
Post Prodigy

Difficult formula, just can't seem to get it to work

Ive got this formula, but can't seem to get it to work.

It returns the error:

 

In DATEDIFF function, the start date cannot be greater than the end dateThe current operation was cancelled because another operation in the transaction failed.

 

It probably is because there are BLANK dates in the colums but how can i change this formula so that it wont return an error.

Something like if the 'DueDate' is blank is should use TODAY () or something ??

 

 

Current Sick Hour =
var currMin=MAX(Verzuimverloop[Datum ziektemelding])
var currMax=MAX(Verzuimverloop[DueDate])
var currDate=MAX('Date'[Date])
var leftPart=DATEDIFF(currMin;DATE(YEAR(currMin);MONTH(currMin);DAY(currMin))+TIME(16;30;0);HOUR)-if(HOUR(currMin)<11;2;if(HOUR(currMin)>11&&HOUR(currMin)<13;13-HOUR(currMin)))
var rightPart=DATEDIFF(DATE(YEAR(currMax);MONTH(currMax);DAY(currMax))+TIME(8;0;0);currMax;HOUR)-if(HOUR(currMin)>13;2;if(HOUR(currMin)>11&&HOUR(currMin)<13;13-HOUR(currMin)))
return
if(OR(WEEKDAY(currDate;2)=6;WEEKDAY(currDate;2)=7)||
 OR(DATEVALUE(currDate)<DATEVALUE(currMin);DATEVALUE(currDate)>DATEVALUE(currMax));0;
 if(DATEVALUE(currDate)>DATEVALUE(currMin)&&DATEVALUE(currDate)<DATEVALUE(currMax);8;
  if(DATEVALUE(currDate)=DATEVALUE(currMin);leftPart;rightPart)))

1 ACCEPTED SOLUTION

Hi @RvdHeijden,

 

This scenario should use table Verzuimverloop. That's why we take this table back. Maybe the formula is this one:

Aantal ziektemeldingen =
CALCULATE (
    COUNT ( 'Verzuimverloop'[Naam voluit] );
    ALLEXCEPT ( 'Aantal keren ziek'; 'Aantal keren ziek'[Naam voluit] )
)

Best Regards!

Dale

Community Support Team _ Dale
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

105 REPLIES 105

@RvdHeijden

 

Hi,

I have no ideas by far. Firstly, we correct the relationship as the picture showed. Delete the one in red cross, then the other one can be activated. Secondly, I have several tips here.

1. Line graph has a timeline?

2. Many months have the same TotalWorkHour, so try more times.

Good luck!

2017-05-17_1539.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale.

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

@v-jiascu-msft

the Timeline is 1 visual and the Line Graph is another one. but the TotalWorkHours always has the same value 296032.

My Table 'Date' starts with 01-01-2014 so it probably counts the total number of workdays * 8

 

i have been changing the relationships but it still doesnt give me the result i wanted.

As you can see we have 70 employees but the formula for TotalWorkingHours reads 280 and with 70 people *8 is 560 hours a day so when i select a month it should read a minimum of 11.200 hours.

 

 

2017-05-18_0912.png2017-05-18_0913.png

 

 

 

 

 

 

@RvdHeijden

 

Hi,

 

It seems that you used the wrong column. You should use column "date" from table "Date" and column "Afdeling" from table "Departments". And also other columns. Because table "Verzuimverloop" only has records. It didn't have the whole thing.  Have a try please. Good luck!

Difficult formula, just can't seem to get it to work3.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale 

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

@v-jiascu-msft

The relationship between 'Date' and 'Verzuimverloop' is between 'Date' and 'Date'

The relationship between 'departments' and 'Verzuimverloop' is between 'Afdeling' and 'Afdeling'.

 

If i check my report and the hours displayed in TotalWorkHours and TotalLeaveHours it looks like the TotalWorkHours is only calculated in a periode in which the person was sick.

 

For example ive selected a department with only 1 person in it and selected the month of May.
Both 'TotalWorkHours' and 'TotalLeaveHours' return (Blank) but when i select  January it reads 16,00 TotalLeaveHours and 16 TotalWorkhours
When i dont select a period it gives a timeline between Feb 2016 and January it reads 48,00 TotalLeaveHours and 16 TotalWorkHours

@RvdHeijden

 

Hi,

 

The relationships sound good. The problem is that you choose the wrong columns in the report. Please refer to the picture and try again.

Difficult formula, just can't seem to get it to work4.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

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

@v-jiascu-msft

i changed one of the collums because they refered to the wrong table but im afraid it's still not ok

ive double checked something and when i select just Q2 of 2017 it has 291.984 workinghours and 997 leavehours.

 

what it does is check who was sick in that period and adds the total amount all the LeaveHours of those people and then counts the total amount of WorkHours of the SAME people in stead of ALL the people

 

EDIT: ive change the picture because i changed the formula back to what it was

 

2017-05-22_1132.png

@RvdHeijden

 

Hi,

 

For the SAME people, we need to change the formula. BTW, is TotalLeaveHours right?

TotalWorkHours =
SUM ( Dates[IsWorkday] ) * 8
    * DISTINCTCOUNT ( Verzuimverloop[People] )

Best Regards!

Dale

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

@v-jiascu-msft

First of all i would like to thank you for al your time and effort you punt in this.

 

Im having a hard time pinpointing the source of the trouble.

 

Sometimes the TotalLeaveTime returns the corrects number of hours and sometimes it doesn't.

The TotalWorkHours never seem to be correct, can you check if they are correct ?

TotalWorkHours = TOTALMTD(SUM('Date'[IsWorkday]); 'Date'[Date] ) * 8 * [Totaal aantal medewerkers]

TotalLeaveHours = CALCULATE (SUM ( Verzuimverloop[WorkHoursLeave] );FILTER ( Verzuimverloop; Verzuimverloop[Check] <> 9999 ))

@RvdHeijden

 

Hi,

 

It's my pleasure. Finding out tricks is a hard work. Let's do it step by step. 

First, this formula depends on the calculated column. As we tested before, [WorkHoursLeave] is right. This should be good.

TotalLeaveHours =
CALCULATE (
 SUM ( Verzuimverloop[WorkHoursLeave] );
 FILTER ( Verzuimverloop; Verzuimverloop[Check] <> 9999 )
)

Second, as you said, the total hours should be the SAME people. So this formula should be changed like this.

TotalWorkHours =
SUM ( Dates[IsWorkday] ) * 8
    * DISTINCTCOUNT ( Verzuimverloop[People] )

Third, the field of slicer of date should come from Dates table. The Afdeling should come from table department. 

Fourth, the table Verzuimverloop have Afeling which could lead mismatch between afeling and employee.

Please check these things. If error appears again, please find out a special one to debug.

 

Best Regards!

Dale

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

@v-jiascu-msft

I'm sorry Dale but both formulas dont seem to work but somtimes it returns the right value and sometimes it dont, i have NO idea why that is.

 

To clarify:

The purpose of my linegraph is to show the sickrate in a given period per department.

For that to work i need 2 things

 

1. Total amount of WorkingHours

2. Total amount of LeaveHours

 

Normally it would be very simple, select a period, Measures 1 and 2 will calculate total WorkHours and LeaveHours in that period and devide them.

Same goes if i select a deparment and no period

 

i might have found a mistake in another formula that calculates the number of sick days but it seems to include the weekends.

Can you change this formula ?

 

Totaal aantal Ziektedagen = var DIFF =
   IF (Verzuimverloop[CorrectStartTime] < Verzuimverloop[CorrectEndTime];
       DATEDIFF ( Verzuimverloop[CorrectStartTime]; Verzuimverloop[CorrectEndTime]; DAY);
       -1 * DATEDIFF ( Verzuimverloop[CorrectEndTime]; Verzuimverloop[CorrectStartTime]; DAY) )
       RETURN IF ( Verzuimverloop[CorrectEndTime] = BLANK();
           DATEDIFF ( Verzuimverloop[CorrectStartTime]; TODAY()+1; DAY); IF (DIFF = 0; 1; DIFF) )
   

i already have a date table with 'IsWorkday' value 0 and 1 in it so it should be simple (for you :))

 

@RvdHeijden

 

Hi,

 

What's this formula for? Did we get the right leave hours? I remember that we have gotten right total leave hours. This formula missed many conditions. There was a formula for days.

Days =
IF (
    Verzuimverloop[Check] = 0,
    DATEDIFF ( Verzuimverloop[CorrectStartTime], Verzuimverloop[CorrectEndTime], DAY )
        - 2
            * (
                WEEKNUM ( Verzuimverloop[CorrectEndTime], 2 )
                    - WEEKNUM ( Verzuimverloop[CorrectStartTime], 2 )
            ),
    9999
)

Best Regards!

Dale

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

@v-jiascu-msft

 

Dale,

i doublechecked this formula and its correct 


The new formula calculates the number of days between 2 dates but it doesn't calculate in workdays.

I think i need it to find the problem with our formula(s)

 

Totaal aantal Ziektedagen = var DIFF =
   IF (Verzuimverloop[CorrectStartTime] < Verzuimverloop[CorrectEndTime];
       DATEDIFF ( Verzuimverloop[CorrectStartTime]; Verzuimverloop[CorrectEndTime]; DAY);
       -1 * DATEDIFF ( Verzuimverloop[CorrectEndTime]; Verzuimverloop[CorrectStartTime]; DAY) )
       RETURN IF ( Verzuimverloop[CorrectEndTime] = BLANK();
           DATEDIFF ( Verzuimverloop[CorrectStartTime]; TODAY()+1; DAY); IF (DIFF = 0; 1; DIFF) )

@RvdHeijden

 

Hi,

 

Which parts or which values are wrong? It's hard to find out the root cause, we'd better start from one employee and a small range of time.

I am not sure about the parts of purple. If you knew what it is, this formula is OK. We can minus 2*[WeekAmount] from this, then we will get the workdays.

Totaal aantal Ziektedagen =
VAR DIFF =
    IF (
        Verzuimverloop[CorrectStartTime] < Verzuimverloop[CorrectEndTime],
        DATEDIFF (
            Verzuimverloop[CorrectStartTime],
            Verzuimverloop[CorrectEndTime],
            DAY
        ),
        -1
            * DATEDIFF (
                Verzuimverloop[CorrectEndTime],
                Verzuimverloop[CorrectStartTime],
                DAY
            )
    )
RETURN
    IF (
        Verzuimverloop[CorrectEndTime] = BLANK (),
        DATEDIFF ( Verzuimverloop[CorrectStartTime], TODAY () + 1, DAY ),
        IF ( DIFF = 0, 1, DIFF )
    )

Best Regards!

Dale

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

@v-jiascu-msft

The formula itself is correct however it only has to count the workdays and NOT the weekend.

If someon is sick from monday till monday it reads 7 days instead of 5 (because it calculates with the saturday and sunday)

 

How do i add this 'We can minus 2*[WeekAmount] from this, then we will get the workdays.' in this formula ?

@RvdHeijden

 

Hi,

 

In order to make it clear, you can add a new column and use this formula.

New Column = [Totaal aantal Ziektedagen] - 2 * [WeekAmount]

 Have a try please. 

 

Best Regards!

Dale

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

@v-jiascu-msft

 

That did the Trick !!

Now we can get back to the real problem with the TotalLeaveHours and TotalWorkHours

 

When i select a single month the maximum TotalWorkHours should be 20 workingdays * 8 hours is 160 hours but some people have way more TotalWorkHours for example 272 hours or 696 hours and that is impossible.

 

Someone actually has 227 TotalLeaveHours and 696 TotalWorkHours in just the month of May, how is that possible

 

TotalWorkHours = TOTALMTD(SUM ( 'Date'[IsWorkday] );'Date'[Date]) * 8 * COUNT (employees[id])

 

im guessing the problem is in the last part of this formula, shouldn't there be a FILTER in this formula ?

@RvdHeijden

 

Hi, 

 

Yeah. This formula is wrong due to your new condition "SAME people". I updated it days ago. Please try this new one.

TotalWorkHours =
SUM ( Dates[IsWorkday] ) * 8
    * DISTINCTCOUNT ( Verzuimverloop[People] )

This post contains too much things. Let's do it step by step.

 

Best Regards!

Dale

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

@v-jiascu-msft

Ive changed the formula but im not sure if it did the trick.

Ive selected one month only in the timeline visual and now it reads 2720 TotalWorkHours

 

quick calculation indicates that 17 people where sick in that month where normally they would work 17*8*20=2720 hours.

So that part works but that is not what i wanted.

 

The TotalWorkHours per person still reads 8 hours, no matter what selection i make and it should read 160 TotalWorkHours - (Value TotalLeaveHours) and if i select 2 months it should read 320 (-Value TotalLeaveHours)

 

If someone has a TotalLeaveHours of 24 then my TotalWorkHours should be 136 (give ive selected 1 month)


however my ultimate goal is that i can have the % of ALL people that were sick per department

 

2017-05-23_1231.png

@RvdHeijden

 

Hi,

 

Do you mean TotalWorkHours = TotalWorkHours(Due to) - TotalLeaveHours? Just consider one day, employee A should work 8 hours. And A asked 3 hours leave. So TotalLeaveHours = 3, TotalWorkHours = 5, TotalWorkHours(Due to) = 8. Is this right?

 

TotalWorkHours evaluate the work hours of the people who have sick leaves as you posted in Monday. (SAME people). Should this be changed?

 

TotalWorkHours would evaluate the whole dates if no filter applied. Please check your formula TotalWorkHours again.

 

Best Regards!

Dale

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

@v-jiascu-msft

ive brainstormed with a collegue of mine concerning this formula and we found out that my way of thinking was wrong.

its hard to explain but ill try none the less

 

My goal is to calculate the % of sickhours within the total amount of workinghours (100%)

We have been calculating with TotalLeaveHoures and TotalWorkHours but the TotalWorkHours always seem to be wrong.

 

Where we went wrong is that if i want to calculate the % SickHours we first need the 100% WorkingHours which is 8 hours a day

and untill now we always had the TotalWorkHours calculated as 8 hours - TotalLeaveHours for example 3 TotalLeaveHours means 5 TotalWorkHours but that should have been 8 hours because if we calculate the % TotalLeaveHours we need the 100 % value, in this case 8 hours a day.

 

When we changed the formulas we found out that if we selected one week it returned 40 TotalWorkHours which is correct but when a person was sick from thursday till wednesday it calculated a TotalLeaveHours of lets say 5 days * 8 hours is 40 hours but in a linegraph those 40 hours where al placed on the first day of that person sickleave which is incorrect.

 

we needed to find a way to devide thoses 40 hours over the 5 days that he was sick and also make that visual in the linegraph.

 

My collegue and i brainstormed and came up with the solution to create a new table using this formula:

 

Verzuimdagen = SUMMARIZE(GENERATE(Verzuimverloop;CALCULATETABLE(VALUES(Date2[Date]);DATESBETWEEN('Date2'[Date];Verzuimverloop[CorrectStartTime];verzuimverloop[CorrectEndTime])));Verzuimverloop[Werknemernummer];Verzuimverloop[Naam voluit];'date2'[Date])

 

That resulted in a new table, a sort of 'Date' table, but only with dates in it where people where sick.
So if one a give date 1 person was sick that date had 1 line and when 10 people where sick on 1 date it returned 10 lines 

the reason we think we need this table is because if someone is sick for 10 days the full 10 days will be shown on the first day of his sickdate instead of devided over the 10 days.

 

Another example is that if someone called in sick, let say on the 28th of April and he calls in better on the 28th of May the full 20 days will be shown in April instead of 2 days in April and 18 days in May, i hope you understand what im trying to explain here.

 

We believe that we needed a table where we can display per date if a person was sick so that we can devide the total Sickdays

But now im struggling how i get this data visual in the line graph, any ideas ? (sorry for the long story :))

2017-05-24_1622.png2017-05-24_1629.png

@RvdHeijden

 

Hi,

 

It's a long time from last reply. I find a way to get it finally. It isn't an easy way. Let's make the steps simple.

1. Install R. Microsoft Open R is recommended. Maybe you need help from your IT admin.

2. Check settings.

 

Difficult formula, just can't seem to get it to work10.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. Backup your current PBIX first.

4. In Query Editor, import the source data or copy a current one.

5. Delete other columns to keep three columns. (I don't know if this is right. Just "employee", "startdate", "enddate" needed. You can merge the other columns later if you need them.)

6. Rename "Datum ziektemelding" to "Datumziektemelding"  and "Totaal.Naam voluit" to "Totaal.Naamvoluit" (delete the space).Difficult formula, just can't seem to get it to work11.jpg

 

 

 

 

 

 

 

 

 

 

 7. Open Run R Script and input these R code.

 

i <- 1
rows <- nrow(dataset)
while (i <= rows) {
  j <- 1
  people <- as.character(dataset[i, 1])
  start_time <- strptime(dataset[i, 2], "%Y-%m-%dT%H:%M:%S", tz = "UTC")
  end_time <- strptime(dataset[i, 3], "%Y-%m-%dT%H:%M:%S", tz = "UTC")
  start_date <- strptime(dataset[i, 2], "%Y-%m-%d", tz = "UTC")
  end_date <- strptime(dataset[i, 3], "%Y-%m-%d", tz = "UTC")
  if (is.na(end_date) == TRUE) {
    end_time <- strptime(Sys.time(), "%Y-%m-%d %H:%M:%S", tz = "UTC")
    end_date <- strptime(Sys.Date(), "%Y-%m-%d", tz = "UTC")
  }
  days <- difftime(end_date, start_date, units = "days")
  if (is.na(days) == FALSE && days > 0) {
    for (j in 1:days ){
      if (j == days) {
        new_record <- data.frame(Totaal.Naamvoluit = people, Datumziektemelding = as.character(as.Date(start_time) + j), Duedate = as.character(end_time))
      }
      else {
        new_record <- data.frame(Totaal.Naamvoluit = people, Datumziektemelding = as.character(as.Date(start_time) + j), Duedate = as.character(as.Date(start_time) + j))
      }
      dataset <- rbind(dataset, new_record)
    }
  }
  i <- i + 1
}
result <- dataset

Difficult formula, just can't seem to get it to work12.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 8. If no error message showed up, you can see there are two results. Click the second Table. 

Difficult formula, just can't seem to get it to work13.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

9. You can see that the duration is split into days. If you chose a special date duration in the report, the result will be sum of the duration respectively.

10. Change the type of columns to Date type. You can change the names of columns back (add spaces to the names where needed).

11. The formula of CorrectStartTime is still OK. The foumula of CorrectEndTime should be this one:

CorrectEndTime =
VAR et =
    TIME ( HOUR ( Verzuimverloop[Duedate] ), MINUTE ( Verzuimverloop[Duedate] ), SECOND ( Verzuimverloop[Duedate] ) )
RETURN
    IF (
        DATEDIFF (
            [Datum ziektemelding],
            IF ( ISBLANK ( [Duedate] ), TODAY (), [Duedate] ),
            DAY
        )
            > 0,
        [Datum ziektemelding].[date] + TIME ( 16, 30, 0 ),
        IF (
            et < TIME ( 8, 0, 0 )
                || et > TIME ( 16, 30, 0 ),
            [Duedate].[date] + TIME ( 16, 30, 0 ),
            [Duedate]
        )
    )

12. Evaluate leave hours:

LeaveHours =
VAR st =
    TIME ( HOUR ( Verzuimverloop[CorrectStartTime] ), MINUTE ( Verzuimverloop[CorrectStartTime] ), SECOND ( Verzuimverloop[CorrectStartTime] ) )
VAR et =
    TIME ( HOUR ( Verzuimverloop[CorrectEndTime] ), MINUTE ( Verzuimverloop[CorrectEndTime] ), SECOND ( Verzuimverloop[CorrectEndTime] ) )
VAR ml =
    TIME ( 12, 0, 0 )
VAR mr =
    TIME ( 12, 30, 0 )
RETURN
    IF (
        WEEKDAY ( [CorrectStartTime], 2 ) IN { 6, 7 },
        0,
        IF (
            st <= ml
                && et <= ml,
            24
                * ( et - st ),
            IF (
                st <= ml
                    && et >= mr,
                24
                    * ( et - st )
                    - 0.5,
                IF ( st >= mr && et >= mr, 24 * ( et - st ), 9999 )
            )
        )
    ) 

13. Add a new column Relationship.

Relationship = [CorrectStartTime].[date]

 

Difficult formula, just can't seem to get it to work16.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14. Create relationships.

 

Difficult formula, just can't seem to get it to work14.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 15. Create report.

TotalLeaveHours =
CALCULATE (
    SUM ( Verzuimverloop[LeaveHours] ),
    FILTER ( Verzuimverloop, Verzuimverloop[LeaveHours] <> 9999 )
)

TotalWorkHours =
SUM ( Dates[IsWorkday] ) * 8
    * DISTINCTCOUNT ( Employee[Totaal.Naam voluit] )

16. The measure will answer the slicer this time. Please have a try.Difficult formula, just can't seem to get it to work15.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

You may encounter problem in the part of R. If so, please post snapshot and error message.

 

Best Regards!

Dale

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

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.