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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Creating Calculated Rows

Hello,

 

I have a system feeding over predicted revenue into PowerBI. The data is similar to this:

Project IDMetricValueYear
1Revenue (Year 1)3000002023
1Revenue (Year 3)6500002025
1Revenue (Year 5)10000002027
2Revenue (Year 1)1250002023
2Revenue (Year 3)2750002025
2Revenue (Year 5)5500002027
3Revenue (Year 1)500002024
3Revenue (Year 3)1500002026
3Revenue (Year 5)3000002028
4Revenue (Year 3)500002025
4Revenue (Year 5)1000002027
5Revenue (Year 3)1500002024
5Revenue (Year 5)3300002026
6Revenue (Year 1)200002023
6Revenue (Year 3)500002025
6Revenue (Year 5)1000002027

 

I am trying to run reports on the in-between years - Year 2 and Year 4 - using a formula of (((Year 3 + Year 1) / 2) + Year 1) for Year 2 and (((Year 5 + Year 3)/2) + Year 3) for Year 4. I created a metric to calcualte it, but my PowerBI reports rely on a calculated table to generate due to other (not included here) metrics. My desired result would be like this on my base table and then I would draw them into my calcualted table: 

 

Project IDMetricValueYear
1Revenue (Year 1)3000002023
1Revenue (Year 2)4750002024
1Revenue (Year 3)6500002025
1Revenue (Year 4)8250002026
1Revenue (Year 5)10000002027
2Revenue (Year 1)1250002023
2Revenue (Year 2)2000002024
2Revenue (Year 3)2750002025
2Revenue (Year 4)4125002026
2Revenue (Year 5)5500002027
3Revenue (Year 1)500002024
3Revenue (Year 2)1000002025
3Revenue (Year 3)1500002026
3Revenue (Year 4)2250002027
3Revenue (Year 5)3000002028
4Revenue (Year 3)500002025
4Revenue (Year 4)750002026
4Revenue (Year 5)1000002027
5Revenue (Year 3)1500002024
5Revenue (Year 4)2400002025
5Revenue (Year 5)3300002026
6Revenue (Year 1)200002023
6Revenue (Year 2)350002024
6Revenue (Year 3)500002025
6Revenue (Year 4)750002025
6Revenue (Year 5)1000002027

 

Is there a way in Power Query to generate these rows?

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

This is called "linear interpolation".

 

Borrowing the interpolation function from here:

https://stackoverflow.com/questions/64067210/how-can-i-interpolate-missing-values-in-a-column-in-pow...

 

Here's an example of how you can use it for your particular scenario:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdIxDsMwCAXQq0SeGimDAeP0HN2qKEMGrx0qtedvTIciwKony3riA/K2JUhLurV3e7zadLm34znBfD5R7ue8YEZK+xJB6rDyD/IAcoeQVclVJMbZgGyyPZRsXNlkeyjZzDaa4mjlysBJMihYB5DtHq8CS1zRrdE7tUY9Cv9vsQzgt0Wys9R4OWi/hHfxKN5Fo+wf", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, Metric = _t, Value = _t, Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", Int64.Type}, {"Metric", type text}, {"Value", type number}, {"Year", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Project ID"}, {{"Year", each {List.Min([Year])..List.Max([Year])}, type list}}),
    #"Expanded Years" = Table.ExpandListColumn(#"Grouped Rows", "Year"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Years",{{"Year", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"Project ID", "Year"}, #"Changed Type", {"Project ID", "Year"}, "Expanded Years", JoinKind.LeftOuter),
    #"Expanded Expanded Years" = Table.ExpandTableColumn(#"Merged Queries", "Expanded Years", {"Value"}, {"Value"}),
    #"Invoked Function" = fn_Interpolate(#"Expanded Expanded Years", "Year", "Value"),
    #"Sorted Rows" = Table.Sort(#"Invoked Function", {{"Project ID", Order.Ascending}, {"Year", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Metric", each "Revenue (Year " & Text.From([Year] - 2022) & ")", type text),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Project ID", "Metric", "Value", "Year"})
in
    #"Reordered Columns"

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

This is called "linear interpolation".

 

Borrowing the interpolation function from here:

https://stackoverflow.com/questions/64067210/how-can-i-interpolate-missing-values-in-a-column-in-pow...

 

Here's an example of how you can use it for your particular scenario:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdIxDsMwCAXQq0SeGimDAeP0HN2qKEMGrx0qtedvTIciwKony3riA/K2JUhLurV3e7zadLm34znBfD5R7ue8YEZK+xJB6rDyD/IAcoeQVclVJMbZgGyyPZRsXNlkeyjZzDaa4mjlysBJMihYB5DtHq8CS1zRrdE7tUY9Cv9vsQzgt0Wys9R4OWi/hHfxKN5Fo+wf", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, Metric = _t, Value = _t, Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", Int64.Type}, {"Metric", type text}, {"Value", type number}, {"Year", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Project ID"}, {{"Year", each {List.Min([Year])..List.Max([Year])}, type list}}),
    #"Expanded Years" = Table.ExpandListColumn(#"Grouped Rows", "Year"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Years",{{"Year", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"Project ID", "Year"}, #"Changed Type", {"Project ID", "Year"}, "Expanded Years", JoinKind.LeftOuter),
    #"Expanded Expanded Years" = Table.ExpandTableColumn(#"Merged Queries", "Expanded Years", {"Value"}, {"Value"}),
    #"Invoked Function" = fn_Interpolate(#"Expanded Expanded Years", "Year", "Value"),
    #"Sorted Rows" = Table.Sort(#"Invoked Function", {{"Project ID", Order.Ascending}, {"Year", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Metric", each "Revenue (Year " & Text.From([Year] - 2022) & ")", type text),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Project ID", "Metric", "Value", "Year"})
in
    #"Reordered Columns"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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