Forum Discussion
Dynamically Sum between two datasets with multiple criteria
- 4 years ago
Hi mattwoldt ,
Please take note that there is an overlap in your second table - 202106 is both the end period of Year 1 and start of Year 2.
To make the calcuation simple, I created several columns to help with the calculations.
Table1
Revenue YrMo = VALUE ( Table1[Revenue Year] & FORMAT ( Table1[Revenue Month], "00" ) )Table2
Start YrMo = VALUE ( Table2[Start Year] & FORMAT ( Table2[Start Month], "00" ) )End YrMo = VALUE ( Table2[End Year] & FORMAT ( Table2[End Month], "00" ) )For the revenue, you can either use a calculated column or a measure approach.
Calc column in table2
Revenue Sum (Calculated Column) = CALCULATE ( SUM ( Table1[Revenue] ), FILTER ( Table1, Table1[Revenue YrMo] >= EARLIER ( Table2[Start YrMo] ) && Table1[Revenue YrMo] <= EARLIER ( Table2[End YrMo] ) ), Table1[ID] = EARLIER ( Table2[ID] ) )Measure
Revenue Sum (Measure) = SUMX ( ADDCOLUMNS ( SUMMARIZE ( Table2, Table2[ID], Table2[Start YrMo], Table2[End YrMo] ), "x", CALCULATE ( SUM ( Table1[Revenue] ), FILTER ( Table1, Table1[Revenue YrMo] >= [Start YrMo] && Table1[Revenue YrMo] <= [End YrMo] && Table1[ID] = [ID] ) ) ), [x] )Those columns/measure would result to this:
Please refer to this link for the sample pbix - https://drive.google.com/file/d/1_Iv8UAqk58yxQOEDsLFdS7l3fgjxIUOh/view?usp=sharing
Sorry, re-edited my post, didn't realize I had some typos. Essentially I am wanting to do a dynamic sum if that looks at the rows year and month. Essentially it is sum revenue that is equal to the start year and greater than or equal to the month # in the revenue table + revenue that is equal to the end year and less than or equal to the end month #.
Hi mattwoldt ,
Please take note that there is an overlap in your second table - 202106 is both the end period of Year 1 and start of Year 2.
To make the calcuation simple, I created several columns to help with the calculations.
Table1
Revenue YrMo =
VALUE ( Table1[Revenue Year] & FORMAT ( Table1[Revenue Month], "00" ) )Table2
Start YrMo =
VALUE ( Table2[Start Year] & FORMAT ( Table2[Start Month], "00" ) )End YrMo =
VALUE ( Table2[End Year] & FORMAT ( Table2[End Month], "00" ) )
For the revenue, you can either use a calculated column or a measure approach.
Calc column in table2
Revenue Sum (Calculated Column) =
CALCULATE (
SUM ( Table1[Revenue] ),
FILTER (
Table1,
Table1[Revenue YrMo] >= EARLIER ( Table2[Start YrMo] )
&& Table1[Revenue YrMo] <= EARLIER ( Table2[End YrMo] )
),
Table1[ID] = EARLIER ( Table2[ID] )
)Measure
Revenue Sum (Measure) =
SUMX (
ADDCOLUMNS (
SUMMARIZE ( Table2, Table2[ID], Table2[Start YrMo], Table2[End YrMo] ),
"x",
CALCULATE (
SUM ( Table1[Revenue] ),
FILTER (
Table1,
Table1[Revenue YrMo] >= [Start YrMo]
&& Table1[Revenue YrMo] <= [End YrMo]
&& Table1[ID] = [ID]
)
)
),
[x]
)
Those columns/measure would result to this:
Please refer to this link for the sample pbix - https://drive.google.com/file/d/1_Iv8UAqk58yxQOEDsLFdS7l3fgjxIUOh/view?usp=sharing
- mattwoldt4 years agoFrequent Visitor
Wow!
I am absolutely amazed that is incredible. It 100% works. Thank you so much! So I guess the filter function with an earlier is a way to do a dynamic sum if it seems? Just absolutely incredible.
Thank you so much. - mattwoldt4 years agoFrequent Visitor
Well I may have spoken a little too soon. I am getting calculations for some but for others it shows up as blank, any idea why?
I checked the other data set the id's are definitely there and the ID's have revenue between those ranges.
I'm not sure if I got it or not, but I tried trimming text and that seems to have fixed it so I think I am all good now. - danextian4 years agoSuper User
I'm glad that worked.
EARLIER returns the value of a row from a column. If the second argument is not specified, that defaults to the current row.