Forum Discussion

GuillaumeB's avatar
GuillaumeB
Helper I
5 years ago
Solved

Aggregating calculation by months within a variable

I have a table that has a structure similar to this: Item Date Profit Cost1 Cost2 A 1-jan 9.53 0.15 7.10 A 31-jan 9.50 0.15 7.10 A 01-Feb 9.48 0.20 7.15 A 28-Feb ...
  • lbendlin's avatar
    5 years ago

    Your sample data does not agree with your explanation of the expected outcome. For example there is no 5.60 value (did you mean 5.78?) and no 5.89 value (did you mean 5.87 and 5.33 ?

     

    First step: Create a calculated column identifying the month:

     

    YearMonth = FORMAT('Table'[Date],"YYYYmm")

     

    Next:  Write the measure :

     

    Measure = 
    // find max date per month
    var a = GROUPBY('Table','Table'[YearMonth],"d",maxx(CURRENTGROUP(),'Table'[Date]))
    // calculate quotient
    var b = ADDCOLUMNS(a,"q",CALCULATE(DIVIDE(SUM('Table'[Profit])-SUM('Table'[Cost1])-SUM('Table'[Cost2]),SUM('Table'[Cost2]),0),Filter('Table','Table'[Date]=[d])))
    //multiply result
    return PRODUCTX(b,[q])

     

    And finally, put the yearmonth and the measure into a table.

    The way the measure is written will work both for the column total and the individual rows