Search code examples
excelpowerbidaxpowerquerym

Power BI Running total horizontally


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 =
    CALCULATE (
        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

Solution

  • 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"

    let
        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",
          "Total",
      each List.Sum(
        Record.ToList(
          Record.SelectFields(
          _,
          List.RemoveItems(Table.ColumnNames(#"Changed Type"), {"Vendor/Month"})))))
    in
        #"Sum"
    

    This is the Custom Column Dialog:

    enter image description here

    And this is the result:

    enter image description here