Forum Discussion

AUEng_403's avatar
AUEng_403
Regular Visitor
4 years ago
Solved

Measure with an if statement

Hello, 

 

First time posting in the forum, apologies if this has been answered, I'm not even quite sure what I'm doing wrong to be able to look up my problem. 

 

 I have two fact tables - Planned and Actuals, each containing a field called TOTAL PLANNED TONNAGE and TOTAL Actual TONNAGE respectively. I would like to compare the two to see what was done "In Plan" by Workplace and Month for a variety of different plans.  

 

 My DAX code for the InPlan measure is below. It functions correctly at the Workplace Row level, but when I look at the month, it's using the totals for the month to do the comparison, and I somehow need it to still compare it based on Workplace. So for the shown table, I'm expecting the result to be 6,220 for January (sum of 1150, 70, 5000), not 9,500. 

 

 I believe what I need to do is somehow force the filter by WORKPLACE somehow, but I just can't wrap my head around this yet. 

 

Report Table: 

 

Relationships

 

DAX code for InPlan: 

 

InPlan =
IF (
    OR (
        CALCULATE (
            SUM ( Plans[TOTAL PLANNED TONNAGE] ),
            ALLEXCEPT (
                Plans,
                'Calendar'[Month Name],
                Dim_Workplaces[WORKPLACE],
                Plans[Plan#]
            )
        ) = 0,
        CALCULATE (
            SUM ( Actuals[TOTAL Actual TONNAGE] ),
            ALLEXCEPT ( Actuals, 'Calendar'[Month Name], Dim_Workplaces[WORKPLACE] )
        ) = 0
    ),
    0,
    IF (
        CALCULATE (
            SUM ( Plans[TOTAL PLANNED TONNAGE] ),
            ALLEXCEPT (
                Plans,
                'Calendar'[Month Name],
                Dim_Workplaces[WORKPLACE],
                Plans[Plan#]
            )
        )
            >= CALCULATE (
                SUM ( Actuals[TOTAL Actual TONNAGE] ),
                ALLEXCEPT ( Actuals, 'Calendar'[Month Name], Dim_Workplaces[WORKPLACE] )
            ),
        CALCULATE (
            SUM ( Actuals[TOTAL Actual TONNAGE] ),
            ALLEXCEPT ( Actuals, 'Calendar'[Month Name], Dim_Workplaces[WORKPLACE] )
        ),
        CALCULATE (
            SUM ( Plans[TOTAL PLANNED TONNAGE] ),
            ALLEXCEPT (
                Plans,
                'Calendar'[Month Name],
                Dim_Workplaces[WORKPLACE],
                Plans[Plan#]
            )
        )
    )
)

 

A simplified view of the calculation is this: 

 

if (or (Plan Tonnage = 0, Actual Tonnage = 0), 0 , if (Plan Tonnage >= Actual Tonnage, Actual Tonnage, Plan Tonnage) 

 

  Any help is greatly appreciated!

 

 

 

  • AUEng_403,

     

    Try these measures. CROSSJOIN creates every combination of Date and Workplace in the filter context. Then, ADDCOLUMNS calculates the Plan Tonnage and Actual Tonnage for each combination of Date and Workplace, and SUMX sums them. This forces the total to equal the sum of its parts.

     

    Plan Tonnage = SUM (Plans[TOTAL PLANNED TONNAGE] )
    Actual Tonnage = SUM ( Actuals[TOTAL Actual TONNAGE] )
    InPlan = 
    VAR vBaseTable =
        CROSSJOIN ( VALUES ( Calendar[Date] ), VALUES ( Dim_Workplaces[WORKPLACE] ) )
    VAR vCalcTable =
        ADDCOLUMNS ( vBaseTable, "@Plan", [Plan Tonnage], "@Actual", [Actual Tonnage] )
    VAR vResult =
        SUMX (
            vCalcTable,
            SWITCH (
                TRUE,
                [@Plan] = 0 || [@Actual] = 0, 0,
                [@Plan] >= [@Actual], [@Actual],
                [@Plan]
            )
        )
    RETURN
        vResult

     

     

1 Reply

  • AUEng_403,

     

    Try these measures. CROSSJOIN creates every combination of Date and Workplace in the filter context. Then, ADDCOLUMNS calculates the Plan Tonnage and Actual Tonnage for each combination of Date and Workplace, and SUMX sums them. This forces the total to equal the sum of its parts.

     

    Plan Tonnage = SUM (Plans[TOTAL PLANNED TONNAGE] )
    Actual Tonnage = SUM ( Actuals[TOTAL Actual TONNAGE] )
    InPlan = 
    VAR vBaseTable =
        CROSSJOIN ( VALUES ( Calendar[Date] ), VALUES ( Dim_Workplaces[WORKPLACE] ) )
    VAR vCalcTable =
        ADDCOLUMNS ( vBaseTable, "@Plan", [Plan Tonnage], "@Actual", [Actual Tonnage] )
    VAR vResult =
        SUMX (
            vCalcTable,
            SWITCH (
                TRUE,
                [@Plan] = 0 || [@Actual] = 0, 0,
                [@Plan] >= [@Actual], [@Actual],
                [@Plan]
            )
        )
    RETURN
        vResult