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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

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
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.