Forum Discussion
Sandertjuh
6 years agoFrequent Visitor
Sum per year for date range
Hello, Im stuck on getting the Sum of PricePerMonth for a Name in a year. Name StartDate EndDate PricePerMonth ABC 01/01/2019 30/06/2019 500 ABC 01/07/2019 30/06/2020 600 ...
Sandertjuh
6 years agoFrequent Visitor
Created a one-column table Years with a Year column. When i try to setup the measure like you provide i get the message that i dont select a single value for the year, im selecting the column as in your example.
And there is something strange that the End_ variable is not found in the return section as 2nd param.
AlB wrote:Create a one column-table witht the years Aux[Year] and place it in the visual too. Then something along these lines:
Measure = SUMX ( Table1, Table1[Price] * ( DATEDIFF ( VAR Start_ = IF ( Aux[Year] < YEAR ( Table1[StartDate] ), DATE ( YEAR ( Table1[StartDate] ), 1, 1 ), Table1[StartDate] ) VAR End_ = IF ( Aux[Year] > YEAR ( Table1[EndDate] ), DATE ( YEAR ( Table1[EndDate] ), 12, 31 ), Table1[EndDate] ) RETURN Start_, End_, MONTH ) + 1 ) )
Please mark the question solved when done and consider giving kudos if posts are helpful.
Cheers
AlB
6 years agoCommunity Champion
Measure =
SUMX (
Table1,
Table1[Price]
* (
DATEDIFF (
VAR Start_ =
IF (
SELECTEDVALUE(Aux[Year]) < YEAR ( Table1[StartDate] ),
DATE ( YEAR ( Table1[StartDate] ), 1, 1 ),
Table1[StartDate]
)
VAR End_ =
IF (
SELECTEDVALUE( Aux[Year] ) > YEAR ( Table1[EndDate] ),
DATE ( YEAR ( Table1[EndDate] ), 12, 31 ),
Table1[EndDate]
)
RETURN
Start_,
End_,
MONTH
) + 1
)
)- Sandertjuh6 years agoFrequent Visitor
The 2nd param is not working in this case, the second param can't find the var Start_ or End_. As first param in DATEDIFF i can see both the variable.
AlB wrote:Measure = SUMX ( Table1, Table1[Price] * ( DATEDIFF ( VAR Start_ = IF ( SELECTEDVALUE(Aux[Year]) < YEAR ( Table1[StartDate] ), DATE ( YEAR ( Table1[StartDate] ), 1, 1 ), Table1[StartDate] ) VAR End_ = IF ( SELECTEDVALUE( Aux[Year] ) > YEAR ( Table1[EndDate] ), DATE ( YEAR ( Table1[EndDate] ), 12, 31 ), Table1[EndDate] ) RETURN Start_, End_, MONTH ) + 1 ) )