Average of Averages by Row in Pivot Table

ollieotis

New Member
Joined
Jun 6, 2006
Messages
44
Hello,

First off, the official disclaimer... I'm new to power pivot, but just finished reading Rob and Avi's book, and have spent the last few weeks learning as much as I can. Apologies if I'm using incorrect terminology or am asking an obvious question, but the solution to the problem that led me to power pivot in the first place continues to evade me. I've made what I feel to be a lot of headway in other areas but this one is killing me, and no amount of forum searching has lead me to an elegant solution. I think I know why this isn't simple, but not sure I can explain it succinctly. Side note: power pivot is AMAZING and I keep finding myself asking where it has been all my life!

The problem:

I'm calculating Days Sales Outstanding (DSO) per month, or AR Balance / Average Daily Sales. I need a dynamic table, where I can modify the filter context numerous times based on need, e.g. by customer, region, state, etc., not just by a single column or filter. From there, I need the monthly average of the results generated by the measures. The table below outlines what I'm looking for, with the Jan-Mar DSO results calculated as:

DIVIDE(sumx('DSO_Base_Data','DSO_Base_Data'[201701 _AR Balance]),(SUMX('DSO_Base_Data','DSO_Base_Data'[201701 Invoice])/92))

Where my source data may be 10-15 lines per customer ID, or per person responsible, etc.

The average column below is just a simple Average() formula bolted on the side for illustrative purposes, but highlights ultimately what I'm trying to get into my pivot table.

CUSTOMERIDJan DSOFeb DSOMar DSOAverage
CID000501100106105104
CID00063658737970
CID000969775281121
CID001159146171179165
CID00162313568108104
CID002271135169216173
CID003183165220171185
CID003222909090
CID00863976699379
CID01280044658765
CID01702963799077
CID21850463638871
Grand Total329110376

<tbody>
</tbody>

Some of the research I've done leads me to potential use of the summarize() function, but I don't seem to have that in my list of formulas (using 2010).

Any ideas how I might approach this one?

Many thanks in advance for any help provided!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Thank you, Matt. I'm probably missing something obvious here, but I used sumx to ensure I got the correct grand total at the bottom of the pivot table, and did an offline check to confirm that calculation came out correctly. After reading your article though (which was a good refresher from the book I just finished), I'm thinking sum would have worked just as well in this case. The row and filter context concepts, and difference between aggregator and iterator functions are starting to become clearer to me, but I'm not seeing how that will help me here.

The grand total is correct, but what I'm looking for is a fourth column that calculates the average of that row's measure results. The following (from a smaller file used to test) seems to work, but I need to account for periods where there are no values:

divide('DSO_Base_Data'[Jan DSO]+'DSO_Base_Data'[Feb DSO],2)

Maybe I'm overthinking this? Just need a way to make the denominator account for months where there are no values and the above would seem to work.

Thanks!
 
Last edited:
Upvote 0
I ended up solving the issue, although I would say my solution doesn't feel very elegant:

First, to account for periods with no values in my denominator, I used the following measure to return a value of 1 or blank:

IF((calculate(countx('DSO_Base_Data',DSO_Base_Data[Jan DSO]),FILTER('DSO_Base_Data',countx('DSO_Base_Data',DSO_Base_Data[Jan DSO])<>Blank())))=Blank(),Blank(),1)

After replicating the measure for the other two months, I then used a simple divide function:

divide('DSO_Base_Data'[Jan DSO]+'DSO_Base_Data'[Feb DSO]+'DSO_Base_Data'[Mar DSO],'DSO_Base_Data'[Count Jan]+'DSO_Base_Data'[Count Feb]+'DSO_Base_Data'[Count Mar])

This seems to work no matter the filter context applied in the pivot table. Just feels really clunky, and will get painful once I build out a 12-24 month view, and have to keep updating manually for trailing views.
 
Upvote 0
Sorry, but I can't really follow your descriptions. Are you using daxformatter.com rules of formatting? ie a column is prefixed by the table name, but a measure is not? I suspect you are not, but can't tell. If you can explain the structure of the underlying data, either by giving the column names, or a screenshot I'm sure someone can help you.

Apologies if this is what you've done, but you should be trying to create a single measure that calculates all of the cells in the table. You would then want to drag CUSTOMERID into the rows, and MONTHS into the columns. Let's assume that you've managed to write the measure that calculates the values for each cell in the table and called it [DSOMeasure], then to get your average column on the end it would be something like.

Code:
=AVERAGEX(
    VALUES ( Dates[Months] ),
    [DSOMeasure]
)

AVERAGEX is an iterator and will iterate through every month returned by VALUES ( Dates[Month] ). VALUES() operates in the existing filter context, so in a column for a given month it will iterate for one month only and return the average, which is just [DSOMeasure]. In the total column, there is no month filter context, so it will do the average of [DSOMeasure] for each month, ie the desired result. NB this only works because [DSOMeasure] is a measure and so context transition is carried out, if you replaced that with just DAX code you would need to wrap it in a CALCULATE() to get the same effect.
 
Last edited:
Upvote 0
Thank you, Gazpage. Let me see if I can explain this a little better.

My output for this test case at this point looks like the following (will be part of a much more complex table with other metrics later on, where month column headers won't work):

CUSTOMERIDJan DSOFeb DSOMar DSOAverage
CID0010251049310299
CID0014050303020
CID00277556647063
CID002843909090
CID00298821563236
CID00548218489052
CID00646789803166
CID00768033332630
Grand Total48675657

<tbody>
</tbody>

My data table is structured as follows:

Project201701 _AR Balance201702 _AR Balance201703 _AR Balance201701 Invoice201702 Invoice201703 Invoice
A (140,730) (141,255) 1,575 (99,636) (82,089) (115,294)
B (113,203) (52,052) 8,243 198,966 197,104 193,052
C (113,152) (121,629) (162,015) (34,384) (85,620) (123,159)
D - 2,385 2,590 - 2,385 2,590
E 4,308,349 3,441,112 3,656,067 6,398,352 6,240,404 5,563,036
F 5,240,348 5,139,773 5,560,221 4,507,568 4,878,939 4,912,535

<tbody>
</tbody>


The first measure I wrote to get DSO for each month is:

Jan DSO = divide(sumx('DSO_Base_Data','DSO_Base_Data'[201701 _AR Balance]),(SUMX('DSO_Base_Data','DSO_Base_Data'[201701 Invoice])/92))

where 92 is number of days in 3 month period and invoice value is sum of invoices for those 3 months. repeat for Feb & Mar

The second measure I wrote provides me with a count of 1 or blank() per month (deleted table references before measures - not quite sure how to do that when writing the formulas out, and daxformatter.com returned same code):

Jan Count = IF((calculate(countx('DSO_Base_Data',[Jan DSO]),FILTER('DSO_Base_Data',countx('DSO_Base_Data',[Jan DSO])<>Blank())))=Blank(),Blank(),1)

repeat for Feb & Mar

The average measure is a function of the sum of the count values and sum of the DSO values (again, deleted table references from measures):

Jan - Mar Average DSO = divide([Jan DSO]+[Feb DSO]+[Mar DSO],[Count Jan]+[Count Feb]+[Count Mar])

I tried replicating this for a 12 month period last night, and it crashed on me and I lost a bunch of work. No big deal, but I'm trying to incorporate this analysis into a more complex table that incorporates data from several other data sources, where months aren't applicable in the column header. Based on what you said, I'm thinking I can restructure my data table to include months in a column, but that won't let me solve for my end table that I'm looking for. At least I don't think it will.

As always, thank you so much for helping me with my problem! Hopefully this is a little easier to understand now.
 
Last edited:
Upvote 0
Yes. I can now see that your data is not well structured for use in Power Pivot.

You want the data to have 4 columns:

Project
Month
Item
Value

Where Item is either AR Balance or Invoice. Use Power Query to do that, transpose your table and then unpivot the A to F columns that you will end up with.
 
Upvote 0
Awesome, thanks Gary! I'm not sure I've fully grasped it all yet, but the point on data structure makes perfect sense and you've given me what I need to figure it out from here. I can't download the file, but I can easily modify my initial query to fit this format (and will remember to set all of them up like this moving forward).

Again, thank you!
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,541
Latest member
iparraguirre89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top