Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Sum column for dates within period

Hi, everybody! There are two simple tables: events: d_user_id date count 1 2019-04-01 5 1 2022-05-01 19 2 2020-01-01 78 3 2020-05-01 22 3 2018-05-01 32 4 2021-0...
  • daxer-almighty's avatar
    5 years ago
    // There has to be a dimension
    // that holds all the users.
    // It'll be connected to both
    // tables, Events and Periods,
    // on UserID with one-way filtering.
    // There must not be any direct relationship
    // between Events and Periods. They can
    // only be linked via dimensions.
    
    [Count for Events] =
    SUMX(
        DISTINCT( Users[UserId] ),
        CALCULATE(
            SUMX(
                Events,
                var EventDate = Events[Date]
                VAR EventCount = Events[Count]
                var PeriodExists =
                    NOT ISEMPTY(
                        FILTER(
                            Periods,
                            Periods[start_date] <= EventDate
                            &&
                            EventDate <= Periods[end_date]
                        )
                    )
                return
                    IF( PeriodExists, EventCount )
            )
        )
    )