How to get Excel Pivot 'Summarize by' to return actual data values not sum, avg, etc? - excel

I am using Excel Powerpivot with data in two separate tables. Table 1 has ITEM level data with a BRAND characteristic. Table 2 has BRAND level data. The two tables are linked by the BRAND key. The measure I am using is non addable. i.e. the sum of the ITEMS does not equal the BRAND. The pivot is set up with ITEMS nested under BRANDS in the rows and the Measure in the column.
Excel assumes that I want to summarize ITEM to a BRAND level by applying SUM, MAX, MIN, AVG, etc. I would like to return the actual values from the appropriate ITEM or BRAND level table and not apply any calculations to the values. Is this possible?

If what you are effectively trying to do is produce a different result for the Brand rows (e.g. blank()) then the answer is to write a further measure that does a logic check to determine whether or not the row in question is an ITEM or a BRAND.
= IF (HASONEVALUE(table1[Item]), [Measure], Blank() )
Bear in mind that this will work for your current pivot but may not be adaptable to all pivots.
This assumes that you have explicitly created a measure called [Measure] and you are not just dragging the numeric column into the values box. If not you can create the initial [Measure] something like this:
= Sum(table1[Value])
Where Value is the column you want to use in the measure. Although you have used a sum, if it relates to a single item which has a single row in the table it will give the desired result.

Related

Power BI - RLS: adding label to a visual and aggregate values

I need to set some RLSs on some tables and columns, and for a specific Matrix I want to show all the all the other values, that are not shown to that specific user, summed up together as "Others". I give you an example.
I have a matrix which on the rows and columns has different product categories and the values are the impact in $ that each category has on the others (it is the sum of multiples rows under IMPACT column in the source table). Particularly, the rows are the impacted products while the columns the impacting ones.
The Matrix looks like this:
I want to create an additional label , OTHERS, which has to be displayed both as a column and row, which sums up all the values of the non-shown values. For instance, for the RED RLS categories I want to get a matrix like this:
FYI, I want to keep the totals of the rows and columns.
Does anybody have any ideas?
I tried to create some measures and calculated tables but I was not able to get the final view I want.

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 Rank by Date

I am Counting on Distinct ID's in a column - this is leading to the sum of the subtotals not equalling the grand total as follows:
What I want to do is rank the Payment Dates in cronological order and select ONLY the highest date to display. In the example above the Grand Total won't change, but the Townville row will not show a Distinct Student Count.
This is a very specific requirement and I'm assuming there's an easy way to do it in DAX - I've tried playing around with both RANKX and MAX but am no closer to solving this.
One last thing - the Rank must be contextual to the Time Filter selected by the user (so if they select 2015 it'd give the second record Rank 1 and the top record wouldn't show. If they select May 2015 it'd give the top record Rank 1 and the second record wouldn't show)
I think this is what you are looking for - I added a calculated column to the PowerPivot model that provides a rank based on the Last Payment Date and the Name of the Student. It will rank the earliest payment for any student as a 1.
The code for the column is as follows:
=RANKX(FILTER(Table1, [Student Name] = EARLIER([Student Name])), [Last Payment Date])
... assuming your table is named "Table1"!
The FILTER is the key that limits the ranking to dates belonging to students with that name only.
Update for Multiple tables
To set up relationships between the tables, go to the "Diagram View" of the model, available in the Home tab of the Power Pivot window.
You can drag fields from one table to the other to create relationships. This will only work if at least one of the fields is unique - it's a good idea to think of the model as a dimensional model, with a tables that acts like a fact and other tables around it that act like dimensions.
From the comment, I would try to get the Payments to act like the fact, and have it link to the Community and Student tables. in this case, you could then have the following code:
=RANKX(FILTER(Table1, Related('Students'[Student Name]) = EARLIER('Students'[Student Name])), [Last Payment Date])
This calculated column would be on your Payments Fact table, and it uses a lookup to a related field.
Note that in this specific case, it would be easier to just run the filter over your Student ID field that is used to lookup the Student name.

How to add calculated column to a pivot table connected to SSAS tabular model?

Or in another words the question is - how to add some calculation in the pivot table based on columns which do not exist in model level.
I've reproduced my problem using AdventureWorksDW2014 sample database.
Let's say I want to calculate difference between Actual and Budget scenario amounts in the FactFinance table for each Organisation and present it in a form of pivot table.
To achieve that I've created a simple model (screen above) and added SumOfAmount measure to the FactFinance table SumOfAmount:=SUM([Amount])
Next, I've opened my model in Excel and created very simple pivot table (shown below)
So, (the question part) now I want to add an extra column to my pivot table, which should calculate something (for example difference) between columns Actual and Budget. And I want this new column been a part of the pivot table so I could filter it or\and add new grouping levels without necessity to change something "outside" the pivot table.
TRIED SO FAR
I tried to add Calculated Field but it seems like I can only use "real" columns for calculations. Columns which appeared in a pivot table based on values from COLUMNS quadrant can't be used as sources for calculations.
FINAL SOLUTION
I got it finally combined two pivot tables: the old one and the one with Diff measure, defined as Diff:=[Actual Amount]-[Budget Amount], where
Actual Amount:=Calculate([SumOfAmount];'DimScenario'[ScenarioName] = "Actual")
Budget Amount:=Calculate([SumOfAmount];'DimScenario'[ScenarioName] = "Budget")
as #WimV suggested
First calculated measure is good:
SumOfAmount:=SUM('FactFinance'[Amount])
Add the following Calculated measures (if needed mark as hidden):
Budget Amount:=Calculate([SumOfAmount],'DimScenario'[Scenariokey] = "Budget")
Actual Amount:=Calculate([SumOfAmount],'DimScenario'[Scenariokey] = "Actual")
You can use the new calculations for example in a difference calculations

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