Forum Discussion
Sum all values per row until limit is reached with multiple criteria
Good explanation, still tricky to get it right.
I have not been able to solve this using a measure only, perhaps someone else want to give it a go.
I have made a solution using calculated columns, but this allows for less filtering oportunies than pure measure solution would. Essentially, I calculate revenue pr day for each line, and then find the number of days which are within 12 months/365 days from the latest [Contract end date] in each grouping of [Account ID] and [Product category].
Number of days =
VAR LastEndDate =
CALCULATE (
MAX ( data[Contract End Date] );
FILTER (
ALL ( data );
data[Account ID] = EARLIER ( data[Account ID] )
&& data[Product Category] = EARLIER ( data[Product Category] )
)
)
VAR LastEndDatePrevYear =
DATE ( YEAR ( LastEndDate ) - 1; MONTH ( LastEndDate ); DAY ( LastEndDate ) )
RETURN
SWITCH (
TRUE ();
LastEndDatePrevYear < data[Contract End Date]
&& data[Contract Start Date] < LastEndDatePrevYear; DATEDIFF ( LastEndDatePrevYear; data[Contract End Date]; DAY );
LastEndDatePrevYear < data[Contract End Date]
&& data[Contract Start Date] >= LastEndDatePrevYear; DATEDIFF ( data[Contract Start Date]; data[Contract End Date]; DAY )
)[Revenue pr day] =
DIVIDE (
data[Revenue];
DATEDIFF ( data[Contract Start Date]; data[Contract End Date]; DAY )
)and the measure can then be written as
Revenue last 12 months =
CALCULATE (
SUMX ( data; data[Revenue pr day] * data[Number of days] );
FILTER (
ALL ( data );
data[Account ID] = MAX ( data[Account ID] )
&& data[Product Category] = MAX ( data[Product Category] )
)
)sturlaws - I have made a few tweaks to your formulas and I am very close to getting it to work (I think) but I need the DATEDIFF to be more exact. Do you know if there is a way to force the DATEDIFF function to NOT round? Instead of coming up with 310 days, I am looking for a difference of 309.9458333 days.
- sturlaws7 years ago
Resident Rockstar
Anonymous, it as precisise as the input data is, there is 310 days between those two dates. If you need more precision, you have to add time as well.
- Anonymous7 years agoNot applicable
sturlaws - Here is the last thing keeping my report from working right now. How can I reference a value in an above row? In the attached screenshot link below, I need to have the values for 55 days (highlighted in yellow) to be subtracting out the YearFrac_Remainder in days (circled in red) so that I can have a more accurate view.
In the column 'Number of Days', the top 2 values are correct and I just need to bottom 3 lines to be 55.00 - .15063 = 54.84937.
Any idea how I can reference that specific cell?
https://drive.google.com/open?id=1mQ1De2HcZjpqH8GNepBJpL6AWZMm57gX
- sturlaws7 years ago
Resident Rockstar
what is you code for the column yearFrac_Remainder?