Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
MariekevdAkker
Frequent Visitor

Refer to calculated table

I use a live connection to a SSAS Tabular Model. Therefor I cannot use calculated columns.


The table factDataExport contains a row for each answer in a form. 
Each participant can have multiple baseline forms with a visit_date. The measure below calculates the number of baselines in each age group. Agegroups are defined in the AddCategories table. 

In the measure below I would like to replace TODAY() with the birth_year of the particpiant. These are available in the 'Birth_year_table' and can be related to the factDataExport by [Record_Name_GUID]. However I can't get the measure to refer to the Birth_Year_table. 

I need the age of the participant as "visit_date" - "Birth_Year" wich are both stored on different rows in the same (factDataExport) table. 

AgeGroups BL = 


var VarCode = SELECTEDVALUE('AddCategories'[QNAME])
var MinAge = MIN('AddCategories'[From])
var MaxAge = MIN('AddCategories'[To])


VAR BL_Form_Instance_ID =          
    CALCULATETABLE(
        VALUES(factDataExport[Form_Instance_ID])
        ,ALL(dimMetadata)
        ,dimMetadata[Field_Variable_Name]="Visit_Type"
        ,factDataExport[AnswerNumeric]=0 --=BL visit
        ,factDataExport[Report_Status] <> BLANK()
    )
  

VAR Birth_Year_table =
		ADDCOLUMNS(
            //Participant IDs
			CALCULATETABLE(VALUES(factDataExport[Record_Name_GUID]))
            // Birth_Year
			,"Birth_Year"
			,CALCULATE(
                MAX(factDataExport[AnswerNumeric])
                ,dimMetadata[Field_Variable_Name]="Birth_Year"
                ,ALL(dimMetadata)
                ,factDataExport[AnswerNumeric] <> BLANK()
            )
		)


Return
    CALCULATE(
        COUNTROWS(factDataExport)
        ,dimMetadata[Field_Variable_Name] = "Visit_Date"
        ,ALL(dimMetadata)
        ,TREATAS(BL_Form_Instance_ID,factDataExport[Form_Instance_ID])  --BL Visits
        ,INT ( YEARFRAC ( TODAY(), factDataExport[AnswerDate], 1 ) ) > MinAge
        ,INT ( YEARFRAC ( TODAY(), factDataExport[AnswerDate], 1 ) ) < MaxAge
        
    )
 


 

 

1 ACCEPTED SOLUTION

Hi @Anonymous,

This is long overdue but I finally found the solution. 


The measure that eventually worked is:

 

 

AGE GROUPS per BIODAYMED = 
VAR x =
CALCULATETABLE (
        ADDCOLUMNS(
            SUMMARIZE (
                factAnswer,
                factAnswer[Participant_GUID],
                factAnswer[Form_Instance_ID]
            )
            ,"BirthYear",
                    DATE ( CALCULATE (
                        MIN ( factAnswer[AnswerNumeric] ),
                        dimMetadata[Field_Variable_Name] = "Birth_Year",
                        ALL ( factAnswer[Form_Instance_ID] )
                    ), 1, 1 ),
                "VisitDate",
                    CALCULATE (
                        MIN ( factAnswer[AnswerDateDate] ),
                        dimMetadata[Field_Variable_Name] = "Visit_Date"
                    ),
                "VisitType",
                    CALCULATE (
                        MIN ( factAnswer[Answer] ),
                        dimMetadata[Field_Variable_Name] = "Visit_Type"
                    ),
                "AgeAtVisit",
                    DATEDIFF (
                        DATE ( CALCULATE (
                            MIN ( factAnswer[AnswerNumeric] ),
                            dimMetadata[Field_Variable_Name] = "Birth_Year",
                            ALL ( factAnswer[Form_Instance_ID] )
                        ), 1, 1 ),
                        CALCULATE (
                            MIN ( factAnswer[AnswerDateDate] ),
                            dimMetadata[Field_Variable_Name] = "Visit_Date"
                        ),
                        YEAR
                    ),
                "Medication",
                    CALCULATE (
                        MIN ( factAnswer[Answer] ),
                        dimMetadata[Field_Variable_Name] = "BioDayMed_Type"
                    )
            )
            
            )


    VAR MinAge = SELECTEDVALUE(AddCategories[From],0.01)
    VAR MaxAge = SELECTEDVALUE(AddCategories[To],150)
    VAR Med = CONVERT(SELECTEDVALUE(WhatIf[Value]),STRING)
    VAR VisitType = "0"

    VAR CountMedAgeGroup = COUNTX (
            FILTER (x, [AgeAtVisit]>=MinAge && [AgeAtVisit]<=MaxAge && [Medication] = Med && [VisitType] = VisitType ),
            [Form_Instance_ID]
        )

   
   VAR NoSelectedMed = COUNTX (
            FILTER (x, [AgeAtVisit]>=MinAge && [AgeAtVisit]<=MaxAge && [VisitType] = VisitType ),
            [Form_Instance_ID]
        )

        
    RETURN
IF(Med=BLANK(), NoSelectedMed, CountMedAgeGroup)

 

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @MariekevdAkker ,

You can update the formula of measure [AgeGroups BL] as below and check if it can return your expected result...

AgeGroups BL =
VAR VarCode =
    SELECTEDVALUE ( 'AddCategories'[QNAME] )
VAR MinAge =
    MIN ( 'AddCategories'[From] )
VAR MaxAge =
    MIN ( 'AddCategories'[To] )
VAR BL_Form_Instance_ID =
    CALCULATETABLE (
        VALUES ( factDataExport[Form_Instance_ID] ),
        ALL ( dimMetadata ),
        dimMetadata[Field_Variable_Name] = "Visit_Type",
        factDataExport[AnswerNumeric] = 0 --=BL visit
        ,
        factDataExport[Report_Status] <> BLANK ()
    )
VAR Birth_Year_table =
    ADDCOLUMNS (
        //Participant IDs
        CALCULATETABLE ( VALUES ( factDataExport[Record_Name_GUID] ) ) // Birth_Year
        ,
        "Birth_Year",
            CALCULATE (
                MAX ( factDataExport[AnswerNumeric] ),
                dimMetadata[Field_Variable_Name] = "Birth_Year",
                ALL ( dimMetadata ),
                factDataExport[AnswerNumeric] <> BLANK ()
            )
    )
RETURN
    CALCULATE (
        COUNTROWS ( factDataExport ),
        dimMetadata[Field_Variable_Name] = "Visit_Date",
        ALL ( dimMetadata ),
        TREATAS ( BL_Form_Instance_ID, factDataExport[Form_Instance_ID] ) --BL Visits
        ,
        INT (
            YEARFRAC (
                MAXX (
                    FILTER (
                        Birth_Year_table,
                        [Record_Name_GUID] = factDataExport[Record_Name_GUID]
                    ),
                    [Birth_Year]
                ),
                factDataExport[AnswerDate],
                1
            )
        ) > MinAge,
        INT (
            YEARFRAC (
                MAXX (
                    FILTER (
                        Birth_Year_table,
                        [Record_Name_GUID] = factDataExport[Record_Name_GUID]
                    ),
                    [Birth_Year]
                ),
                factDataExport[AnswerDate],
                1
            )
        ) < MaxAge
    )

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Thank you for your answer, I get the following error:
"Calculation error in measure 'MeasureTable'[AgeGroups BL]: A function 'MAXX' has been used in a True/False expression that is used as a table filter expression. This is not allowed."

@Anonymous I have uploaded a pbx here: Dropbox 

Anonymous
Not applicable

Hi @MariekevdAkker ,

I tried to update the formula of the measure [AgeGroups BL] using the formula in my previous post, it works welll. Please find the details in the screenshot below. Is there anything else I missing here?

yingyinr_0-1663730566252.png

Best Regards

Hi @Anonymous,

This is long overdue but I finally found the solution. 


The measure that eventually worked is:

 

 

AGE GROUPS per BIODAYMED = 
VAR x =
CALCULATETABLE (
        ADDCOLUMNS(
            SUMMARIZE (
                factAnswer,
                factAnswer[Participant_GUID],
                factAnswer[Form_Instance_ID]
            )
            ,"BirthYear",
                    DATE ( CALCULATE (
                        MIN ( factAnswer[AnswerNumeric] ),
                        dimMetadata[Field_Variable_Name] = "Birth_Year",
                        ALL ( factAnswer[Form_Instance_ID] )
                    ), 1, 1 ),
                "VisitDate",
                    CALCULATE (
                        MIN ( factAnswer[AnswerDateDate] ),
                        dimMetadata[Field_Variable_Name] = "Visit_Date"
                    ),
                "VisitType",
                    CALCULATE (
                        MIN ( factAnswer[Answer] ),
                        dimMetadata[Field_Variable_Name] = "Visit_Type"
                    ),
                "AgeAtVisit",
                    DATEDIFF (
                        DATE ( CALCULATE (
                            MIN ( factAnswer[AnswerNumeric] ),
                            dimMetadata[Field_Variable_Name] = "Birth_Year",
                            ALL ( factAnswer[Form_Instance_ID] )
                        ), 1, 1 ),
                        CALCULATE (
                            MIN ( factAnswer[AnswerDateDate] ),
                            dimMetadata[Field_Variable_Name] = "Visit_Date"
                        ),
                        YEAR
                    ),
                "Medication",
                    CALCULATE (
                        MIN ( factAnswer[Answer] ),
                        dimMetadata[Field_Variable_Name] = "BioDayMed_Type"
                    )
            )
            
            )


    VAR MinAge = SELECTEDVALUE(AddCategories[From],0.01)
    VAR MaxAge = SELECTEDVALUE(AddCategories[To],150)
    VAR Med = CONVERT(SELECTEDVALUE(WhatIf[Value]),STRING)
    VAR VisitType = "0"

    VAR CountMedAgeGroup = COUNTX (
            FILTER (x, [AgeAtVisit]>=MinAge && [AgeAtVisit]<=MaxAge && [Medication] = Med && [VisitType] = VisitType ),
            [Form_Instance_ID]
        )

   
   VAR NoSelectedMed = COUNTX (
            FILTER (x, [AgeAtVisit]>=MinAge && [AgeAtVisit]<=MaxAge && [VisitType] = VisitType ),
            [Form_Instance_ID]
        )

        
    RETURN
IF(Med=BLANK(), NoSelectedMed, CountMedAgeGroup)

 

Anonymous
Not applicable

Hi @Marieke2 ,

It's glad to hear that your problem has been resolved. And thanks for sharing your solution here. Could you please mark your post as Answered? It will help the others in the community find the solution easily if they face the same problem as yours.  Thank you.

Best Regards

Hi @Anonymous ,

 

Thank you again for your time to help me. 
In the example dataset I get the same results as you, however it should not reply only adults (18+), there are children in the selection. It apears to ignore the filter and uses the overall MAX of all the birth_years.

Furthermore, If I use the exact same measure on my actual model which has a live connection to an analysis server I get the following error:
"Calculation error in measure 'MeasureTable'[AgeGroups BL]: A function 'MAXX' has been used in a True/False expression that is used as a table filter expression. This is not allowed."

I can also generate the error in the example dataset by adding a calculate around the MAXX function. 

Kind Regards,

Marieke

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.