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
Anonymous
Not applicable

Changes by Date and Items

hi all, i have a table in power bi desktop as in the following, i need to calculate the daily changes for each item starting from the minimum value whcih is already on the first date,new dates are added every day with new values (accumulated)

 

NameDateCumulative Value
item1date11
item2date16
item3date19
item1date26
item2date212
item3date216
item1date311
item2date326
item3date328
1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyixJzTVU0lEyMjAy1DfRBzENlWJ1IBJGqBJmcAljVAlLuASSUUYoOoxQJQyNsJkFljHDZhhIkSFWh4G1Y3UZmGmhFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Date = _t, #"Cumulative Value" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Date", type date}, {"Cumulative Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Data", each let tab=Table.AddIndexColumn(_,"Index",1,1,Int64.Type) in Table.AddColumn(tab,"New",(x)=>try Table.Max( Table.SelectRows(tab,(y)=>y[Index]=x[Index]-1),"Index")[Cumulative Value] otherwise (x)[Cumulative Value] ), type table [Name=nullable text, Date=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "New Data", each Table.AddColumn([Data],"Diff", each [Cumulative Value] - [New])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Expanded New Data" = Table.ExpandTableColumn(#"Removed Columns", "New Data", {"Date", "Cumulative Value", "Diff"}, {"New Data.Date", "New Data.Cumulative Value", "New Data.Diff"}),
    #"Sorted Rows" = Table.Sort(#"Expanded New Data",{{"New Data.Date", Order.Ascending}, {"Name", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"New Data.Date", "Date"}, {"New Data.Cumulative Value", "Cumulative Value"}, {"New Data.Diff", "Diff"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}, {"Cumulative Value", Int64.Type}, {"Diff", Int64.Type}})
in
    #"Changed Type1"

diff.png

 

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

View solution in original post

2 REPLIES 2
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyixJzTVU0lEyMjAy1DfRBzENlWJ1IBJGqBJmcAljVAlLuASSUUYoOoxQJQyNsJkFljHDZhhIkSFWh4G1Y3UZmGmhFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Date = _t, #"Cumulative Value" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Date", type date}, {"Cumulative Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Data", each let tab=Table.AddIndexColumn(_,"Index",1,1,Int64.Type) in Table.AddColumn(tab,"New",(x)=>try Table.Max( Table.SelectRows(tab,(y)=>y[Index]=x[Index]-1),"Index")[Cumulative Value] otherwise (x)[Cumulative Value] ), type table [Name=nullable text, Date=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "New Data", each Table.AddColumn([Data],"Diff", each [Cumulative Value] - [New])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Expanded New Data" = Table.ExpandTableColumn(#"Removed Columns", "New Data", {"Date", "Cumulative Value", "Diff"}, {"New Data.Date", "New Data.Cumulative Value", "New Data.Diff"}),
    #"Sorted Rows" = Table.Sort(#"Expanded New Data",{{"New Data.Date", Order.Ascending}, {"Name", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"New Data.Date", "Date"}, {"New Data.Cumulative Value", "Cumulative Value"}, {"New Data.Diff", "Diff"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}, {"Cumulative Value", Int64.Type}, {"Diff", Int64.Type}})
in
    #"Changed Type1"

diff.png

 

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

artemus
Employee
Employee

You can use the following formula, just replace MyTable with the name of the previous step:

 

= Table.FromColumns(Table.ToColumns(MyTable) & {List.Accumulate(MyTable[Cumulative Value], [List = {}, Prev = 0], (current, next) => [List = current[List] & {next - current[Prev]}, Prev = next - current[Prev]])[List]}, Table.ColumnNames(MyTable) & {"DoD"})

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.

Top Solution Authors
Top Kudoed Authors