Forum Discussion

vin26's avatar
vin26
Icon for Resolver I rankResolver I
1 year ago
Solved

Get the Max Available Month on Selection

Hello,

 

I am trying to calculate average of value for last 6 months by skipping selected month. When I select a specific month if the data exist for the specific employee in the selected month I am getting correct numbers. But the issue is, if the selected month does not have any value, the measure is not picking up previous month numbers. Please help me to get the Max available month for the specific Employee and calculation should be based on available numbers of months during those selected 6 months. Eg: if I select Aug’24, Avg would be Feb to July or any available data between these time period. Here is my data and measures:

 

MonthEMP IDAmount
2024-01A1004657
2024-02A1003480
2024-03A1004965
2024-04A1001510
2024-05A1004384
2024-06A1001374
2024-01A2003369
2024-02A2002445
2024-03A2004494
2024-04A2003621
2024-05A2002093
2024-06A2001350
2024-01A3003447
2024-02A3003603
2024-03A3003010
2024-04A3002305
2024-01A4001470
2024-02A4004998
2024-03A4002158

 

Data Modeling

 

Measure:

 

Measure = CALCULATE(Average(Table1[Amount]),DATESINPERIOD('DimDate'[Month],EOMONTH(MAX('DimDate'[Month]),-1),-6,MONTH))

 

 

Max Month

 

Measure 2 = EOMONTH(MAX(Table1[Month]),-1)

 

 

 

  • Hi vin26 -Create a Measure to Get the Max Available Month for Each Employee

     

    Max Available Month =
    CALCULATE(
    MAX(Table1[Month]),
    FILTER(
    ALL(Table1),
    Table1[EMP ID] = MAX(Table1[EMP ID]) && Table1[Month] <= MAX('DimDate'[Month])
    )
    )

     

    Measure for last 6 months calc.

    Average Last 6 Months =
    CALCULATE(
    AVERAGE(Table1[Amount]),
    DATESINPERIOD(
    'DimDate'[Month],
    [Max Available Month],
    -6,
    MONTH
    ),
    Table1[Amount] <> BLANK()
    )

     

    check with above measures, if still issue exist please share the pbix file by removing the sensitive data.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi vin26 

     

    Please try this:
    Maybe you can delete the relationship between the Table1 and the DimDate:

    Then add a measure:

     

    MEASURE =
    VAR _slicer =
        MONTH ( MAX ( 'DimDate'[Month] ) )
    RETURN
        CALCULATE (
            AVERAGE ( 'Table'[Amount] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                MONTH ( 'Table'[Month] ) < _slicer
                    && MONTH ( 'Table'[Month] ) >= _slicer - 6
            )
        )
    

     

    Then add a slicer with DimDate[Month]:

    The result is as follow:

     

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Hi vin26 -Create a Measure to Get the Max Available Month for Each Employee

     

    Max Available Month =
    CALCULATE(
    MAX(Table1[Month]),
    FILTER(
    ALL(Table1),
    Table1[EMP ID] = MAX(Table1[EMP ID]) && Table1[Month] <= MAX('DimDate'[Month])
    )
    )

     

    Measure for last 6 months calc.

    Average Last 6 Months =
    CALCULATE(
    AVERAGE(Table1[Amount]),
    DATESINPERIOD(
    'DimDate'[Month],
    [Max Available Month],
    -6,
    MONTH
    ),
    Table1[Amount] <> BLANK()
    )

     

    check with above measures, if still issue exist please share the pbix file by removing the sensitive data.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi vin26 

     

    Please try this:
    Maybe you can delete the relationship between the Table1 and the DimDate:

    Then add a measure:

     

    MEASURE =
    VAR _slicer =
        MONTH ( MAX ( 'DimDate'[Month] ) )
    RETURN
        CALCULATE (
            AVERAGE ( 'Table'[Amount] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                MONTH ( 'Table'[Month] ) < _slicer
                    && MONTH ( 'Table'[Month] ) >= _slicer - 6
            )
        )
    

     

    Then add a slicer with DimDate[Month]:

    The result is as follow:

     

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • vin26's avatar
    vin26
    Icon for Resolver I rankResolver I

    Anonymous rajendraongole1 Thanks a lot for your responses. Both the solution works and my logic also working. Actual issue was with my Data modelling, I had used  'Both' in cross filter direction in connection between DimDate and Table1, after making the connection to single worked fine. Thanks again