Forum Discussion

surajde's avatar
surajde
Icon for Helper I rankHelper I
3 years ago
Solved

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))

 

 

 

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
  • 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]) )

  • 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.

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

    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

4 Replies

  • 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]) )

    • surajde's avatar
      surajde
      Icon for Helper I rankHelper I

      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. 

       

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

       

       

      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
  • 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.

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

    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

    • surajde's avatar
      surajde
      Icon for Helper I rankHelper I

       Amazing!! This one worked ğŸ˜€