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
surajde
Helper I
Helper I

most recent value of same calculated column previous row using Variable

Hi All, 

 

Please help with a formula for getting most recent value of same calculated column previous row. 

I need to replicate this in PowerBI. 
Column E is calculated column and I need to somehow capture the last calculated value on a variable and then variable +1 

 

Excel formula : 

=IF(A2<>A1, 0, IF(C2=0, E1, E1+1))

 

surajde_0-1684075485487.png

 

 

Data : 

IDDeltaTimeCurrent Group TimeCount
1231230.0007/1/22 9:04 AM0
1231230.0040.9666677/1/22 9:04 AM1
2342430.0007/1/22 9:04 AM0
2342430.0007/1/22 9:04 AM0
2342430.0038.9333337/4/22 5:46 AM1
23424374.8817/4/22 6:43 AM2
2342430.0038.9333337/4/22 6:43 AM3
3456750.0038.9333337/4/22 6:43 AM0
2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@surajde , Create a new column like

 

if([Time] =0, 0, countx(filter(Table, [ID] = earlier([ID]) && [Time] >0 && [Current Group Time] <= earlier([Current Group Time])), [ID]) )

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

View solution in original post

v-yanjiang-msft
Community Support
Community Support

Hi @surajde ,

According to your description, here's my solution.

1.Add an index column in Power Query.

2.Create two calculated columns.

flag =
IF (
    'Table'[ID]
        <> MAXX (
            FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
            'Table'[ID]
        ),
    "yes",
    "no"
)
Column =
VAR _c = 'Table'[Index]
RETURN
    IF (
        [ID]
            <> MAXX (
                FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
                'Table'[ID]
            ),
        0,
        IF (
            MAXX (
                FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
                'Table'[flag]
            ) = "no",
            IF (
                'Table'[Time] = 0,
                COUNTROWS (
                    FILTER (
                        'Table',
                        'Table'[Index] < EARLIER ( 'Table'[Index] )
                            && 'Table'[Index]
                                > MAXX (
                                    FILTER ( 'Table', 'Table'[flag] = "yes" && 'Table'[Index] < _c ),
                                    'Table'[Index]
                                )
                            && 'Table'[Time] <> 0
                    )
                ) + 0,
                COUNTROWS (
                    FILTER (
                        'Table',
                        'Table'[Index] < EARLIER ( 'Table'[Index] )
                            && 'Table'[Index]
                                > MAXX (
                                    FILTER ( 'Table', 'Table'[flag] = "yes" && 'Table'[Index] < _c ),
                                    'Table'[Index]
                                )
                            && 'Table'[Time] <> 0
                    )
                ) + 1
            ),
            IF ( 'Table'[Time] = 0, 0, 1 )
        )
    )

Get the correct result.

vyanjiangmsft_0-1684315410846.png

I also change the data source with different IDs, still get correct result.

vyanjiangmsft_1-1684315452596.png

I attach my file below for your reference.

 

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

Best regards,

Community Support Team_yanjiang

View solution in original post

4 REPLIES 4
v-yanjiang-msft
Community Support
Community Support

Hi @surajde ,

According to your description, here's my solution.

1.Add an index column in Power Query.

2.Create two calculated columns.

flag =
IF (
    'Table'[ID]
        <> MAXX (
            FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
            'Table'[ID]
        ),
    "yes",
    "no"
)
Column =
VAR _c = 'Table'[Index]
RETURN
    IF (
        [ID]
            <> MAXX (
                FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
                'Table'[ID]
            ),
        0,
        IF (
            MAXX (
                FILTER ( 'Table', 'Table'[Index] = EARLIER ( 'Table'[Index] ) - 1 ),
                'Table'[flag]
            ) = "no",
            IF (
                'Table'[Time] = 0,
                COUNTROWS (
                    FILTER (
                        'Table',
                        'Table'[Index] < EARLIER ( 'Table'[Index] )
                            && 'Table'[Index]
                                > MAXX (
                                    FILTER ( 'Table', 'Table'[flag] = "yes" && 'Table'[Index] < _c ),
                                    'Table'[Index]
                                )
                            && 'Table'[Time] <> 0
                    )
                ) + 0,
                COUNTROWS (
                    FILTER (
                        'Table',
                        'Table'[Index] < EARLIER ( 'Table'[Index] )
                            && 'Table'[Index]
                                > MAXX (
                                    FILTER ( 'Table', 'Table'[flag] = "yes" && 'Table'[Index] < _c ),
                                    'Table'[Index]
                                )
                            && 'Table'[Time] <> 0
                    )
                ) + 1
            ),
            IF ( 'Table'[Time] = 0, 0, 1 )
        )
    )

Get the correct result.

vyanjiangmsft_0-1684315410846.png

I also change the data source with different IDs, still get correct result.

vyanjiangmsft_1-1684315452596.png

I attach my file below for your reference.

 

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

Best regards,

Community Support Team_yanjiang

 Amazing!! This one worked 😀

amitchandak
Super User
Super User

@surajde , Create a new column like

 

if([Time] =0, 0, countx(filter(Table, [ID] = earlier([ID]) && [Time] >0 && [Current Group Time] <= earlier([Current Group Time])), [ID]) )

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 , thanks for quick response. 

 

Please check that wehn I add your formula to BI then I get below results. most of the values are 0. 

Also we're not complaring time here. your formula is comparing previous and current time. 

surajde_0-1684141837334.png

 

Please try with below data: in this data last column is taking its own last calculated value. 

surajde_1-1684142141789.png

 

 

IDCREATE_DATEMODIFY_DATEDeltaScopeTimeCurrent Group TimeCount
42217577/1/22 9:04 AM7/1/22 9:04 AM0YES07/1/22 9:04 AM0
42217577/1/22 9:04 AM7/1/22 9:08 AM0YES07/1/22 9:04 AM0
42217577/1/22 9:04 AM7/1/22 9:45 AM0YES40.966666677/1/22 9:04 AM1
42217577/1/22 9:04 AM7/1/22 2:01 PM0NO07/1/22 2:01 PM1
42217577/1/22 9:04 AM7/1/22 2:07 PM6.46666667NO07/1/22 2:01 PM1
42217577/1/22 9:04 AM7/4/22 5:45 AM0NO07/1/22 2:01 PM1
42217577/1/22 9:04 AM7/4/22 5:46 AM0YES07/4/22 5:46 AM1
42217577/1/22 9:04 AM7/4/22 6:25 AM0YES38.933333337/4/22 5:46 AM2
42217577/1/22 9:04 AM7/4/22 6:27 AM0YES07/4/22 5:46 AM2
42217577/1/22 9:04 AM7/4/22 6:31 AM0YES07/4/22 5:46 AM2
42217577/1/22 9:04 AM7/4/22 6:43 AM0NO07/4/22 6:43 AM2
42217577/1/22 9:04 AM7/4/22 7:58 AM74.88333333107/4/22 6:43 AM2
42217577/1/22 9:04 AM7/6/22 2:50 AM0NO07/4/22 6:43 AM2
42217577/1/22 9:04 AM7/6/22 3:02 AM0YES07/6/22 3:02 AM2
42217577/1/22 9:04 AM7/6/22 3:07 AM0YES4.766666677/6/22 3:02 AM3
42217577/1/22 9:04 AM7/6/22 3:26 AM0NO07/6/22 3:26 AM3

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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