Forum Discussion
Syndicate_Admin
3 years agoAdministrator
Dax problem
Dear good afternoon I want to ask for your help to be able to perform the following measure or generacy to achieve the following I have the following data enterprise Date ini End date Monthl...
MFelix
3 years agoSuper User
Hi mavn23061997 ,
I assume the years table is separated from the other table, create the following measure:
Values per Month =
VAR temptable =
ADDCOLUMNS (
FILTER (
ADDCOLUMNS (
CROSSJOIN (
'Years_Table',
ADDCOLUMNS (
'DataValues',
"StartYear", YEAR ( 'DataValues'[Date ini] ),
"EndYear", YEAR ( 'DataValues'[End date] )
)
),
"Flag",
IF ( [StartYear] <= [Years] && [EndYear] >= [Years], 1, 0 )
),
[Flag] = 1
),
"TotalMonths",
SWITCH (
TRUE (),
[StartYear] = [Years],
DATEDIFF (
'DataValues'[Date ini],
MIN ( DATE ( [years], 12, 31 ), 'DataValues'[End date] ),
MONTH
) + 1,
[StartYear] > [Years], DATEDIFF ( DATE ( YEAR ( [StartYear] ), 12, 31 ) + 1, 'DataValues'[Date ini], MONTH ),
[StartYear] < [Years]
&& [EndYear] > [Years], DATEDIFF ( DATE ( [Years], 1, 1 ), DATE ( [Years], 12, 31 ), MONTH ) + 1,
[EndYear] = [Years], DATEDIFF ( DATE ( [years], 1, 1 ), 'DataValues'[End date], MONTH ) + 1
)
)
RETURN
SUMX ( tempTable, [TotalMonths] * 'DataValues'[Monthly Value] )
Now just use this measure with the years column:
Syndicate_Admin
3 years agoAdministrator
I just tried it and it works, just out of curiosity if in case I had a calendar table related to start date and end date, how could the same result be achieved
- MFelix3 years agoSuper User
This depends on how they are related.
What is the active relationship?
- Syndicate_Admin3 years agoAdministrator
Start date
- MFelix3 years agoSuper User
Try the following formula:
Values per Month = VAR temptable = ADDCOLUMNS ( FILTER ( ADDCOLUMNS ( CROSSJOIN ( DISTINCT('Calendar'[Year]), ADDCOLUMNS ( 'DataValues', "StartYear", YEAR ( 'DataValues'[Date ini] ), "EndYear", YEAR ( 'DataValues'[End date] ) ) ), "Flag", IF ( [StartYear] <= [Year] && [EndYear] >= [Year], 1, 0 ) ), [Flag] = 1 ), "TotalMonths", SWITCH ( TRUE (), [StartYear] = [Year], DATEDIFF ( 'DataValues'[Date ini], MIN ( DATE ( [year], 12, 31 ), 'DataValues'[End date] ), MONTH ) + 1, [StartYear] > [Year], DATEDIFF ( DATE ( YEAR ( [StartYear] ), 12, 31 ) + 1, 'DataValues'[Date ini], MONTH ), [StartYear] < [Year] && [EndYear] > [Year], DATEDIFF ( DATE ( [Year], 1, 1 ), DATE ( [Year], 12, 31 ), MONTH ) + 1, [EndYear] = [Year], DATEDIFF ( DATE ( [year], 1, 1 ), 'DataValues'[End date], MONTH ) + 1 ) ) RETURN SUMX ( tempTable, [TotalMonths] * 'DataValues'[Monthly Value] )Don't forget to accept the correct answer to help others.