Home » excel » excel – Power BI Running total horizontally

excel – Power BI Running total horizontally

Posted by: admin May 14, 2020 Leave a comment


I am working with Power BI tables and I am not being able to calculate balance “Running total” like I need.

I was searching in Stack Overflow and other webpages and I always find the same solution for a very similar situation, which is not this.

First of all, this is my table:

First table

I found in this and other sites the same solution:

Running Total COLUMN =
        SUM ( 'My table'[Accounting Balance] ),
        ALL ( 'My table' ),
       'My table'[Date] <= EARLIER ( 'My table'[Date] ))

This would work whenever I need to sum the rows vertically, which is not my case. Indeed, I need to sum the valu horizontally:

Second table

Any suggestions?

Edit 1:

This is what I need:

Result table

So if you take a close look to this table it has the resulting calculation of each column for each vendor like:

  1. Vendor 1 owed $200 on January 2017
  2. Vendor 1 owed $0 on February 2017 because he made a $200 payment
  3. Vendor 1 owed $50 on March 2017 because $0 + $50
  4. Vendor 1 owed $50 on April 2017 because he didn’t make any payment.
  5. Vendor 1 owes $50 in total.
  6. etc
How to&Answers:

This is done in Power Query as I don’t have BI, but I assume the M-Code will work just as well. It will also auto adjust if you refresh the query as you add/delete columns, so there is no need to reference each column individually.

You add a Custom Column which sums all the columns except the “Vendor/Month”

    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor/Month", type text}, {"Jan-17", Int64.Type}, {"Feb-17", Int64.Type}, {"Mar-17", Int64.Type}, {"Apr-17", Int64.Type}}),
    #"Sum" = Table.AddColumn(
    #"Changed Type",
  each List.Sum(
      List.RemoveItems(Table.ColumnNames(#"Changed Type"), {"Vendor/Month"})))))

This is the Custom Column Dialog:

enter image description here

And this is the result:

enter image description here


I’m making a big assumption here: That “Totals” the column does NOT need a “Total” row.

If not, then isn’t the Column defined as shown:

Totals = Table1[Jan-17]+Table1[Feb-17]+Table1[Mar-17]

PowerBI Totals Column


As suggested earlier by Alexis Olson, you should import the ‘table’ you get from the OLAP-cube into the Power Query Editor and unpivot it. You would start with this:

enter image description here

Use a script like this:

    Source = Excel.Workbook(File.Contents("C:\Users7085\Documents\Marco\SO\SO18-10-1.xlsx"), null, true),
    Sheet3_Sheet = Source{[Item="myTable",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Sheet3_Sheet,{{"Column1", type text}, {"Column2", type any}, {"Column3", type any}, {"Column4", type any}, {"Column5", type any}, {"Column6", type any}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Totals"}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Columns",1),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Bottom Rows", {"Vendor/Month"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each "1-"&[Attribute]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Custom", "Month"}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Renamed Columns",{"Attribute"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns1",{{"Value", Int64.Type}}),
    #"Renamed Columns1" = Table.RenameColumns(#"Changed Type2",{{"Value", "Accounting Balance"}, {"Vendor/Month", "Vendor"}})
    #"Renamed Columns1"

And the resulting table would be this:

enter image description here

Then create the following maesure:

RunningTotal = 
    SUM ( myTable[Accounting Balance] ),
    FILTER (
        ALLEXCEPT ( 'myTable', myTable[Vendor] ),
        'myTable'[Month] <= SELECTEDVALUE ( myTable[Month] )

In a matrix visual put Vendor on rows, Month on columns and the [RunningTotal] on Values.

enter image description here