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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
sivasrao
Helper III
Helper III

Row Count value assigned to Previous Row

The below table has 2 columns, 

 

Event Target
C0
C0
C3
R0
R0
R0
C0
C2
R0
R0

 

My requirement is Target Column.

Target: 

The count of the R value should assign to the previous row where the first R is come appear.


In this example, R will start from the 4th row up to the 6th row. The total Count is 3.

The value 3 should assign to the 3rd row ( previous cell where the first R appears)

 

Same as the 9th and 10th rows having R, the count is 2. That should assign to the 8th cell.

Thank you.

 

2 REPLIES 2
v-rzhou-msft
Community Support
Community Support

Hi @sivasrao ,

 

Firstly, I think you need to add an index column in Power Query Editor.

Then you can create a calculated column by below code to achieve your goal.

Target = 
VAR _Step1 =
    ADDCOLUMNS (
        'Table',
        "Column1",
            CALCULATE (
                MIN ( 'Table'[Index] ),
                FILTER (
                    'Table',
                    'Table'[Index] >= EARLIER ( 'Table'[Index] )
                        && 'Table'[Event] = "R"
                )
            )
    )
VAR _Step2 =
    ADDCOLUMNS (
        _Step1,
        "Column2",
            MAXX (
                FILTER ( _Step1, [Event] <> "R" && [Index] <= EARLIER ( [Index] ) ),
                [Column1]
            )
    )
VAR _Step3 =
    ADDCOLUMNS (
        _Step2,
        "Column3",
            VAR _MAXR =
                MAXX ( FILTER ( _Step2, [Column2] = EARLIER ( [Column2] ) ), [Index] )
            RETURN
                IF ( [Index] = [Column1] - 1, _MAXR - [Column2] + 1, 0 )
    )
RETURN
    SUMX ( FILTER ( _Step3, [Index] = EARLIER ( 'Table'[Index] ) ), [Column3] )

Result is as below.

RicoZhou_0-1677053562206.png

 

Best Regards,
Rico Zhou

 

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

Thank you for the reply.

 

   Perfect solution.👍 

 

But a little bit confused about DAX.  🙂

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.