March 13, 2023
I'm a new member, Excel power-user, intermediate level in PowerQuery/PowerPivot. I don't write code or macros, but I am good at looking at data and seeing inconsistencies linked to bad inputs or incorrect processing.
I'm on Excel 2013 with required add-ins, if it matters.
I've stumbled on a topic that is not frequently discussed online, and the answers are often over simplistic.
I'd love to hear from people with experience in this particular annoyance:
- I start with a table that holds items & quantity values, in columns. I'm interested in calculating sums & averages
- The difficulty is **weighted** average (eg average price paid for various purchases of different quantities of same item at different unit price). Most answers online cover straight averages, only.
1) a standard pivot table produces the subtotal sums by item, and a Calculated Field (price paid) / (quantity purchased) will produce
- the unit price paid per table line item. Not interesting & collapsed unless I want to drill down
- the subtotal row will show the sum of (price paid) and the sum of (quantity), AND Excel will use those 2 values for computing the ratio, which happens to be the correct, weighted average price paid. Great.
2) transpose this to PowerQuery/PowerPivot, and things go subtly wrong:
- I use PowerQuery to combine several data tables, and an output Connection (only) since I'm not interested in the long output table
- I use PowerPivot to "Manage the Data Model" and visualise the table. I have 2 options at this point:
a) add (in the memory table) a CalculatedColumn with the same formula above. Results in the table are fine.
I then create a pivot table, using that query as input. The line item averages are fine, BUT the rolled up subtotal calculates the average unit price of all the unit prices, NOT weighted. The average is calculated on the vertical axis / column only, even though the (sum of price paid) and (sum of quantity are correct). There is a valid number, but it doesn't have the correct average value!!!! (it's the unweighted average)
b) I do NOT create a CalculatedColumn, and instead I create a CalculatedField from the PowerPivot menu (the calculated field from the PivotTable menu is greyed out...). I use the same formula as above, and Excel returns an obscure error "Calculation error in measure.... The value cannot be determined."
This seems incorrect, since I use the built-in formula editor to avoid typos, and the formula produces valid results in scenario 1 and 2A.
I've been reading about Measure vs CalculatedField. I don't believe I'm doing (or need) a Measure.
My calculation is step-by-step, by row, only.
=> how can I get PowerPivot to work on row values at Subtotal level?
The easy way out for now is to skip PowerQuery/PowerPivot and just use a simple pivot table.
It bothers me to have all this computing power and still do things the old-fashioned way.
November 1, 2018
March 13, 2023
I learned something today, and at 57 that doesn't happen often 🙂
Measure it is, if you say so. I need to read more on that!
If anyone is reading this thread later, note that the reply above doesn't fully solve the issue.
What it produces is the "grand total weighted average of all unit rates"....across ALL items and ALL transactions.
The subtotals and individual unit rates appear ALL equal to the grand total value, which is not correct. And I can't drill down to check further. Some kind of filter condition needs to be added to group the sums by item name, and produce correct average weighted unit rate by item.
As I wrote in the intro, it's a subtle issue, and easy to get it wrong: a bad number looks as good as a good number if we're not paying close attention!
It seems like a lot of extra formula efforts to do, to produce the same results as the very easy option #1.
I'm sure there is a way, but I'll follow the path of least resistance for the moment.
Thanks for the lesson & tip
November 1, 2018
From your description, I'd say your relationships are not set correctly, or there's an issue with which fields you are using in the pivot table. Filters only go from the one side to the many side in the relationship, not the other way in PP.
As an aside, PP is great when you have a well designed model and an absolute nightmare when you don't. As a general rule I find that the more complicated the DAX you have to write is, the more flawed your model.
March 13, 2023
Don't get me wrong, I am not trying to argue with you. I'm only replying for the sake of future readers, since I had so much difficulty finding good/definitive information on this thorny question:
- There is no relation setup in my case. Its a raw inventory list with 1 line per each item entry & purchase.
Single flat table with several columns. PowerQuery just appends several sources.
- I agree that a poorly designed model will lead to headaches. This is the reason why I struggle: my table is already normalised with values everywhere, repeated as often as required to make it very easy to analyse without worrying about relations.
- The likely answer to my PP question is something like SUMIF(item=XXX, price paid) / SUMIF(item = XXX, quantity purchased). I do NOT know the correct command or syntax, so future readers should read the manual if they want to explore further. The important point is that some kind SUM() function is required to satisfy the Measure criteria..
In my case, I am trying to avoid a SUMIF() scenario, it's not elegant for what I'm doing.
I was hoping to do better than a simple pivot table, but maybe PowerPivot is not a better tool for this scenario.
March 13, 2023
Bing Chat provided another suggestion: stay with PowerQuery and Group rows according to item names, then add CalculatedFields for the unit rates.
It proposed a simple code example that does produce the correct summary Table, neatly.
Unfortunately, I loose the ability to drill down with this approach, so I'm back to old-school Pivot Table.