Forum Discussion

kkanda's avatar
kkanda
Icon for Resolver II rankResolver II
2 years ago
Solved

Cumulative total for month

Hi All,

I have run into a problem I cannot resolve. I have set of dates, Target and Actual numbers in a table. I need to calculate the cumulative total for each day of the month. The logic appears to be simple enough, but it doesn't work. Here is the data:

DATE            TARGET ACTUAL RT_mnth st_day_mnth  last_day_mnth

1/1/20232738381/1/20231/31/2023
1/2/20233920201/1/20231/31/2023
1/3/20232637371/1/20231/31/2023
1/4/20233624241/1/20231/31/2023
1/5/20231823231/1/20231/31/2023
1/6/20232319191/1/20231/31/2023
1/7/20231423231/1/20231/31/2023
1/8/20233610101/1/20231/31/2023
1/9/20231440401/1/20231/31/2023
1/10/20231012121/1/20231/31/2023
1/11/20233625251/1/20231/31/2023
1/12/20232020201/1/20231/31/2023
1/13/20231037371/1/20231/31/2023
1/14/20233610101/1/20231/31/2023
1/15/20234029291/1/20231/31/2023

'st_day_mnth' and 'last_day_mnth' are calculate measures from DATE. 'RT_mnth' is the cumulative total of ACTUAL column and has the following expression:

RT_mnth = CALCULATE(
    SUM(TestSh[ACTUAL]), FILTER(TestSh,
        TestSh[DATE]>= [st_day_mnth] && TestSh[DATE] <= [last_day_mnth])
)
I tried ALL and ALLEXCEPT functions but it does not give me the desired result. I have also tried SUMX with the table name and the column ACTUAL, but there is no change. 
Please suggest a suitable change to my code so that I get the cumulative total up to the date in the DATE column for each month. One solution is to change this to a date table and apply DATEMTD. But I want to understand why it is not working. 
Please suggest where I am going wrong. 
 
  • kkanda 

    Use this measure:



    RT_mnth =
    VAR __StartDate =
        EOMONTH ( MAX ( TestSh[DATE] ), -1 ) + 1
    VAR __LastDate =
        MAX ( TestSh[DATE] )
    RETURN
        CALCULATE (
            SUM ( TestSh[ACTUAL] ),
            TestSh[DATE] >= __StartDate,
            TestSh[DATE] <= __LastDate
        )

2 Replies

  • kkanda 

    Use this measure:



    RT_mnth =
    VAR __StartDate =
        EOMONTH ( MAX ( TestSh[DATE] ), -1 ) + 1
    VAR __LastDate =
        MAX ( TestSh[DATE] )
    RETURN
        CALCULATE (
            SUM ( TestSh[ACTUAL] ),
            TestSh[DATE] >= __StartDate,
            TestSh[DATE] <= __LastDate
        )
  • Thank you Fowmy... it worked. I tried STARTOFMONTH function for __startDate. That has worked also.