Forum Discussion
Month/Day Variable within DATESINPERIOD?
- 1 year ago
Hi ptmuldoon
1. The 4th argument of DATESINPERIOD must be one of the keywords (DAY, MONTH, QUARTER, YEAR) but cannot be any other expression. In fact, there is no way to return one of these four keywords with any other expression.
2. The functions IF or SWITCH cannot be used to return tables. They can only return scalar values.
However, a different approach to produce a "conditional" table is to FILTER each possible table with a boolean expression that is true/false per table, and take the union.For example, you could write:
VAR APICallValue = SELECTEDVALUE ( APICalls[APICall] ) VAR FilterDay = FILTER ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, DAY ), APICallValue = "Daily" ) VAR FilterMonth = FILTER ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, MONTH ), APICallValue = "Monthly" ) VAR FilterSelected = UNION ( FilterDay, FilterMonth ) // only one of the two tables is non-empty RETURN CALCULATE ( [Amt], REMOVEFILTERS ( 'Dim Period' ), KEEPFILTERS ( FilterSelected ), USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] ) )Alternatively, if you want to use SWITCH, you would need to restate the CALCULATE expressions, with the only difference being the 4th argument of DATESINPERIOD.
SWITCH ( SELECTEDVALUE ( APICalls[APICall] ), "Daily", CALCULATE ( [Amt], REMOVEFILTERS ( 'Dim Period' ), KEEPFILTERS ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, DAY ) ), USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] ) ), "Monthly", CALCULATE ( [Amt], REMOVEFILTERS ( 'Dim Period' ), KEEPFILTERS ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, MONTH ) ), USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] ) ) )(You could create separate Daily/Monthly measures and reference them here but the structure remains the same.)
Do any of those ideas help?
Regards
Hi ptmuldoon
1. The 4th argument of DATESINPERIOD must be one of the keywords (DAY, MONTH, QUARTER, YEAR) but cannot be any other expression. In fact, there is no way to return one of these four keywords with any other expression.
2. The functions IF or SWITCH cannot be used to return tables. They can only return scalar values.
However, a different approach to produce a "conditional" table is to FILTER each possible table with a boolean expression that is true/false per table, and take the union.
For example, you could write:
VAR APICallValue =
SELECTEDVALUE ( APICalls[APICall] )
VAR FilterDay =
FILTER (
DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, DAY ),
APICallValue = "Daily"
)
VAR FilterMonth =
FILTER (
DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, MONTH ),
APICallValue = "Monthly"
)
VAR FilterSelected =
UNION ( FilterDay, FilterMonth ) // only one of the two tables is non-empty
RETURN
CALCULATE (
[Amt],
REMOVEFILTERS ( 'Dim Period' ),
KEEPFILTERS ( FilterSelected ),
USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] )
)
Alternatively, if you want to use SWITCH, you would need to restate the CALCULATE expressions, with the only difference being the 4th argument of DATESINPERIOD.
SWITCH (
SELECTEDVALUE ( APICalls[APICall] ),
"Daily",
CALCULATE (
[Amt],
REMOVEFILTERS ( 'Dim Period' ),
KEEPFILTERS ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, DAY ) ),
USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] )
),
"Monthly",
CALCULATE (
[Amt],
REMOVEFILTERS ( 'Dim Period' ),
KEEPFILTERS ( DATESINPERIOD ( 'Date Table'[Date], maxDate, - Number, MONTH ) ),
USERELATIONSHIP ( 'Dim Period'[Date], 'Date Table'[Date] )
)
)
(You could create separate Daily/Monthly measures and reference them here but the structure remains the same.)
Do any of those ideas help?
Regards
Thank you for your help!!. And after some thought last night, I had worked out and came up with the same thing in your second solution as the work around. But I may try that first solution as well to try and keep the Calcuate function a little cleaner.