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
SherryZhao
Frequent Visitor

how to plus the latest row data of column with previous column

Hi all, hope you all well. It's hard to descripe my problem, so I show in the table. I have the left column and want to get the right column. The logic in excel is shown in the pictures. Thanks for your great support in advance.

Screenshot 2022-12-09 145759.png

CountResult
00
11
12
13
00
11
12
00
11
12
00
11
12
13
14
15
2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

Power BI has no concept of "latest row" or "previous column".  You need to provide guidance yourself, for example via index columns.

 

Here is a Power Query implementation that uses GroupKind.Local (credit to @ImkeF )

let
    SM = (tb) => let 
       #"Added Index" = Table.AddIndexColumn(tb, "Index", 0, 1, Int64.Type),
       #"Added Custom" = Table.AddColumn(#"Added Index", "Result", each List.Sum(List.FirstN(#"Added Index"[Count],[Index]+1)),Int64.Type)
    in #"Added Custom",
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMsQgMcWJEUEjYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Count"}, {{"Count.1", each _, type table [Count=nullable number]}},GroupKind.Local,(x,y)=>Number.From(x=y)),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each SM([Count.1])),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Count", "Result"}, {"Count", "Result"})
in
    #"Expanded Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

Anonymous
Not applicable

Hi @SherryZhao ,

You can create a calculated column as below to get it, please find the details in the attachment.

Result = 
VAR _preclean =
    CALCULATE (
        MAX ( 'Table'[CompletedDate] ),
        FILTER (
            'Table',
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] < EARLIER ( 'Table'[CompletedDate] )
        )
    )
VAR _nextclean =
    CALCULATE (
        MIN ( 'Table'[CompletedDate] ),
        FILTER (
            'Table',
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] > EARLIER ( 'Table'[CompletedDate] )
        )
    )
RETURN
    IF (
        'Table'[Event] = "e_Clean",
        0,
        CALCULATE (
            SUM ( 'Table'[Count] ),
            FILTER (
                'Table',
                'Table'[Event] = EARLIER ( 'Table'[Event] )
                    && 'Table'[CompletedDate] <= EARLIER ( 'Table'[CompletedDate] )
                    && 'Table'[CompletedDate] > _preclean
                    && 'Table'[CompletedDate] < _nextclean
            )
        )
    )

yingyinr_0-1670835356781.png

Or you can also create a measure as below with similar logic to get it:

Measure = 
VAR _selevent =
    SELECTEDVALUE ( 'Table'[Event] )
VAR _selcmdate =
    SELECTEDVALUE ( 'Table'[CompletedDate] )
VAR _preclean =
    CALCULATE (
        MAX ( 'Table'[CompletedDate] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] < _selcmdate
        )
    )
VAR _nextclean =
    CALCULATE (
        MIN ( 'Table'[CompletedDate] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] > _selcmdate
        )
    )
RETURN
    IF (
        _selevent = "e_Clean",
        0,
        CALCULATE (
            SUM ( 'Table'[Count] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Event] = _selevent
                    && 'Table'[CompletedDate] <= _selcmdate
                    && 'Table'[CompletedDate] > _preclean
                    && 'Table'[CompletedDate] < _nextclean
            )
        )
    )

Best Regards

View solution in original post

6 REPLIES 6
lbendlin
Super User
Super User

Power BI has no concept of "latest row" or "previous column".  You need to provide guidance yourself, for example via index columns.

 

Here is a Power Query implementation that uses GroupKind.Local (credit to @ImkeF )

let
    SM = (tb) => let 
       #"Added Index" = Table.AddIndexColumn(tb, "Index", 0, 1, Int64.Type),
       #"Added Custom" = Table.AddColumn(#"Added Index", "Result", each List.Sum(List.FirstN(#"Added Index"[Count],[Index]+1)),Int64.Type)
    in #"Added Custom",
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMsQgMcWJEUEjYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Count"}, {{"Count.1", each _, type table [Count=nullable number]}},GroupKind.Local,(x,y)=>Number.From(x=y)),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each SM([Count.1])),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Count", "Result"}, {"Count", "Result"})
in
    #"Expanded Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Dear Ibendlin, first of all, really appreciate your quick response. I run your code in Power Query and it works. But actually, I got the left column in DAX but not in Power Query, so I wonder if there a DAX solution to get the right column? Thanks.

Anonymous
Not applicable

Hi @SherryZhao ,

Besides that the calculated column [Count], is there any other field in your table? Could you please provide some raw data(exclude sensitive data) in your table, later we will check if we can achieve it by DAX. Thank you.

Best Regards

Dear Rena,

It's ok to share with you the raw data. I actually want to get count between each "e_Clean".

EventCompletedDateCountResult
e_Clean1/5/2019 8:5300
e_In Use1/7/2019 17:0811
e_In Use1/8/2019 13:3512
e_In Use1/11/2019 7:0813
e_Clean1/11/2019 15:5800
e_In Use1/12/2019 10:5511
e_In Use1/12/2019 23:5412
e_Clean1/13/2019 6:5800

Feel free to reach me out if more info needed. Thanks for your effort.

 

Best regards,

Sherry

Anonymous
Not applicable

Hi @SherryZhao ,

You can create a calculated column as below to get it, please find the details in the attachment.

Result = 
VAR _preclean =
    CALCULATE (
        MAX ( 'Table'[CompletedDate] ),
        FILTER (
            'Table',
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] < EARLIER ( 'Table'[CompletedDate] )
        )
    )
VAR _nextclean =
    CALCULATE (
        MIN ( 'Table'[CompletedDate] ),
        FILTER (
            'Table',
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] > EARLIER ( 'Table'[CompletedDate] )
        )
    )
RETURN
    IF (
        'Table'[Event] = "e_Clean",
        0,
        CALCULATE (
            SUM ( 'Table'[Count] ),
            FILTER (
                'Table',
                'Table'[Event] = EARLIER ( 'Table'[Event] )
                    && 'Table'[CompletedDate] <= EARLIER ( 'Table'[CompletedDate] )
                    && 'Table'[CompletedDate] > _preclean
                    && 'Table'[CompletedDate] < _nextclean
            )
        )
    )

yingyinr_0-1670835356781.png

Or you can also create a measure as below with similar logic to get it:

Measure = 
VAR _selevent =
    SELECTEDVALUE ( 'Table'[Event] )
VAR _selcmdate =
    SELECTEDVALUE ( 'Table'[CompletedDate] )
VAR _preclean =
    CALCULATE (
        MAX ( 'Table'[CompletedDate] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] < _selcmdate
        )
    )
VAR _nextclean =
    CALCULATE (
        MIN ( 'Table'[CompletedDate] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Event] = "e_Clean"
                && 'Table'[CompletedDate] > _selcmdate
        )
    )
RETURN
    IF (
        _selevent = "e_Clean",
        0,
        CALCULATE (
            SUM ( 'Table'[Count] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Event] = _selevent
                    && 'Table'[CompletedDate] <= _selcmdate
                    && 'Table'[CompletedDate] > _preclean
                    && 'Table'[CompletedDate] < _nextclean
            )
        )
    )

Best Regards

Dear Rena,

It works!!! Thanks for your support and I will try to understand the logic and coding.

Thanks you all again.

 

Best regards,

Sherry.

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