Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Dynamic Month1 and 2 calculations

Hi All,   I'm stuck on a calculated measure I'm trying to create and would need help here. Problem Breakdown:  On the last day of the quarter/Last day of the month, we need to ensure that Month...
  • Anonymous's avatar
    Anonymous
    1 year ago

    v-csrikanth Thanks for trying but I managed to achieve the desired result myself using the below approach.

    The measure you shared wont work because of the flag condition. Suppose I want Month1 last day's data to be seen when I'm using the flah "Is_Last_Day_of_Fiscal_Quarter" but since that day doesn't fall under the last fiscal month of quarter the measure you tried wont work.

    I created a seperate flag table using the below DAX and altered my measure to use this flag table. 

    Let me know in case any further help/clarification is needed

    Flag_Table = DATATABLE("Flag", BOOLEAN, { { TRUE }, { FALSE } })



    This way I'm able to achieve the desired result.

    Month1_CW =
    VAR SelectedFlag = SELECTEDVALUE(Flag_Table[Flag], FALSE)
    VAR CurrentDS = SELECTEDVALUE(VW_F_PIPELINE_HISTORY[DS])
    VAR CurrentMonth = SELECTEDVALUE(VW_F_PIPELINE_HISTORY[Month])
    VAR CurrentQuarter = SELECTEDVALUE(VW_F_PIPELINE_HISTORY[FISCAL_YEAR_QUARTER])
    VAR FiscalYear = SELECTEDVALUE(VW_F_PIPELINE_HISTORY[Fiscal Year])

    -- 1. Check if current DS is a quarter-end date
    VAR IsQuarterEndDate =
        CALCULATE(
            MAXX(
                FILTER(
                    ALL(VW_F_PIPELINE_HISTORY),
                    VW_F_PIPELINE_HISTORY[DS] = CurrentDS
                ),
                IF(VW_F_PIPELINE_HISTORY[is_last_day_of_fiscal_quarter], 1, 0)
            )
        ) = 1

    -- 2. Get last day of Month1 in that quarter
    VAR LastDayOfMonth1InQuarter =
        CALCULATE(
            MAX(VW_F_PIPELINE_HISTORY[DS]),
            FILTER(
                ALL(VW_F_PIPELINE_HISTORY),
                VW_F_PIPELINE_HISTORY[Month] = 1 &&
                VW_F_PIPELINE_HISTORY[FISCAL_YEAR_QUARTER] = CurrentQuarter &&
                VW_F_PIPELINE_HISTORY[Fiscal Year] = FiscalYear
            )
        )

    -- 3. Get CW_MTD value for that Month1 day, preserve all filters except DS
    VAR Month1Value =
        CALCULATE(
            SUM(VW_F_PIPELINE_HISTORY[CW_MTD]),
            FILTER(
                ALL(VW_F_PIPELINE_HISTORY[DS]),
                VW_F_PIPELINE_HISTORY[DS] = LastDayOfMonth1InQuarter
            )
        )

    -- 4. Show daily values if flag is false and it's Month1
    VAR ShowDaily =
        NOT SelectedFlag &&
        CurrentMonth = 1 &&
        CurrentDS <= TODAY()

    -- Final Output
    RETURN
    SWITCH(
        TRUE(),
        SelectedFlag && IsQuarterEndDate, Month1Value,
        ShowDaily, SUM(VW_F_PIPELINE_HISTORY[CW_MTD]),
        BLANK()
    )