Forum Discussion

TMO_KY's avatar
TMO_KY
Icon for Helper II rankHelper II
3 years ago
Solved

DAX Previous Value with multiple categories

Good afternoon Community,

 

I know this post will be similar to some others so hopefully it doesn't get overlooked as I can't seem to find the answer and I'm getting stumped.  I have a DAX table that I created because I had to use ADDMISSINGITEMS to add in 0's for missing dates.  Now the next step was creating an index on the month-year which I got.  The issue I am having now is returning previous value for specific codes and products.  Here's a table of what I am working with.

 

indexMonth-YearCodesProduct FamilyFailuresPrev Month Value
1Oct-2022NSPILV15592732
2Sep-2022NSPILV27323472
3Aug-2022NSPILV34721786
4Jul-2022NSPILV17861149
5Jun-2022NSPILV11492064
6May-2022NSPILV20643097
1Oct-2022NSPIMS00
2Sep-2022NSPIMS00
3Aug-2022NSPIMS00
4Jul-2022NSPIMS00
5Jun-2022NSPIMS00
6May-2022NSPIMS00
1Oct-2022NSPIPC3458
2Sep-2022NSPIPC58101
3Aug-2022NSPIPC10149
4Jul-2022NSPIPC4970
5Jun-2022NSPIPC70125
6May-2022NSPIPC12569

 

The Prev Month Value is the expected result column.  I need to pull the previous month failures for each product family AND codes.  I have over 300 codes and 5 products.  I shortened my example to show one code and 3 products.  Any ideas?  I've tried to use: 2 different variations but am not getting the results I need.  Here is one example of dax I'm using 

 

VAR _PreviousRow = 
CALCULATE (
    MIN ( '24MonthTable'[Index] ),
    ALL ( '24MonthTable' ),
        '24MonthTable'[Failure Code Description]
        = EARLIER ( '24MonthTable'[Failure Code Description] ),
    '24MonthTable'[Month-Year] < EARLIER ( '24MonthTable'[month-year] )
)

RETURN 

CALCULATE( 
    LASTNONBLANKVALUE( '24MonthTable'[Failure Code Description],1 ),
ALL ('24MonthTable' ),
'24MonthTable'[Index] = _PreviousRow
)

 

 

Here is the 2nd variation, this one gets me closer but it's still not right:

 

PrevMonthSum = 
VAR PrevIndx =
CALCULATE(
    MAX( '24MonthTable'[Index] ),
    FILTER(
        '24MonthTable',
        '24MonthTable'[Index] < EARLIER( '24MonthTable'[Index] )
    )
)

VAR  Result =
CALCULATE(
    MAX( '24MonthTable'[Failures] ),
    FILTER(
        '24MonthTable',
        '24MonthTable'[Index] = PrevIndx
    )
)

RETURN
Result

 


Any help would be greatly appreciated, I'm sure it's a simple fix and I've just gone numb looking at it.

 

  • TMO_KY,

     

    Try this calculated column:

     

    Prev Month Value = 
    VAR vIndex = '24MonthTable'[index]
    VAR vResult =
        CALCULATE (
            MAX ( '24MonthTable'[Failures] ),
            ALLEXCEPT (
                '24MonthTable',
                '24MonthTable'[Product Family],
                '24MonthTable'[Codes]
            ),
            '24MonthTable'[index] = vIndex + 1
        )
    RETURN
        vResult

     

     

3 Replies

  • TMO_KY,

     

    Try this calculated column:

     

    Prev Month Value = 
    VAR vIndex = '24MonthTable'[index]
    VAR vResult =
        CALCULATE (
            MAX ( '24MonthTable'[Failures] ),
            ALLEXCEPT (
                '24MonthTable',
                '24MonthTable'[Product Family],
                '24MonthTable'[Codes]
            ),
            '24MonthTable'[index] = vIndex + 1
        )
    RETURN
        vResult

     

     

    • TMO_KY's avatar
      TMO_KY
      Icon for Helper II rankHelper II

      THANK YOU so much!  I've been racking my brain but now that I see it written and working, it makes sense.  Appreciate it!