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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
jaipal
Resolver III
Resolver III

Power Query Running Totals

Hello Community, I am facing an issue while runnign totals in Power Query.

 

When I run totals for revenue and cost the output is correct:

jaipal_0-1643254238200.png                       

jaipal_1-1643254281521.png

 

But when I merge those two Queries, I am getting wrong values like this:

jaipal_3-1643254477278.png

 

 

Here is my Code:

let
Source = Fact,
#"Filtered Rows" = Table.SelectRows(Source, each ([ProjectKey] = 430) and ([FiscalYear] = 2021)),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"DateKey", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"ProjectKey", "FiscalYear"}, {{"Count", each _, type table [DateKey=number, MonthName=text, FiscalYear=nullable number, ProjectKey=nullable number, CostAmount=nullable number, Revenue=nullable number, BudgetMonthlyRevenue=nullable number, BudgetMonthlyCost=nullable number, BudgetYTDRevenue=nullable number, BudgetYTDCost=nullable number]}}),


// Function to calculate running totals
RunFunction = (RunTable as table) as table=>
let
#"Added Index" = Table.AddIndexColumn(RunTable, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "CummulativeRevenue", each List.Sum(List.Range(#"Added Index" [Revenue],0,[Index])))

in
#"Added Custom",
// call the function
RunTotals= Table.TransformColumns(#"Grouped Rows", {"Count", each RunFunction(_)}),
#"Expanded Count" = Table.ExpandTableColumn(RunTotals, "Count", {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue"}, {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue"}),
// Function to calculate running totals
RunFunction2 = (RunTable as table) as table=>
let
#"Added Index" = Table.AddIndexColumn(RunTable, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "CummulativeCost", each List.Sum(List.Range(#"Added Index" [CostAmount],0,[Index])))

in
#"Added Custom",
// call the function
RunTotals2 = Table.TransformColumns(#"Grouped Rows", {"Count", each RunFunction2(_)}),
#"Expanded Count1" = Table.ExpandTableColumn(RunTotals2, "Count", {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeCost"}, {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeCost"}),
#"Merged Queries" = Table.NestedJoin(#"Expanded Count1", {"ProjectKey", "DateKey","FiscalYear"}, #"Expanded Count", {"ProjectKey", "DateKey","FiscalYear"},"Expanded Count", JoinKind.LeftOuter),
#"Expanded Expanded Count" = Table.ExpandTableColumn(#"Merged Queries", "Expanded Count", {"CummulativeRevenue"}, {"CummulativeRevenue"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Expanded Count",{"DateKey", "MonthName", "FiscalYear", "ProjectKey", "CummulativeCost", "CummulativeRevenue", "BudgetYTDCost", "BudgetYTDRevenue"}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"CummulativeCost", "Cost"}, {"CummulativeRevenue", "Revenue"}, {"BudgetYTDCost", "BudgetCost"}, {"BudgetYTDRevenue", "BudgetRevenue"}}),
#"Sorted Rows1" = Table.Sort(#"Renamed Columns",{{"DateKey", Order.Ascending}})
in
#"Sorted Rows1"

 

 

Is the problem with Index or Merge queries?

Hope someone can help me with this..

Thanks

 

 

 

 

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Community Champion
Community Champion

let
Source = Fact,
#"Filtered Rows" = Table.SelectRows(Source, each ([ProjectKey] = 430) and ([FiscalYear] = 2021)),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"DateKey", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"ProjectKey", "FiscalYear"}, {"Count", each _,}),
// Function to calculate running totals
RunFunction = (RunTable as table) as table=>
let
#"Added Index" = Table.AddIndexColumn(RunTable, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "n", each {List.Sum(List.Range(#"Added Index" [Revenue],0,[Index])),List.Sum(List.Range(#"Added Index" [CostAmount],0,[Index]))}),

Custom1 = Table.SplitColumn(#"Added Custom","n",each _,{"CummulativeRevenue","CummulativeCost"})
in
Custom1,
// call the function
RunTotals= Table.TransformColumns(#"Grouped Rows", {"Count", each RunFunction(_)}),
#"Expanded Count" = Table.ExpandTableColumn(RunTotals, "Count", {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue","CummulativeCost"}, {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue","CummulativeCost"})

in

#"Expanded Count"

View solution in original post

2 REPLIES 2
jaipal
Resolver III
Resolver III

@wdx223_Daniel  Thank you. 

wdx223_Daniel
Community Champion
Community Champion

let
Source = Fact,
#"Filtered Rows" = Table.SelectRows(Source, each ([ProjectKey] = 430) and ([FiscalYear] = 2021)),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"DateKey", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"ProjectKey", "FiscalYear"}, {"Count", each _,}),
// Function to calculate running totals
RunFunction = (RunTable as table) as table=>
let
#"Added Index" = Table.AddIndexColumn(RunTable, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "n", each {List.Sum(List.Range(#"Added Index" [Revenue],0,[Index])),List.Sum(List.Range(#"Added Index" [CostAmount],0,[Index]))}),

Custom1 = Table.SplitColumn(#"Added Custom","n",each _,{"CummulativeRevenue","CummulativeCost"})
in
Custom1,
// call the function
RunTotals= Table.TransformColumns(#"Grouped Rows", {"Count", each RunFunction(_)}),
#"Expanded Count" = Table.ExpandTableColumn(RunTotals, "Count", {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue","CummulativeCost"}, {"DateKey", "MonthName", "BudgetYTDRevenue", "BudgetYTDCost", "CummulativeRevenue","CummulativeCost"})

in

#"Expanded Count"

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.