Forum Discussion

Contezini's avatar
Contezini
Frequent Visitor
4 years ago
Solved

Advanced Sum between Dates

Dear community,   I know there are hundreds of topics regarding calculation of Sum Between dates, but none of them seems to fit my needs.   I have 2 data tables + 1 calendar table Site Sizes: ...
  • tamerj1's avatar
    4 years ago

    Hi Contezini 

    Here is a sample file with the solution https://www.dropbox.com/t/BFyYg068Dfa9YNbe
    We have two issue to solve here.

    The first one we have define which start belongs to which end as there is no stage or ranking column that related each start to its relevant end. 

    The 2nd issue is generate only the relevant rows in between each start and stop. 

    Here is the data model with inactive or no relationship with the date table.

    The first issue can be solved by creating the following calculated column

    Ranking = 
    RANKX ( 
        CALCULATETABLE ( 
            'Site Log', 
            ALLEXCEPT ( 'Site Log', 'Site Log'[Site_ID],'Site Log'[Log Change] ) 
        ), 
        'Site Log'[Date],, 
        ASC 
    )

    The measure that generates the relevant rows with the relevant site size

    Filter Measure = 
    VAR CurrentDate = MAX ( 'Date'[Date] )
    VAR T1 =
        ADDCOLUMNS (
            SUMMARIZE ( 'Site Log', 'Site Log'[Site_ID],'Site Log'[Ranking],'Site Sizes'[Site_Size] ),
            "@Start", CALCULATE ( MAX ( 'Site Log'[Date] ), 'Site Log'[Log Change] = "Started" ),
            "@End", COALESCE ( CALCULATE ( MAX ( 'Site Log'[Date] ), 'Site Log'[Log Change] = "Ended" ), TODAY ( ) )
        )
    VAR T2 =
        GENERATE ( 
            T1,
            VAR StartDate = [@Start]
            VAR EndDate =  [@End]
            RETURN
                CALENDAR ( StartDate, EndDate )
        )
    VAR T3 = FILTER ( T2, [@Start] <= CurrentDate && [@End] >= CurrentDate )
    RETURN
        MAXX ( T3, [Site_Size] )