Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Diference between 2 dates by period

Hi,

I need to calculate Time utilization like this:

TU = DIVIDE([Rented Out] , [Days])

I need the amount of rented out in days of each vehicle, but what I can not figure out is how to outcome by a selected period (diference of days betwen end date and start day):

Outcome

Rented out is the amount of days with machines rented and Days, the amount of aviable days without weekends in the same period.

The problem is that my database has diferent machines whose are rented by day. I have a list of those machines with the start date (Fecha inicio) and end date (Fecha Fin). The report should be able to visualize TU by the user time selected.

I can not find out the way to calculate the workday (without weekend) and to calculate properly by month, for example if a machine starts june 29th  and ends july 5th should count 2 days in june, and 3 in july (without week end).

I don not know why NETWORKDAYS is not working.

 

This is my DAX formula and my database and calendar table.

Rented out =
VAR Inicio = CALCULATE( MIN(Starting date) )

VAR Fin =  CALCULATE( MAX([Ending date]))

VAR Laborales = NETWORKDAYS(Inicio, Fin)


VAR Diferencia =CALCULATE( Laborales ,  'Calendar'[Date] <= Inicio && 'Calendario' [Date] >= Fin, DATESBETWEEN('Calendario'[Date] , FIRSTDATE('Calendario inicio'[Date]), ENDOFMONTH('Calendario inicio'[Date])))

return


Diferencia

 

This is mi data:

VehicleStarting dateEnding date
XS3G7531/01/2022 0:0001/02/2022 0:00
XS3G7501/02/2022 0:0002/02/2022 0:00
NBT3 -511/01/2022 0:0012/01/2022 0:00
N89M 37211/02/2022 0:0015/02/2022 0:00
XS3G7502/02/2022 0:0003/02/2022 0:00
NBT3 -502/02/2022 0:0002/02/2022 0:00
NBT3 -512/01/2022 0:0013/01/2022 0:00
XS3G7511/01/2022 0:0012/01/2022 0:00
NBT3 -502/02/2022 0:0004/02/2022 0:00
XS3G7512/01/2022 0:0013/01/2022 0:00
XS3G7513/01/2022 0:0013/01/2022 0:00
XS3G7503/02/2022 0:0003/02/2022 0:00
XS3G7503/02/2022 0:0003/02/2022 0:00
XS3G7503/02/2022 0:0004/02/2022 0:00
XS3G7513/01/2022 0:0014/01/2022 0:00
NBT3 -513/01/2022 0:0014/01/2022 0:00
N89M 37215/02/2022 0:0011/02/2022 0:00
N89M 37211/02/2022 0:0016/02/2022 0:00
N89M 37216/02/2022 0:0011/02/2022 0:00
N89M 37211/02/2022 0:0018/02/2022 0:00
XS3G7504/02/2022 0:0007/02/2022 0:00
NBT3 -504/02/2022 0:0009/03/2022 0:00
NBT3 -514/01/2022 0:0014/01/2022 0:00
XS3G7514/01/2022 0:0028/01/2022 0:00
N89M 37207/02/2022 0:0008/12/2021 0:00
XS3G7507/02/2022 0:0007/02/2022 0:00
XS3G7507/02/2022 0:0030/03/2022 0:00
NBT3 -514/01/2022 0:0021/01/2022 0:00
NBT3 -521/01/2022 0:0026/01/2022 0:00
N89M 37208/02/2022 0:0008/02/2022 0:00
N89M 37218/02/2022 0:0018/02/2022 0:00
N89M 37208/02/2022 0:0009/02/2022 0:00
N89M 37218/02/2022 0:0018/02/2022 0:00
N89M 37218/02/2022 0:0025/02/2022 0:00
N89M 37209/02/2022 0:0010/02/2022 0:00
N89M 37210/02/2022 0:0011/02/2022 0:00
N89M 37207/03/2022 0:0030/05/2022 0:00
N89M 37225/02/2022 0:0028/02/2022 0:00
NBT3 -526/01/2022 0:0002/02/2022 0:00
NBT3 -509/03/2022 0:0009/03/2022 0:00
N89M 37228/02/2022 0:0002/03/2022 0:00
NBT3 -509/03/2022 0:0009/03/2022 0:00
NBT3 -509/03/2022 0:0015/03/2022 0:00
N89M 37202/03/2022 0:0002/03/2022 0:00
N89M 37202/03/2022 0:0002/03/2022 0:00
N89M 37202/03/2022 0:0004/03/2022 0:00
NBT3 -515/03/2022 0:0009/03/2022 0:00
XS3G7528/01/2022 0:0031/01/2022 0:00
N89M 37204/03/2022 0:0007/03/2022 0:00
NBT3 -509/03/2022 0:0021/03/2022 0:00
XS3G7501/04/2022 0:0001/04/2022 0:00
XS3G7501/04/2022 0:0005/04/2022 0:00
NBT3 -501/04/2022 0:0001/04/2022 0:00
NBT3 -501/04/2022 0:0006/04/2022 0:00
XS3G7505/04/2022 0:0005/04/2022 0:00
XS3G7505/04/2022 0:0005/04/2022 0:00
XS3G7505/04/2022 0:0005/04/2022 0:00
XS3G7505/04/2022 0:0008/04/2022 0:00
NBT3 -506/04/2022 0:0006/04/2022 0:00
NBT3 -506/04/2022 0:0007/04/2022 0:00
NBT3 -507/04/2022 0:0008/04/2022 0:00
XS3G7505/05/2022 0:0006/06/2022 0:00
NBT3 -508/04/2022 0:0008/04/2022 0:00
NBT3 -508/04/2022 0:0008/04/2022 0:00
XS3G7508/04/2022 0:0008/04/2022 0:00
XS3G7508/04/2022 0:0008/04/2022 0:00
NBT3 -508/04/2022 0:0008/04/2022 0:00
NBT3 -508/04/2022 0:0011/04/2022 0:00
NBT3 -511/04/2022 0:0023/05/2022 0:00
XS3G7508/04/2022 0:0011/04/2022 0:00
XS3G7511/04/2022 0:0005/05/2022 0:00
NBT3 -523/05/2022 0:0009/04/2022 0:00
NBT3 -521/03/2022 0:0021/03/2022 0:00
NBT3 -509/04/2022 0:0017/05/2022 0:00
NBT3 -517/05/2022 0:0017/05/2022 0:00
NBT3 -517/05/2022 0:0002/06/2022 0:00
N89M 37202/06/2022 0:0003/06/2022 0:00
NBT3 -502/06/2022 0:0023/06/2022 0:00
N89M 37203/06/2022 0:0027/12/2046 0:00
N89M 37230/05/2022 0:0030/05/2022 0:00
XS3G7513/06/2022 0:0015/06/2022 0:00
N89M 37230/05/2022 0:0002/06/2022 0:00
NBT3 -521/03/2022 0:0001/04/2022 0:00
XS3G7506/06/2022 0:0010/06/2022 0:00
XS3G7515/06/2022 0:0015/06/2022 0:00
XS3G7515/06/2022 0:0020/06/2022 0:00
NBT3 -523/06/2022 0:0024/06/2022 0:00
NBT3 -524/06/2022 0:0030/06/2022 0:00
NBT3 -530/06/2022 0:0024/06/2022 0:00
XS3G7520/06/2022 0:0020/06/2022 0:00
XS3G7520/06/2022 0:0020/06/2022 0:00
XS3G7520/06/2022 0:0006/07/2022 0:00
XS3G7530/03/2022 0:0030/03/2022 0:00
NBT3 -524/06/2022 0:0030/06/2022 0:00
NBT3 -530/06/2022 0:0024/06/2022 0:00
NBT3 -524/06/2022 0:0030/06/2022 0:00
NBT3 -530/06/2022 0:0030/06/2022 0:00
XS3G7530/03/2022 0:0030/03/2022 0:00
NBT3 -530/06/2022 0:0030/06/2022 0:00
XS3G7530/03/2022 0:0031/03/2022 0:00
NBT3 -530/06/2022 0:0030/06/2022 0:00
NBT3 -530/06/2022 0:0001/07/2022 0:00
NBT3 -501/07/2022 0:0027/12/2046 0:00
XS3G7531/03/2022 0:0001/04/2022 0:00
XS3G7506/07/2022 0:0007/07/2022 0:00
XS3G7507/07/2022 0:0007/07/2022 0:00
XS3G7507/07/2022 0:0007/07/2022 0:00
XS3G7512/07/2022 0:0013/07/2022 0:00
XS3G7507/07/2022 0:0008/07/2022 0:00
XS3G7513/07/2022 0:0027/12/2046 0:00
XS3G7508/07/2022 0:0012/07/2022 0:00
XS3G7510/06/2022 0:0013/06/2022 0:00
  • Hi Anonymous ,

     

    I made some changes based on your original DAX:

     

    First create a weekday column in calendar table to help calculate:

     

     

    weekday = WEEKDAY([Date],2)

     

     

    Then apply it to the changed DAX :

     

     

    Rented out =
    
    VAR Inicio = CALCULATE( MIN('Table'[Starting date]) )
    
    
    
    VAR Fin =  CALCULATE( MAX('Table'[Ending date]))
    
    
    
    VAR Laborales = CALCULATE(COUNT('Calendar'[Date]),FILTER('Calendar',[weekday]<=5&&[Date]<=Fin&&[Date]>=Inicio))
    
    
    
    VAR Diferencia =CALCULATE( Laborales ,  'Calendar'[Date] <= Inicio && 'Calendar'[Date] >= Fin, DATESBETWEEN('Calendar'[Date] , FIRSTDATE('Calendar'[Date]), ENDOFMONTH('Calendar'[Date])))
    
    
    
    return
    
    Diferencia

     

     

    Output:

    If the result is not what you want, please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

    Refer to:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

     

    Best Regards,

    Jianbo Li

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

1 Reply

  • Hi Anonymous ,

     

    I made some changes based on your original DAX:

     

    First create a weekday column in calendar table to help calculate:

     

     

    weekday = WEEKDAY([Date],2)

     

     

    Then apply it to the changed DAX :

     

     

    Rented out =
    
    VAR Inicio = CALCULATE( MIN('Table'[Starting date]) )
    
    
    
    VAR Fin =  CALCULATE( MAX('Table'[Ending date]))
    
    
    
    VAR Laborales = CALCULATE(COUNT('Calendar'[Date]),FILTER('Calendar',[weekday]<=5&&[Date]<=Fin&&[Date]>=Inicio))
    
    
    
    VAR Diferencia =CALCULATE( Laborales ,  'Calendar'[Date] <= Inicio && 'Calendar'[Date] >= Fin, DATESBETWEEN('Calendar'[Date] , FIRSTDATE('Calendar'[Date]), ENDOFMONTH('Calendar'[Date])))
    
    
    
    return
    
    Diferencia

     

     

    Output:

    If the result is not what you want, please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

    Refer to:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

     

    Best Regards,

    Jianbo Li

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