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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register 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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.