DAX % of total count if measure qualifies criteria - powerpivot

DAX 2013 standalone power pivot.
I have a sales table with Product and Brand columns, and Sales measure which explicitly sums up sales column.
Task in hand: I need to create 1 measure RANK which would ...
if Product is filtered expressly, then return count of Products that have higher or equal sales amount, divided by total count of products.
If it's a subtotal brand level, show the same but for brands.
My current approach is using RANK and then MAXX of rank which seems working but a no-go - slow nightmare. Excel runs out of memory.
Research: it's been a week. This is the most relevant post i found anywhere, this question here , but it's in MDX.
In my example picture, I'm showing Excel formulas with which I can get to the result. Ideally there shouldn't be any helpers, 1 formula for all.
I.E.
RANK:=IF( HASONEFILTER(PRODUCTS[PRODUCT], HELPER_PROD, HELPER_BRAND)
where HELPER_PROD part would be something like this - need to find a way to refer to "current" result in pivot table like Excel does using [#[...:
HELPER_PROD:=COUNTX(ALL(PRODUCTS), [SALES]>=[#[SALES]]) / COUNTX(ALL(PRODUCTS))
HELPER_BRAND:=COUNTX(
DISTINCT(ALL(PRODUCTS[BRAND])),
[SALES]>=[#[SALES]]) /
COUNT(DISTINCT(ALL(PRODUCTS[BRAND]))

You can use the "Earlier" function to compare with the current record.
ProductsWithHigherSales:=CALCULATE(countrows(sales),
FILTER(all(Sales),
countrows(filter(Sales,Sales[Sales]<=EARLIER(Sales[Sales])))
))
Using Earlier function in measures: can-earlier-be-used-in-dax-measures
Used workbook: Excel File

Related

Creating a Calculation in an Analysis cube to produce a Distinct Count by criteria

I have a multi-dimensional cube that has multiple rows for each shop. There is a ShopCount measure that is a DistinctCount over the ShopKey field in the cube, which is in another measure group. I can get Shop counts over all sorts of different dimensions, which in this case is usually location. That works fine.
Now I want a variant of this. I want an inline distinct count of shops based on another measure or dimension.
Here is an example mdx that gives me a distinct count of shops for a particular month, where the shop type is either automotive or camping.
SELECT [Measures].[Shop Count] ON COLUMNS
FROM [Retail Cube]
WHERE ([Report Date].[Month].[201905],
{[Shop].[ShopType].&[Automotive],[Shop].[ShopType].&[Camping]})
CELL PROPERTIES VALUE
In Excel, I would like to be able to get another column that has the distinct count of Automotive and Camping shops over the range of months in my database. I would like to then be able to filter the columns by all the existing dimensions that I am currently filtering by.
I tried creating a calculated field in the Calculations tab, such as:
COUNT(DISTINCT CASE WHEN [Shop].[ShopType].&[Automotive] THEN [Shop].[Shop Key]
WHEN [Shop].[ShopType].&[Camping] THEN [Shop].[Shop Key]
ELSE NULL END)
(Note: Shop Key is what I do my Distinct Count over)
After substantial processing it came up with an error in that column.
How can I achieve what I am trying to do?

Subtotal by unique store with multiple sales staff over period

I am trying to gather various KPI's for salespersons in multiple stores. The goal is to break down store performance into salesperson level.
I am facing an issue when trying to add a hitrate, as this is normally only by store. Number of quotes given / Visitors.
Even though it may not be 100% accurate I still wish to have the KPI by sales person. I am able to do this on a sales person level, but my subtotal for the store is incorrect, as it makes a summation of visitors by sales persons.
Monthly period is to be considered to as sales persons comes and goes throughout the period. Example of what I wish for a Subtotal for the measure "Vis". Store X 370 for month 1,2 & 3. For Store Y 395.
Vis measure = Visitor (Calculation i have tried but gives the wrong result for the store total for the period.)
I have tried various Calculate, Sum, max functions, but nothing seems to provide the result I need.
I hope that someone might be able to help me get along with this.
Example data tables is link as shown below:
enter image description here
Thanks in advance.
This sounds like a case where the HASONVALUE function would be useful.
The idea being that you would the result of that function in an if to determine if you are calculating at your sales person level or a the store level which should contain multiple salespersons. Thus you would have two different calculations, one for the sales person and store combination, and one for the Store level.
Example would be sometime like the following, in this example I am assuming you have a sales person table:
Measure:= IF( HASONEVALUE( Salesperson[Sales Person] ),
[Vis],
[Measure for subtotal]
)
[Measure for Subtotal] would just being the calculation that you want for your store total.
Of course if you filter to just a single sales person, then the totals for the store will just match that sales person.

PowerPivot Ranking Groups using DAX's Rankx - Ranking Using Sum of a Field

Am trying to rank groups by summing a field (not a calculated column) for each group so I get a static answer for each row in my table.
For example, I may have a table with state, agent, and sales. Sales is a field, not a measure. There can be many agents within a state, so there are many rows for each individual state. I am trying to rank the states by total sales within each state.
I have tried many things, but the ones that make the most sense to me are:
rankx(CALCULATETABLE(Table,allexcept(Table,Table[AGENT]),sum([Sales]),,DESC)
and
=rankx(SUMMARIZE(State,Table[State],"Sales",sum(Table[Sales])),[Sales])
The first one is creating a table where it sums sales without grouping by Agent. and then tries to rank based on that. I get #ERROR on this one.
The second one creates a table using SUMMARIZE with only sum of Sales grouped by state, then tries to take that table and rank the states based on Sales. For this one I get a rank of 1 for every row.
I think, but am not sure, that my problem is coming from the sales being a static field and not a calculated measure. I can't figure out where to go from here. Any help?
Assuming your data looks something like this...
...have you tried this:
Ranking Measure = RANKX(ALL('Table'[STATE]),CALCULATE(SUM('Table'[Sales])))
The ALL('Table'[STATE]) says to rank all states. The CALCULATE(SUM('Table'[Sales])) says to rank by the sum of their sales. The CALCULATE wrapper is important; a plain SUM('Table'[Sales]) will be filtered to the current row context, resulting in every state being ranked #1. (Alternatively, you can spin off SUM('Table'[Sales]) into a separate Sales measure - which I'd recommend.)
Note: the ranks will change based on slicers/filters (e.g. a filter by agent will re-rank the states by that agent). If you're looking for a static rank of states by their total sales (i.e. not affected by filters on agent and always looking at the entire table), then try this:
Static Ranking Measure = CALCULATE([Ranking Measure], ALLEXCEPT('Table', 'Table'[State]))
This takes the same ranking measure, but removes all filters except the state filter (which you need to leave, as that's the column you're ranking by).
I did figure out a solution that's pretty simple, but it's messier than I'd like. If it's the only thing that works though, that's okay.
I created a new table with each distinct state along with a sum of sales then just do a basic RANKX on that table.

Excel PowerPivot Help: Totals is not summing the Absolute Value correctly

I know PowerPivot is not programming, but I wanted to see if I could get help or a recommendation on how to get the total rows to correctly calculate the sum of Absolute Values at a higher aggregation level that the detailed data in a PowerPivot data model.
Here is my example: This is detailed data format. There are 2 hierarchies:
1. Product Group\Product Family\**Material** '3 columns'
2. Region\**Plant**\SalesMgr\Employee\Customer '5 columns'
Hierarchy represents 8 columns in my data model
All data in the data model is at the lowest level (i.e. Material/Customer). However I want to calculate Forecast Accuracy at Material/Plant Level (a higher aggregation level).
DataModel Column9: Sales 'Sales at the detailed Material/Customer'
DataModel Column10: Forecast 'Forecast at the detailed Material/Customer'
Calculated Column11: AbsoluteError = ABS(Forecast - Sales) 'calced at Mat/Cust'
PowerPivot DAX formulas/measures, shortened for simplicity
TotalSales:=SUM(Sales)
TotalAbsError:=SUM(AbsoluteError)
MAPE:= TotalAbsError/TotalSales
ForecastAccuracy:= 1 - MAPE (Mean Absolute Percentage Error)
This calculates the data correctly at the Material/Plant level when I create my Pivot Table. However the Total values in the Pivot Table are incorrect.
For example, the Total of AbsError is the Sum of 20 rows of Material/Customer Combinations. I need the Total of only the 5 Material/Plant Combinations. The 20 rows of detailed Material/Customer data aggregates up to only 5 rows of Material/Plant Combinations.
I have a sample spreadsheet I can share. Thanks Dimitry for updating the question!
I would like to post the spreadsheet / powerpivot so that the StackOverflow community can see it. I assume I have to post a link. I will start trying to figure out how.
Thanks
David

MDX Query SUM PROD to do Weighted Average

I'm building a cube in MS BIDS. I need to create a calculated measure that returns the weighted-average of the rank value weighted by the number of searches. I want this value to be calculated at any level, no matter what dimensions have been applied to break-down the data.
I am trying to do something like the following:
I have one measure called [Rank Search Product] which I want to apply at the lowest level possible and then sum all values of it
IIf([Measures].[Searches] IS NOT NULL, [Measures].[Rank] * [Measures].[Searches], NULL)
And then my weighted average measure uses this:
IIf([Measures].[Rank Search Product] IS NOT NULL AND SUM([Measures].[Searches]) <> 0,
SUM([Measures].[Rank Search Product]) / SUM([Measures].[Searches]),
NULL)
I'm totally new to writing MDX queries and so this is all very confusing to me. The calculation should be
([Rank][0]*[Searches][0] + [Rank][1]*[Searches][1] + [Rank][2]*[Searches][2] ...)
/ SUM([searches])
I've also tried to follow what is explained in this link http://sqlblog.com/blogs/mosha/archive/2005/02/13/performance-of-aggregating-data-from-lower-levels-in-mdx.aspx
Currently loading my data into a pivot table in Excel is return #VALUE! for all calculations of my custom measures.
Please halp!
First of all, you would need an intermediate measure, lets say Rank times Searches, in the cube. The most efficient way to implement this would be to calculate it when processing the measure group. You would extend your fact table by a column e. g. in a view or add a named calculation in the data source view. The SQL expression for this column would be something like Searches * Rank. In the cube definition, you would set the aggregation function of this measure to Sum and make it invisible. Then just define your weighted average as
[Measures].[Rank times Searches] / [Measures].[Searches]
or, to avoid irritating results for zero/null values of searches:
IIf([Measures].[Searches] <> 0, [Measures].[Rank times Searches] / [Measures].[Searches], NULL)
Since Analysis Services 2012 SP1, you can abbreviate the latter to
Divide([Measures].[Rank times Searches], [Measures].[Searches], NULL)
Then the MDX engine will apply everything automatically across all dimensions for you.
In the second expression, the <> 0 test includes a <> null test, as in numerical contexts, NULL is evaluated as zero by MDX - in contrast to SQL.
Finally, as I interpret the link you have in your question, you could leave your measure Rank times Searches on SQL/Data Source View level to be anything, maybe just 0 or null, and would then add the following to your calculation script:
({[Measures].[Rank times Searches]}, Leaves()) = [Measures].[Rank] * [Measures].[Searches];
From my point of view, this solution is not as clear as to directly calculate the value as described above. I would also think it could be slower, at least if you use aggregations for some partitions in your cube.