# Building a cash flow statement in PowerPivot using dynamic measures in DAX

I got an excelent question today at my new “Ask a question page”. Greg asked me if i knew of a sample for a cash flow statement in PowerPivot. I did not know of one, and to be honest i didn’t even know what a cash flow statement was :).

Searching for a sample i found this picture of a cash flow statement: This made things more clear. We want to see income and expense in two different tables. And then in a new table the cash flow statement where we can see the starting cash at the start of a month, income and expense in the month and the ending cash with the values at the end of the month.

I got really excited to solve this using PowerPivot, so I decided to build a sample myself. First i had to create a fact table that contains the values of the income and expense, i used the picture from above to create sample data: As you can see we have income and expense for days in three months. To determine if a specific activity is income or expense, i created a secondary table that groups my activities: We can create a relationship between the two tables so we can put them in a table and calculate a measure with income – expense.

Next i created a separate related time table to use in time intel. functions: We create relationships between the date from the facttable and the new date table Now we can create the first table putting income and expense in one table per month using only the relationships between the tables and no DAX: Now for the interesting part, how do we create the cash flow table.

What we really need is a way to create 4 different calculations per row for each month on column, as we can see in our sample we need to have the following measures:

1. Starting cash= the total ytd of (sum of disbursements – sum of receipts) until the previous month
2. Receipts = sum of Receipts at the current month
3. Disbursements = sum of Disbursements at the current month
4. Ending cash = the total ytd of (sum of disbursements – sum of receipts) until the current month

These measure will translate to dax as the following:

1. TOTALYTD(Activities[Sum of Value](‘Group'[group] = “Receipts”) – Activities[Sum of Value](‘Group'[group] = “Disbursements”),DATEADD(‘Date'[Date],-1,MONTH))
subtract values from group disbursements from values of the group receipts for the totalytd until the previous month
2. Activities[Sum of Value](‘Group'[group] = “Receipts”)
values from group  receipts in the current month (current column context)
3. Activities[Sum of Value](‘Group'[group] = “Disbursements”)
values from group disbursements in the current month (current column context)
4. TOTALYTD(Activities[Sum of Value](‘Group'[group] = “Receipts”) – Activities[Sum of Value](‘Group'[group] = “Disbursements”),’Date'[Date])
subtract values from group disbursements from group  receipts for the totalytd until the end of the current month

But how can we do this in PowerPivot ? We need to determine what to calculate per row .. to do this we can use dynamic measures in DAX, as in this excellent post from MSFT Howie Dickerman.

First we need to be able to put values on the rows on which we can base our measures, to do this i created a new table in excel with rows and loaded it into PowerPivot: We now can put these row labels against months in a new pivottable: Now we can create a measure which uses the current row context to determine what to calculate at the measure. When we use the VALUES function we can see what the current row context for our Cashflow[CashFlow]  is and use this in an IF  statement in the measure to calculate a different measure for each row.

The formula will look like:

```= IF(COUNTROWS(VALUES( Cashflow[CashFlow])) =1,
IF( VALUES(Cashflow[CashFlow]) = "1 Starting Cash",  TOTALYTD(Activities[Sum of Value]('Group'[group] = "Receipts") - Activities[Sum of Value]('Group'[group] = "Disbursements"),DATEADD('Date'[Date],-1,MONTH)) ,
IF( VALUES(Cashflow[CashFlow]) = "2 Receipts",  Activities[Sum of Value]('Group'[group] = "Receipts"),
IF( VALUES(Cashflow[CashFlow]) = "3 Disbursements",  Activities[Sum of Value]('Group'[group] = "Disbursements"),
IF( VALUES(Cashflow[CashFlow]) = "4 Ending Cash", TOTALYTD(Activities[Sum of Value]('Group'[group] = "Receipts") - Activities[Sum of Value]('Group'[group] = "Disbursements"),'Date'[Date]) ,
BLANK())))), Activities[Sum of Value])
```
This will create one measure that depending on the row context shows different calculations.
Resulting the actual cashflow pivottable:
You can see that the values are calculated per cashflow type per month showing a different calculation. I hope this was what Greg ment 🙂
This example shows again the great power of DAX, a lot of things are possible. I still get really excited about PowerPivot.
The sample file can be downloaded from my skydrive.

## 10 Replies to “Building a cash flow statement in PowerPivot using dynamic measures in DAX”

1. WesleyB says:

Way to go Kasper 🙂
Keep ’em coming!

2. Sergey Volegov says:

Great article, PowerPivot can’t hope for better documentation than your blog posts 🙂

There might be a way to format measure formula for better readability, something like:
= IF
(
COUNTROWS(VALUES( Cashflow[CashFlow])) =1,
IF
(
VALUES(Cashflow[CashFlow]) = “1 Starting Cash”,
TOTALYTD(Activities[Sum of Value](‘Group’[group] = “Receipts”) – Activities[Sum of Value](‘Group’[group] = “Disbursements”),DATEADD(‘Date’[Date],-1,MONTH)),
IF
(
VALUES(Cashflow[CashFlow]) = “2 Receipts”,
Activities[Sum of Value](‘Group’[group] = “Receipts”),
IF
(
VALUES(Cashflow[CashFlow]) = “3 Disbursements”,
Activities[Sum of Value](‘Group’[group] = “Disbursements”),
IF
(
VALUES(Cashflow[CashFlow]) = “4 Ending Cash”,
TOTALYTD(Activities[Sum of Value](‘Group’[group] = “Receipts”) – Activities[Sum of Value](‘Group’[group] = “Disbursements”),’Date’[Date]) ,
BLANK()
)
)
)
),
Activities[Sum of Value]
)

3. Sergey Volegov says:

Oh well, WordPress stripped all whitespace from my comment
I was thinking of this: http://i.imgur.com/25fCY.png

4. Kasper de Jonge says:

@Sergey Volegov
Thanks, i made it somewhat better now i think using a addin to wordpress

5. Greg says:

Kasper,

This is exactly what I meant! Very nice. I’ll try it out.

Thanks,

Greg

6. duncanwil says:

I tried to download your file Kasper but got a message that it had been moved … and I couldn’t get it! Any advice?

1. Kasper says:

wow that is from an old post 🙂 I fixed the link!

7. Ear Suyheng says:

Dear Kasper,

Could you send me the sample file as your link drive is not working.
my email: esuyheng@gmail.com

Much appreciate for your help

1. Kasper says:
1. Ear Suyheng says:

Dear Kasper,
I am really inspired by your method but I have one problem with your formula that
I want the continuous opening cash year over year without restart every year.
I don’t know how to do it. Could you help to amend formula for me.
Thanks for you help and great post!
Regards,
Heng
=IF(COUNTROWS(VALUES( CF[CashFlow])) =1,
IF( VALUES(CF[CashFlow]) = “1 Starting Cash”, TOTALYTD([CFBal](CoA[Group] = “1 Sources of Fund”) + [CFBal](CoA[Group] = “2 Project Cost”),DATEADD(‘Calendar'[Date],-1,MONTH)) ,
IF( VALUES(CF[CashFlow]) = “2 Sources of Fund”, [CFBal](CoA[Group]= “1 Sources of Fund”),
IF( VALUES(CF[CashFlow]) = “3 Project Cost”, [CFBal](‘CoA'[Group] = “2 Project Cost”),
IF( VALUES(CF[CashFlow]) = “4 Ending Cash”, TOTALYTD([CFBal](CoA[Group] = “1 Sources of Fund”) + [CFBal](CoA[Group] = “2 Project Cost”),’Calendar'[Date]) ,
BLANK())))), [CFBal])

This site uses Akismet to reduce spam. Learn how your comment data is processed.