Forum Discussion
Sum all values per row until limit is reached with multiple criteria
Hi Anonymous,
a file with some example data would really be helpful
sturlaws - Here is the example data for both power bi and excel from my screenshot. The simplest example of the scenario as already shown if you stay filtered on that account ID, Product Category, and Contract Number but I need this calculation to work without filtering the data.
Thanks so much for the help!
https://drive.google.com/open?id=1kPW5eAEieDxl5_rZydw0bB-2yW1936gi
https://drive.google.com/open?id=15bAu-qsKEmeGMNZSpHzFCNLVcg9lSkUc
- sturlaws7 years ago
Resident Rockstar
A bit tricky this.
How do you want to visualize the 12 month revenue? Is it just latest date pr contract, or do you want to track the development over time?
Or do you want to show it row by row like you have shown in you example?- Anonymous7 years agoNot applicable
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.
- sturlaws7 years ago
Resident Rockstar
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 asRevenue 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] ) ) )