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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
zahirr
Regular Visitor

Dealing with Measures within measures

I want to achieve the following logic

zahirr_1-1650980318043.png

 

 

I have achieved the GREEN part, but struggling with the RED part.
My Dax is as follows -

 

=IF(SUM ( 'MasterTbl'[Sett] ) = 1,

                                SUMX (
                (IF (
            SUM ( 'MasterTbl'[Sett] ) = 0,
            CALCULATE (
                SUM ( 'MasterTbl'[Total number of beneficiaries] ),
                ALLEXCEPT ( MasterTbl, 'MasterTbl'[Activities and Indicators.Activities] )
            ) )),

                 IF

            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            )) )) >= SUM('Settlement name'[population]), SUM('Settlement name'[population])),
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            ) ) ) ) ),

                            (   IF (
            SUM ( 'MasterTbl'[Sett] ) = 0,
            CALCULATE (
                SUM ( 'MasterTbl'[Total number of beneficiaries] ),
                ALLEXCEPT ( MasterTbl, 'MasterTbl'[Activities and Indicators.Activities] )
            ),
    IF
        (
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            )
                        )
                    )
            ) >= SUM('Settlement name'[population]), SUM('Settlement name'[population]),
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            ) )  ) ) )  )))

 

 

 

 

1 REPLY 1
v-kkf-msft
Community Support
Community Support

Hi @zahirr ,

 

I can't understand what you mean, please share your sample data and expected output.


Also, I formatted your formula but not sure if it is consistent with yours, please check my sample data and output.

 

Measure = 
VAR _categoryfirst =
    CALCULATETABLE (
        ADDCOLUMNS (
            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
            "MaxValue",
                CALCULATE (
                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                )
        ),
        ALL ( 'MasterTbl'[Total number of beneficiaries] )
    )
VAR max_beneficiary =
    CALCULATE (
        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
        KEEPFILTERS (
            TREATAS (
                _categoryfirst,
                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                'MasterTbl'[Total number of beneficiaries]
            )
        )
    )
VAR Sum_ =
    CALCULATE (
        SUM ( 'MasterTbl'[Total number of beneficiaries] ),
        ALLEXCEPT ( MasterTbl, 'MasterTbl'[Activities and Indicators.Activities] )
    )
RETURN
    IF (
        SUM ( 'MasterTbl'[Sett] ) = 1,
        SUM ( 'MasterTbl'[Total number of beneficiaries] ),
        IF (
            SUM ( 'MasterTbl'[Sett] ) = 0,
            MIN ( max_beneficiary, SUM ( 'Settlement name'[population] ) )
        )
    )

vkkfmsft_0-1651200504052.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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