Pivot table showing too many rows with DAX measure - excel

Consider the following two tables
Major
Major Department
Department Name
Department Number
Produce
Produce
2
Produce
Taxable Produce
3
Frozen
Frozen
5
Grocery
Grocery
1
Grocery
Taxable Grocery
10
and Sales
UPC
Department
Category
SubCategory
Sales
1125
2
Fruit
Oranges
20
8256
5
Frozen Treats
Fruit Bars
15
9230
1
Snacks
Chips
28
4018
2
Fruit
Bananas
10
925
2
Vegetables
Onions
9
A relationship is created between the Department and Department Number columns.
I create the following pivot table:
What I want to do is add a measure which shows the total for the Major Department on each row.
I have tried MajorDepartmentSales:=CALCULATE(SUM(Sales[Sales]),ALL(Sales[SubCategory]),ALL(Sales[Category]))
which should remove the filters on category and subcategory. I would expect this to work, however it adds every category under the major department, but with the correct values.
Note, that the value of this measure is correct. It shows the totals under that particular major department. The problem is that it shows every category and subcategory under each major department whether they belong there or not. Why is this?
I have found two ways around this. The first modifies the measure to IF(COUNT(Sales[UPC])>0,CALCULATE(SUM(Sales[Sales]),ALL(Sales[SubCategory]),ALL(Sales[Category])),BLANK()) which checks if there are any items under that particular row, and blanks it out otherwise. The second method is to pull the major department to the Sales table with a calculated column MajorDepartmentOnSales:=Related(Major[Major Department]) and then using this column in the pivot table instead of the major department from the Major table.
Both produce what I want. The IF method seems a bit sketchy to me, however.
Question
My question is then why do I get these extra rows in the original approach?
It seems that DAX is correctly recognizing which major department is in play as it gets the value correct, but it is not recognizing that when it comes to filtering it out of the pivot table. I am really new to DAX, and it seems that I am not understanding something either in how the relationship is propagated down or how power pivot interacts with the pivot table.
How do I solve this? Is there a way to rewrite the measure to not cause these extra rows, or do I have to use one of these alternative methods? Ideally, I don't want to change the model. (The actual data in the real report has more tables and more (slightly different) columns than this example, but the example recreates the essential issue.)

Internal engin produce a cross-join for this combination of column/measure:
SELECT {[Measures].[Sum of Sales],[Measures].[MajorDepartmentSales]}
DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON COLUMNS , NON
EMPTY
Hierarchize(DrilldownMember(DrilldownMember(CrossJoin({[Table1].[Major
Department].[All],[Table1].[Major Department].[Major
Department].AllMembers},
{([Table2].[Category].[All],[Table2].[SubCategory].[All])}),
[Table1].[Major Department].[Major Department].AllMembers,
[Table2].[Category]), [Table2].[Category].[Category].AllMembers,
[Table2].[SubCategory])) DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON ROWS FROM
[Model] CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR,
FORE_COLOR, FONT_FLAGS
If you add a Sales[Department] then you get correct results (what you are expecting), Engine still produce a crossjoin (because of ALL() in measure) but also add a Department (out of crossjoin scoope) and corect relationship are used:
SELECT {[Measures].[Sum of Sales],[Measures].[MajorDepartmentSales]}
DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON COLUMNS , NON
EMPTY
Hierarchize(DrilldownMember(DrilldownMember(DrilldownMember(CrossJoin({[Table1].[Major
Department].[All],[Table1].[Major Department].[Major
Department].AllMembers},
{([Table2].[Department].[All],[Table2].[Category].[All],[Table2].[SubCategory].[All])}),
[Table1].[Major Department].[Major Department].AllMembers,
[Table2].[Department]), [Table2].[Department].[Department].AllMembers,
[Table2].[Category]), [Table2].[Category].[Category].AllMembers,
[Table2].[SubCategory])) DIMENSION PROPERTIES
PARENT_UNIQUE_NAME,MEMBER_VALUE,HIERARCHY_UNIQUE_NAME ON ROWS FROM
[Model] CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR,
FORE_COLOR, FONT_FLAGS
You can use such a workaround.
=IF(ISBLANK(SUM(Table2[Sales])); BLANK(); CALCULATE(SUM(Table2[Sales]); ALL(Table2[SubCategory]; Table2[Category])) )

Related

Excel Matching Customer Orders by Item and Quantity

Brief:
I have a large dataset, inside of which are Individual customer orders by item and quantity. What I'm trying to do is get excel to tell me which order numbers contain exact matches (in terms of items and quantities) to each other. Ideally, I'd like to have a tolerance of say 80% accuracy which I can flex to purpose but I'll take anything to get me off the ground.
Existing Solution:
At the moment, I've used concatenation to pair item with quantity, pivoted and then put the order references as column and concat as rows with quantity as data (sorted by quantity desc) and I'm visually scrolling across/down to find matches and then manually stripping in my main data where necessary. I have about 2,500 columns to check so was hoping I could find a more suitable solution with excel doing the legwork on identification.
Index/matching works at cross referencing a match for the concatenation but of course, the order numbers (which are unique) are different so its not giving me matches ACROSS orders.
Fingers crossed!
EDIT:
Data set with outcomes
As you can see, the bottom order reference has no correlation to the orders above it so is not listed as a match to the orders above but 3 are identical and 1 has a slightly different item but MOSTLY matches.

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.

Unable to Create a Sub-Total in the POWERPIVOT

I am pretty new with POWERPIVOT tables. I have searched for a bit of time now to resolve my problem but I have been unsuccessful so far. As you can see below, I have created a POWERPIVOT table in Excel 2013 that is composed of two FACT tables, which are based on: 1) a sheet where the clients can insert vote 1 budget entries; and, 2) another sheet where the clients can insert vote 5 budget entries. Also, a few DIMENSION tables have been added to the combination in order to link the Branch names and the expenditure type. Please note that this is only a simple example of what I am trying to produce.
However, my main problem is that I can't add a sub-total that would sum Salary, Operating and Revenues for Vote 1 and Vote 5 separately. What I would like to show is the following:
Please note that I have tried calculated columns and calculated fields at the best of my knowledge but the results are always showing another set of columns for Salary, Operating and Revenues but what I need is just one column that sum the three components so it displays Salary, Operating, Revenues and Sub-Total. Does anyone know how to resolve this problem that I am facing since a long time?
Thanks to gurus.
I would suggest that you start by putting both budgets in the one table and an extra column to designate Vote id., Otherwise I fearyou will need to add a calculated column concatenating the Branch and Expenditure Type in each table, then doing a LOOKUPVALUE from Vote1 on the concatenated column to Vote5 and pull back the value.

Showing unique values in a column along with another value associated with each

I have an Excel table containing transactions of companies. It looks like this:
Customer Phone Item Price
==================================
Company1
Company2
Company1
Company3
Company1
I need to see the unique values of customer column.
The result I want is:
Customer Phone Item Price
==================================
Company1
Company2
Company3
Here is what I tried already:
Remove duplicates: To just get the unique values, I can use the remove duplicates of excel. However, since this is something I will be doing frequently, I would rather not have to make a copy of the table each time in order to delete duplicates.
Pivot table: A pivot table does this job perfectly. My problem here is that I need other column info as well (e.g., the phone number) which I want to appear in the column next to the company. [I haven't yet figured out how to do this in a pivot table (i.e., to show a value as text instead of sum or count etc.)] - This would be the best option for me.
As long as you don't have a super long table where the recalculation will kill you, you could just add a calculated column like this and then filter on value being 1:
=IF(COUNTIF($B$1:B2,"=" & B3) > 0,0,1)
If recalculation is an issue, you can use the same or similar method with VBA except paste static values.
PivotTables are well suited for this kind of stuff, so that'd be my first go-to, but PivotTables are also frustratingly difficult to adapt to uses beyond their envisioned use... so there's a steep decline in their usefulness as you stray from that.
Ultimately, you may want to consider a relational database paradigm, and/or using Access. That's one step up the sophistication ladder towards managing the kind of data you're talking about.

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

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.

Resources