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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
VV24
Helper III
Helper III

Row context total of previous rows

Dear all,

 

I would like to make a calculated column which calculates on the row context, per key the total untill that moment.

See the example below, I have three columns, and a key is active from a (consecutive) date range. And i would like to count the total amount until that moment for the row. The desired outcome is made in 'New column'

KeyDateAmountNew column
A1-1-201911
A2-1-201912
A3-1-201913
A4-1-201914
A5-1-201915
A6-1-201916
A7-1-201917
A8-1-201918
B15-1-201911
B16-1-201912
B17-1-201913
B18-1-201914

 

 

2 ACCEPTED SOLUTIONS
VV24
Helper III
Helper III

Solved it myself ;), and want to share it, if in any case there is an easier way to do this, im still interested.

My calculated column formula is as follows:

'New column' =
CALCULATE ( SUM ( Table1[Amount] ); ALLEXCEPT ( Table1; Table1[Key] ) )
    - DATEDIFF (
        Table1[Date];
        CALCULATE ( MAX ( Table1[Date] ); ALLEXCEPT ( Table1; Table1[Key] ) );
        DAY
    )

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This M code (Query Editor) will also work

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type datetime}, {"Amount", Int64.Type}}),
    Partition = Table.Group(#"Changed Type", {"Key"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date", "Amount", "Index"}, {"Date", "Amount", "Index"})
in
    #"Expanded Partition"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code (Query Editor) will also work

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type datetime}, {"Amount", Int64.Type}}),
    Partition = Table.Group(#"Changed Type", {"Key"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date", "Amount", "Index"}, {"Date", "Amount", "Index"})
in
    #"Expanded Partition"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish,

 

thanks for the reply, had to solve it in Tabular, but its cool to know how to solve such an issue in PQ!

 

best,

 

Victor

VV24
Helper III
Helper III

Solved it myself ;), and want to share it, if in any case there is an easier way to do this, im still interested.

My calculated column formula is as follows:

'New column' =
CALCULATE ( SUM ( Table1[Amount] ); ALLEXCEPT ( Table1; Table1[Key] ) )
    - DATEDIFF (
        Table1[Date];
        CALCULATE ( MAX ( Table1[Date] ); ALLEXCEPT ( Table1; Table1[Key] ) );
        DAY
    )

Hi @VV24 ,

It's pleasant that your problem has been solved, could you please mark one reply as Answered to close this topic?

 

Regards,

Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.