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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TMO_KY
Helper II
Helper II

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.

 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@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

 

DataInsights_0-1667397989467.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@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

 

DataInsights_0-1667397989467.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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

Glad to hear that!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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