Forum Discussion
Sum all values per row until limit is reached with multiple criteria
To keep it somewhat simple let's just say we're looking for the last 12 months of revenue starting at the latest end date per Account ID and per Product category.
So, for this example, the top 2 lines are < 12 months so I'd add the revenue from those lines (49,667 & 193,151) to the next most recent product lines (bottom 3 lines with end dates of 8/24/19). However, for these bottom 3 lines, I'd just be looking for a proportion of that revenue that gets me to a total of 12 months. So the math would be (revenue / initial term) * (12 - 10.19) <--(i.e. this is the sum of the initial terms that have already been summed together. In this scenario there is only one contract with a term of 10.19) for each of the bottom three lines separately (math explained: dividing revenue by initial term gets the monthly revenue value and then the second subtraction portion is getting the number of months that are still remaining out of the 12-months total). I could then sum up each of the 5 lines to get the prior 12-month value total as of 6/29/2019.
Note: I'd need a DO UNTIL function because if the bottom 3 lines were very short, we'll say 1 month long, then I'd need to continue down the column adding in other rows until a 12 month total was reached or if there weren't any more rows to add in. (that being said, I'd also need to add in parameters for this calculation saying that is there is ever a gap period >= 6 months then do not add in that row's revenue into the calculation.
I know this sounds extremely confusing so please let me know what I can explain better.
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] )
)
)- Anonymous7 years agoNot applicable
Thanks for your help! This is close but it is still off by $15,897.21. Please let me know if you (or anyone else) has any ideas.
- Anonymous7 years agoNot applicable
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