Forum Discussion
Dynamic Age Calculation Based on User Selected Cutoff Date
- 8 years ago
Hi Anonymous
Unfortunately as Yuliana mentioned, calculated columns don't respond to any filters within the report. Calculated columns are populated at report refresh, in an 'unfiltered' filter context.
For what you're trying to do, you would need to use something like a Dynamic Segmentation pattern (see DAX Patterns page).
- Create an 'Age' table containing all possible Ages you might want to filter on.
- Include the Age[Age] column in your visual.
- Create a Member Count By Age measure following a Dynamic Segmentation type pattern:
Member Count By Age = IF ( ISFILTERED ( Age[Age] ), VAR SelectedCutoff = MAX ( Cutoff[cutoff_date] ) RETURN CALCULATE ( COUNTROWS ( 'Member' ), FILTER ( VALUES ( 'Member'[date_of_birth] ), VAR AgeCalculated = IF ( 'Member'[date_of_birth] <= SelectedCutoff, TRUNC ( YEARFRAC ( 'Member'[date_of_birth], SelectedCutoff ) ) ) RETURN CONTAINS ( VALUES ( Age[Age] ), Age[Age], AgeCalculated ) ) ), COUNTROWS ( 'Member' ) )(I modified the pattern slightly)
Note that this measure will return a simple count of Members if no Ages are filtered on.
Also, ages are only computed if date_of_birth <= Selected cutoff_date.
Here is a sample pbix demonstrating this.
https://www.dropbox.com/s/5p1faipgycwz5zo/Age%20Dynamic%20Segmentation.pbix?dl=1
Regards,
Owen
Still struggling with this; in a nutshell what I think I need is a way to create a calculated column in a table that will respond to a selected filter context from the report page.
I've tried numerous ways (admitedly blindly) and can't seem to get there (none of the tries below worked):
age_at_date_selected_trial1 =
CALCULATE (
IF (
NOT ( ISBLANK ( MAX ( 'Member'[date_of_birth] ) ) ),
TRUNC (
YEARFRAC (
MAX ( 'Member'[date_of_birth] ),
SELECTEDVALUE ( 'Cutoff'[cutoff_date] )
)
)
)
)
age_at_date_selected_trial2 =
CALCULATE (
IF (
NOT ( ISBLANK ( MAX ( 'Member'[date_of_birth] ) ) )
&& HASONEVALUE ( 'Cutoff'[cutoff_date] ),
TRUNC (
YEARFRAC (
MAX ( 'Member'[date_of_birth] ),
SELECTEDVALUE ( 'Cutoff'[cutoff_date] )
)
)
)
)age_at_date_selected_trial3 =
CALCULATE (
IF (
NOT ( ISBLANK ( MAX ( 'Member'[date_of_birth] ) ) )
&& HASONEVALUE ( 'Cutoff'[cutoff_date] ),
TRUNC (
YEARFRAC ( MAX ( 'Member'[date_of_birth] ), VALUES ( 'Cutoff'[cutoff_date] ) )
)
)
)If anyone can nudge me in the right direction I'd be really grateful.
Hi Anonymous
Unfortunately as Yuliana mentioned, calculated columns don't respond to any filters within the report. Calculated columns are populated at report refresh, in an 'unfiltered' filter context.
For what you're trying to do, you would need to use something like a Dynamic Segmentation pattern (see DAX Patterns page).
- Create an 'Age' table containing all possible Ages you might want to filter on.
- Include the Age[Age] column in your visual.
- Create a Member Count By Age measure following a Dynamic Segmentation type pattern:
Member Count By Age = IF ( ISFILTERED ( Age[Age] ), VAR SelectedCutoff = MAX ( Cutoff[cutoff_date] ) RETURN CALCULATE ( COUNTROWS ( 'Member' ), FILTER ( VALUES ( 'Member'[date_of_birth] ), VAR AgeCalculated = IF ( 'Member'[date_of_birth] <= SelectedCutoff, TRUNC ( YEARFRAC ( 'Member'[date_of_birth], SelectedCutoff ) ) ) RETURN CONTAINS ( VALUES ( Age[Age] ), Age[Age], AgeCalculated ) ) ), COUNTROWS ( 'Member' ) )(I modified the pattern slightly)
Note that this measure will return a simple count of Members if no Ages are filtered on.
Also, ages are only computed if date_of_birth <= Selected cutoff_date.
Here is a sample pbix demonstrating this.
https://www.dropbox.com/s/5p1faipgycwz5zo/Age%20Dynamic%20Segmentation.pbix?dl=1
Regards,
Owen