Forum Discussion

lubosst's avatar
lubosst
Frequent Visitor
7 years ago
Solved

Complicated date interval

Hi, I have problem with calculating date interval (cycle time) in my table. In the table, there are records for order changes - there may be multiple rows for one order: I need to calculate t...
  • lubosst's avatar
    7 years ago

    Hi, I was able to find solution, so maybe it will help somebody...

     

    At first, code for next_changed_date:

    next_changed_date = 
    VAR currentDate = 'Table'[changed_date]
    VAR currentID = 'Table'[id]
    VAR nextDate =
        CALCULATE (
            MIN ( 'Table'[changed_date] ),
            FILTER (
                ALL ( 'Table' ),
                AND (
                    'Table'[id] = currentID,
                    'Table'[changed_date] > currentDate
                )
            )
        )
    RETURN
        IF ( nextDate > 0, nextDate, NOW () )

    Now I have all changes with starting and ending date (in case, that there are no ending, I fill NOW() date).

     

    Then I have created another column for each team, which return changed date, but with only dates after their starting date:

    changed_date_Issued_To_Team1 = 
    IF (
        'Table'[changed_date] < RELATED ( 'Team Table'[Issued_To_Team1] ),
        IF (
            RELATED ( 'Team Table'[Issued_To_Team1] ) > 'Table'[next_changed_date],
            BLANK (),
            RELATED ( 'Team Table'[Issued_To_Team1] )
        ),
        IF (
            RELATED ( 'Team Table'[Issued_To_Team1] ) = 0,
            BLANK (),
            'Table'[changed_date]
        )
    )

    And create another column with next_changed_date for each team - with only dates before team ends (ignoring other dates):

    next_changed_date_End_Team1 = 
    IF (
        'Table'[changed_date_Issued_To_Team1] = 0,
        BLANK (),
        IF (
            RELATED ( 'Team Table'[end_team1] ) <> 0,
            IF (
                'Table'[next_changed_date] < RELATED ( 'Team Table'[end_team1] ),
                'Table'[next_changed_date],
                IF (
                    RELATED ( 'Team Table'[end_team1] ) < 'Table'[changed_date_Issued_To_Team1],
                    BLANK(),
                    RELATED ( 'Team Table'[end_team1] )
                )
            ),
            IF (
                'Table'[next_changed_date] <> 0,
                'Table'[next_changed_date],
                NOW ()
            )
        )
    )

    And finaly CT for each team:

    CT_Team1 = 
    IF (
        'Table'[next_changed_date_End_Team1] > 0,
        'Table'[next_changed_date_End_Team1] - 'Table'[changed_date_Issued_To_Team1]
    )

    Now I have created measure, which calculates sum of CT:

    CT_green_Team1 = 
    CALCULATE (
        SUM ( 'Team'[CT_Team1] ),
        FILTER (
            'Team',
            'Team'[field_value_new] = "green"
        )
    )

    (Except tha last one, all are calculated columns)