Forum Discussion

Sarah_Jolliffe's avatar
Sarah_Jolliffe
New Member
6 years ago

Sum / divide values between dates

Hoping someone can help. I've got stuck trying to do time-based analysis on the following table concerning carbon savings. An example of the table is below and also the closest DAX code used in a measure. The measure is not summing correctly.

 

[Carbon initiatives]

Initiaitve nameStart dateEnd DateDaily tCO2e SavedTotal tCO2e Saved
Carbon 130/6/1831/12/200.00070990.6
Carbon 217/7/1817/10/180.168129715.5
Carbon 31/1/1731/12/192.23804042,448.4
Carbon 422/2/1920/5/200.142583564.6

 

[Measure]

tCO2e saved = VAR tmpCalendar = ADDCOLUMNS('Datetable',"Month",MONTH([Date]),"Year",YEAR([Date]),"MonthYear",VALUE(YEAR([Date])))
VAR tmpCarbon = ADDCOLUMNS('Carbon_Initiatives',"MonthYearBegin",VALUE(YEAR([Start date])),
                                      "MonthYearEnd",VALUE(YEAR([End date])))
VAR tmpTable =
SELECTCOLUMNS(
    FILTER(
        GENERATE(
            tmpCarbon,
            SUMMARIZE(tmpCalendar,[Year],[Month],[MonthYear])
        ),
        [MonthYear] >= [MonthYearBegin] &&
        [MonthYear] <= [MonthYearEnd]
    ),
    "tCO2e",Carbon_Initiatives[Daily tCO2e Saved]
)
RETURN SUmx(tmpTable,[Total tCO2e])
 
[PBI model]
YeartCO2e saved
2017649927.39
2018793721.03
2019483959.38

 

Any help much appreciated

2 Replies

    • Sarah_Jolliffe's avatar
      Sarah_Jolliffe
      New Member
      Spoiler
      Cheers for the quick reply, I did come across those 2 posts and plagerised the code just inserted my table refs but that is chucking out duff figures. this is what i did;



      tCO2e saved = VAR tmpCalendar = ADDCOLUMNS('Dates2',"Month",MONTH([Date]),"Year",YEAR([Date]),"MonthYear",VALUE(YEAR([Date])))
      VAR tmpcarbon = ADDCOLUMNS('Carbon_Initiatives',"MonthYearBegin",VALUE(YEAR(Carbon_Initiatives[Start date])),
                                            "MonthYearEnd",VALUE(YEAR(Carbon_Initiatives[End date])))
      VAR tmpTable =
      SELECTCOLUMNS(
          FILTER(
              GENERATE(
                  tmpcarbon,
                  SUMMARIZE(tmpCalendar,[Year],[Month],[MonthYear])
              ),
              [MonthYear] >= [MonthYearBegin] &&
              [MonthYear] <= [MonthYearEnd]
          ),
          "CI_UID",[CI Ref number],
          "Year",[Year],
          "Month",[Month],
          "tCO2e",Carbon_Initiatives[Daily tCO2e Saved]
      )
      RETURN SUMX(tmpTable,[tCO2e])



      and i now get these results

      YeartCO2e Saved
      2018629.48
      2019708.15
      20201.35

      should be this

      YeartCO2e Saved
      20183,450
      20194,807
      2020909

      Ideas?