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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
imranamikhan
Helper V
Helper V

Get variable previous record

Hello everyone,

 

Context:

 

I have a query which displays a log of programme status information across multiple periods.

 

Index: a standard INDEX generated in Power BI

Period Sort Index: this column is a merge from a separate lookup table. It returns a period number associated to the record

Programme names: a list of programme names

Category: a set list of categories per programme

RAG: the RAG status assigned to each category

 

Data.PNG

 

Problem:

 

It is simple enough to transform the data to display the latest period data only. However, I want to return the previous RAG status for each category as an additional column, based on the whatever the previous period number was for that record.

 

The problem is that the period number can be variable, so I cannot simply subtract 1 from the sort index.

 

In the example above, we have 3 entries for Programme 1. An entry from Period 8 (the current period), as well entries for Period 2 and Period 1.

 

The expected result is to display data for Period 8, but also return the RAG from the previous Period (for Programme 1 this would be Period 2) as an additional column.

 

If a previous period does not exist at all (such as in Programme 2), then return blank.

 

Expected Result.PNG

 

Any support to point me in the right direction would be appreciated.

 

Best regards,

AmiK

1 ACCEPTED SOLUTION

Hi @imranamikhan ,

 

Try this:

Last RAG Column =
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Period Sort Index] < EARLIER ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Category] = EARLIER ( 'Table'[Category] )           -----------added
                && 'Table'[Period Sort Index] = LastPeriod_
        )
    )
Last RAG Measure =
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name] ),
            'Table'[Period Sort Index] < MAX ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name], 'Table'[Category] ),  ---------edited
            'Table'[Period Sort Index] = LastPeriod_
        )
    )

screenshot.PNG

 

 

Best Regards,

Icey

 

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

View solution in original post

10 REPLIES 10
Icey
Community Support
Community Support

Hi @imranamikhan ,

 

I create calculated columns, can this meet your requirements?

LastIndex_ = 
CALCULATE (
    MAX ( 'Table'[Index] ),
    FILTER (
        ALLEXCEPT ( 'Table', 'Table'[Programma_Name], 'Table'[Category] ),
        'Table'[Index] < EARLIER ( 'Table'[Index] )
    )
)
Last RAG = 
IF (
    NOT ( ISBLANK ( 'Table'[LastIndex_] ) ),
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[LastIndex_] ) )
    )
)

last RAG.PNG

 

 

Best Regards,

Icey

 

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

Thanks @Icey for looking into this.

 

This is close but the calculation looks like it is running in ascending order.  The calculation is returning the RAG for the lowest Period Number (Period 1), and then returning the RAG value for previous Period in the Last RAG column (Period 2).

 

This is incorrect because Period 8 is the latest/current RAG for the selected Programme, and the Last RAG column should be derived from whatever the previous Period number is for the Programme in descending order.

 

So for example, Programme 1 has three periods. Period 8, Period 2, and Period 1. If Period 8 is selected, then the Last RAG should be derived for whatever the values are in Period 2.

 

You can see this is happening because in Period 8, the Last RAG column is displaying Blank.

 

example.png

 

example2.PNG

Hi @imranamikhan ,

 

If so, just try to create a calculated column or measure like so:

 

Last RAG Column = 
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Period Sort Index] < EARLIER ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Period Sort Index] = LastPeriod_
        )
    )

 

column.PNG

 

 

Last RAG Measure =
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name] ),
            'Table'[Period Sort Index] < MAX ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name] ),
            'Table'[Period Sort Index] = LastPeriod_
        )
    )

 

measure1.PNG

 measure2.PNG

 

 

Best Regards,

Icey

 

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

Thanks for your help on this @Icey  it is really appreciated.

 

This is almost there, but I have noticed the calculation only works if the RAG value is the same for each category per period.

 

For example, if I change the RAG value to different RAGs (e.g. A & G) in Period 2, the calculation fails and only returns one RAG value.

 

example.png

Hi @imranamikhan ,

 

Try this:

Last RAG Column =
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Period Sort Index] < EARLIER ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            'Table',
            'Table'[Programma_Name] = EARLIER ( 'Table'[Programma_Name] )
                && 'Table'[Category] = EARLIER ( 'Table'[Category] )           -----------added
                && 'Table'[Period Sort Index] = LastPeriod_
        )
    )
Last RAG Measure =
VAR LastPeriod_ =
    CALCULATE (
        MAX ( 'Table'[Period Sort Index] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name] ),
            'Table'[Period Sort Index] < MAX ( 'Table'[Period Sort Index] )
        )
    )
RETURN
    CALCULATE (
        MAX ( 'Table'[RAG] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Programma_Name], 'Table'[Category] ),  ---------edited
            'Table'[Period Sort Index] = LastPeriod_
        )
    )

screenshot.PNG

 

 

Best Regards,

Icey

 

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

You are amazing. Thank you for your support and help on this.

imranamikhan
Helper V
Helper V

Hi @amitchandak, any thoughts on this?

@imranamikhan , not able to check your reply yet.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
amitchandak
Super User
Super User

@imranamikhan , Try a new column like

 


new column =
var _max = maxx(filter(Table, [category] = earlier([category]) && [period] < earlier([Period])),[Period])
return
maxx(filter(Table, [category] = earlier([category]) && [period] =_max),[Rag])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks @amitchandak .

I was having issues with the solution but then as a test, I filtered the data to one programme only in query editor and the solution worked:

 

For example:

Table in Query Editor Single Programme.PNG

 

Working with one programme.PNG

 

However, the reality is that the data includes multiple programmes:

 

Table in Query Editor Multiple Programmes.PNG


In the below screenshot I would expect the Previous RAG for Budget RAG to be G, not R.

 

I cannot quite identify what I need to change in the DAX to get this work?

Error.PNG

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors