How do I SUMIF one PowerPivot table according to the rows of a second PowerPivot table? - excel

I have two tables: one of customers ("Donor"), and one of transactions ("Trans"). In Donor, I want a "Total" column that sums all the transactions by a particular Donor ID, which I would calculate in a standard Excel table thus:
=SUMIF(Trans[Donor ID],[#ID],Trans[Amt])
Simple! How do I do the same thing with a DAX formula? I thought
=CALCULATE(SUM(Trans[Amt]),Trans[Donor ID]=[ID])
would do it, but I get the error
Column "ID" cannot be found or may not be used in this expression.
Strangely, when I use
=CALCULATE(SUM(Trans[Amt]),Trans[Donor ID]=3893)
I do get the total for ID 3893.
Eschewing CALCULATE, I did find that this works:
=SUMX(FILTER(Trans, Trans[Donor ID]=[ID]),[Amt])
...but it only allows the one filter, and I'll need to be able to add more filters, but:
=SUMX(CALCULATETABLE(Trans, Trans[Donor ID]=[ID]),[Amt])
...(which I understand is like FILTER but allows for multiples) does not work.
Can you identify what I'm doing wrong?

After putting together a quick model that looks like this:
I've confirmed that this DAX forumla works as a Calculated Column in the Donor table:
=CALCULATE(SUM(Trans[Amt]), FILTER(Trans, Trans[Donor] = Donor[DonorKey]))
The key here is to make sure that the relationship between the two tables is correctly configured, and then make sure to use the combination of CALCULATE() and FILTER() -- filtering the trans table based on the current donor context.

Related

How do I create a measure in Power Pivot that pulls a value from another table?

I have two tables that use a unique concatenated column for their relationship. I simply want to make a measure that uses the values from C4 of Table1. I thought I could use a simple formula like =values(Table1[C4]) but I get an error of "A table of multiple values was supplied where a single value was expected."
Side note: I realize the concatenation is unnecessary here in this simple example, but it is necessary in the full data I am working with which is why I added it into this example.
Here's a simplified set of tables for what I am trying to do:
Table1
Table2
Relationships
First you should think. Do I really need a Calculated column? Can't this be calculated at runtime?
But if you still want to do it, you can use RELATED or RELATEDTABLE.
Keep in mind if you are pulling from RELATEDTABLE, returns many values. So you should apply some type of aggregation like SUMX or MAXX.
You can use context transition to retrieve the value.
= CALCULATE(MAX(Table1[C4])

Excel Pivot Table - Uneven group interval

I have around about 12,000 individual accounts with balances ranging from .01 to over 5mm. I want to group them in a column header so that I know the number of accounts that fall into each range as well as the summed up value of those accounts. I know how to create my pivot table to do this except for the grouping. I can only get one group to work (0-100,000 and >100,000). What I need is:
0-100,000; 100,000-1,000,000; 1,000,000-5,000,000; 5,000,000+
Can this be done? Manually grouping them isn't very viable given that I have 12,000 different account balances....and I wouldn't really want to do that manually anyway.
Can you add a calculated column to the source of the data with a formula to return the categories you want? A nested "If" statement should handle it with ease. Something along the line of:
=IF(AND(BALANCE>0,BALANCE<100000),"0-100,0000",IF(AND(BALANCE>=100000,BALANCE<1000000),"100,000-1,000,000"))
Just add in the rest of the IF(AND(X,Y),"Display text") to generate your categories.
You could skip the Pivot table entirely and use COUNTIFS
=COUNTIFS(A$1:A$10,">"&0,A$1:A$8,"<="&100000)
=COUNTIFS(A$1:A$10,">"&100000,A$1:A$10,"<="&500000)
And so on
For the last category you can use a COUNTIF
=COUNTIF(A$1:A$10,">"&5000000)
(My formulas assume the data is in Range A1:A10)

How to add multiple measures into a pivot table?

I'm quite new to using pivot tables and data models, so I don't even know if what I want to do is possible. I have a pivot table (PivotTable1) and its source (Table 25) and I would like to add a hundred or so measures which are listed in the TableCombinations.
For example, I entered the two first measure in orange, but they are not linked to TableCombination and entering them all one by one would be quite long. Each measure is for a distinct Sum wfn column that sums all other rows multiplied by a coefficient. The TableCombinations table simply states the coefficient to be used for each column. For the first three rows, these are my measure formulas :
sum wf1=1.4*Table25[Sum of wD]+0*Table25[Sum of wL]+0*Table25[Sum of wS]+0*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
sum wf2=1.25*Table25[Sum of wD]+1.5*Table25[Sum of wL]+1*Table25[Sum of wS]+0*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
sum wf3=1.25*Table25[Sum of wD]+1.5*Table25[Sum of wL]+0*Table25[Sum of wS]+0.4*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
...
Two questions :
Is there a way to link the tables so that any change made to TableCombination would then be updated in the pivot table measures?
Is there a way to generate all the of the measures without typing them in one by one.
You should be able to use just one DAX measure to do this, using the CROSSJOIN function.
Don't set up a relationship between the Tables, and drag # to the Columns area of the PivotTable. Then create this Measure:
=SUMx(CROSSJOIN(Table1,Table2),Table1[wD]*Table2[wD]+Table1[wL]*Table2[wL]+Table1[wS]*Table2[wS]+Table1[wW]*Table2[wW]+Table1[wWSOUL]*Table2[wWSOUL])
That should give you the exact answer you need.
Here's how it looks using some sample data:
...and here's the sample data I'm using:
You could certainly use VBA to add measures, and to update them when the Table changes. I might have a crack at writing up an answer along that approach shortly. But here's another way to achieve what you want.
I've previously written some code to slave a Table to a PivotTable, so that any change in the PivotTable's dimensions or placement will be reflected in the shadowing Table's dimensions and placement. This effectively gives us a way to add a calculated field to a PivotTable that can refer to something outside of that PivotTable. If the PivotTable grows, the Calculated Table will grow. If the PivotTable shrinks, the Calculated Table will shrink, and any redundant formulas in it will be deleted.
You can easily use this approach to perform your calculations in a 2nd table alongside your PivotTable, and each column x in that 2nd table could easily reference row x in your 'parameters' table.
See Select Newest Record and Create New Table of Unique Values in Excel

DAX - Display the top 1 count in a calculated column

I have a [Company] column, I have a [Billing day] column in a powerpivot sheet.
A Company can have several different Billing days.
I want to show in a Pivot table, next to the company name, the Billing day with the highest number of occurences.
The tricky part is that the number must appear in a [Calculated Column] that will not be put as a Measure in my Pivot table, but as a Column.
I have reviewed such posts as:
this one or this one.
I have learned much but I am still unable to get the correct values.
Any idea ?
Many thanks
This may not be the best way to get you what I understand you are asking for, but it is a way. I believe it achieves what you want regarding the calculated column.
If I start with this table as Table1:
Then I add a column using this code: MaxByDate = COUNTX(FILTER(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),Table1[Billing Day]=EARLIER(Table1[Billing Day])),Table1[Billing Day])
And I add another column using this code: MaxOverall = MAXX(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),COUNTX(FILTER(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),Table1[Billing Day]=EARLIER(Table1[Billing Day])),Table1[Billing Day]))
Then I add one last column using this code: DateOfMaxOverall = FORMAT(SUMX(FILTER(FILTER(Table1,[Company]=EARLIER([Company])),[MaxByDate]=[MaxOverall]),[Billing Day])/COUNTX(FILTER(FILTER(Table1,[Company]=EARLIER([Company])),[MaxByDate]=[MaxOverall]),[Billing Day]),"m/d/yyyy")
I get this table:
Then if I choose to create a Flattened PivotTable from it (Home tab / PivotTable dropdown arrow button / Flattened PivotTable), and set things up like this:
...and turn off the subtotals and grand totals, I get this:
Which is, I believe, what you requested.
But I think a simpler approach might be this:
Start with the first table (Table1):
And, using that table as is, create a Flattened PivotTable directly. Then set the PivotTable up like this:
...and turn off the subtotals and grand totals, to get this:
...Which you can then filter for the max occurring day for each company:
...to get this:

Avoid DISTINCTCOUNT in PowerPivot

Due to performance issues I need to remove a few distinct counts on my DAX. However, I have a particular scenario and I can't figure out how to do it.
As example, let's say one or more restaurants can be hired at one or more feasts and prepare one or more menus (see data below).
I want a PowerPivot table that shows in how many feasts each restaurant was present (see table below). I achieved this by using distinctcount.
Why not precalculating this on Power Query? The real data I have is a bit more complex (more ID columns) and in order to be able to pivot the data I would have to calculate thousands of possible combinations.
I tried adding to my model a Feast dimensional table (on the example this would only be 1 column of 2 rows). I was hoping to use that relationship to be able to make a straight count, but I haven't been able to come up with the right DAX to do so.
You could use COUNTROWS() combined with VALUES().
Specifically, COUNTROWS() will give you the count of rows in a table. That means COUNTROWS is expecting a table is input. Here's the magic part: VALUES() will return a table as results, and the table it returns are the distinct values in the table/column that you provide as the argument for VALUES().
I'm not sure if I'm explaining it well, so for the sample data you provided, the measure would look like this (assuming the table is named Table1):
Unique Feasts:=COUNTROWS(VALUES('Table1'[Feast Id]))
You can then create a pivot table from Powerpivot, and drag Restaurant Id into Rows, and drag the measure above into Values. Same result as DISTINCTCOUNT, but with less performance overhead (I think).

Resources