Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

DATEDIFF With Month Filter

Hello my colleagues!

 

My problem is:

 

I need to calculate the time difference between dates, starting at 06/01/2020 and ending at "NOW". However, I need this calculation to respect the hour limit of the month, but also be cumulative.

 

How i'm calculating now:

 

Currently, my equation is as follows:

Spoiler
TimeNow = DATEDIFF (DATE (2020,06,01), NOW (), HOUR)

The problem in this formula is:

 

I need every month that passes, those hours accumulate since 06/01/2020, but stop at the end of the month where there was a maintenance register.

 

My formula will start at June, and the difference in this month is only from 06/01/2020 to 06/30/2020. However for July that is cumulative, from 06/01/2020 to 07/31/2020, For August from 06/01/2020 to 08/31/2020 and so on.

 

My database have the following data, and the DATEDIFF is what i want to create:

 

EquipmentBreakdown StartBreakdown EndDATEDIFF to NOW (Hours)
A03/06/2020 09:05:0003/06/2020 15:00:00730
B15/06/2020 10:15:0015/06/2020 12:00:00730
C08/07/2020 12:00:0008/07/2020 13:00:001185 (Max is 1460, two months)


How can i solve this question? Can be in DAX or Query.

 

Thanks for the help!

5 Replies

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    Here is a general pattern for running total calculations:

    Running Total = 
    VAR MaxDateInFilterContext =
        MAX ( Dates[Date] )
    VAR MaxYear =
        YEAR ( MaxDateInFilterContext )
    VAR DatesLessThanMaxDate =
        FILTER (
            ALL ( Dates[Date], Dates[Calendar Year Number] ),
            Dates[Date] <= MaxDateInFilterContext
                && Dates[Calendar Year Number] = MaxYear
        )
    VAR Result =
        CALCULATE (
            [Total Sales],
            DatesLessThanMaxDate
        )
    RETURN
        Result
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Antriksh Sharma!

       

      Could you explain me the steps of this calculation?

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    What do you mean by "hour limit of the month"?  Can you provide example data table and desired output, so a solution can be suggested?

     

    Regards,

    Pat

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello my friend, post edited with the data table example.

  • Anonymous's avatar
    Anonymous
    Not applicable
    It's not clear at all what you want to do... "respect the hour limit of the month" - please define what you mean by this.

    Best
    D