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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
stalerik
Helper II
Helper II

DAX - IF Statement to Hold Last Value

Hello,

 

I am trying to come up with a DAX formula for a calculated column "Last Index" that will return the current index if the NAME has changed from previous value, and hold the last index if the value has not changed.  Please see the example data below.  I have tried to make an if statement (IF current name <> previous name, index, __________)  but I'm getting stuck with the Else.  I've also tried LASTNONBLANK but I'm confused by it and getting errors.

 

Thanks for the help!

 

Example Data

 

IndexNAMELast Index
1

A

1
2A1
3B3
4B3
5C5
6C5
7C5
8A8
9A8
10C10

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

[Last Index] = // calc column
var VeryFirstIndex = MIN( T[Index] )
var CurrIndex = T[Index]
var CurrName = T[Name]
var PrevIndex = CurrIndex - 1
var PrevName =
    MAXX(
        FILTER(
            T,
            T[Index] = PrevIndex
        ),
        T[Name]
    )
var Result =
    switch( true(),
    
        // If we're on the first row,
        // return the current index.
        CurrIndex = VeryFirstIndex, CurrIndex,
        
        // If the current name is same as
        // the prev name, return the value
        // of the index for the last different
        // name before this name plus 1.
        CurrName = PrevName,
            MAXX(
                FILTER(
                    T,
                    T[Name] <> CurrName
                    &&
                    T[Index] < CurrIndex
                ),
                T[Index]
            ) + VeryFirstIndex,
        
        // CurrName <> PrevName
        CurrIndex
    )
RETURN
    Result

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Why can't you use a composite model so that you can mix DQ and Import?

Since my source table was changed to Direct Query, I was thinking that it might not refresh correctly if I made the calculated table as Import.  I've usually only made models with one or the other, not a composite.  I will try a composite model like you suggest.  Thank you again for a quick response.

Anonymous
Not applicable

[Last Index] = // calc column
var VeryFirstIndex = MIN( T[Index] )
var CurrIndex = T[Index]
var CurrName = T[Name]
var PrevIndex = CurrIndex - 1
var PrevName =
    MAXX(
        FILTER(
            T,
            T[Index] = PrevIndex
        ),
        T[Name]
    )
var Result =
    switch( true(),
    
        // If we're on the first row,
        // return the current index.
        CurrIndex = VeryFirstIndex, CurrIndex,
        
        // If the current name is same as
        // the prev name, return the value
        // of the index for the last different
        // name before this name plus 1.
        CurrName = PrevName,
            MAXX(
                FILTER(
                    T,
                    T[Name] <> CurrName
                    &&
                    T[Index] < CurrIndex
                ),
                T[Index]
            ) + VeryFirstIndex,
        
        // CurrName <> PrevName
        CurrIndex
    )
RETURN
    Result

This calculated column works great for Import tables, but I have recently switched the data source to be Direct Query and this method does not work with the MAXX function.  Is there a method for performing the same calculations with a measure instead of a column, or some other way for Direct Query?  I have tried to modify it but I am getting stuck.

This works great, thank you!

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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