Get the top 5 brands of each food subcategory - grouping

I would like to take the top 5 selling brands for each subcategory of the category food compared with the yearly income in mdx query using the foodmart database.
To be more specific, we have a dimension called product that contains the product category and the brand name of a product. A product category may contains many sub categories. For example:
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Small Eggs]}
In the above example we can see that the subcategory eggs contains many subcategories also that contains the brands.
The final purpose is to show that for the subcategory eggs the top 5 brands for the people that have as yearly income < 10.000 are {the list}.
And we want that for each sub-category of category foods using the measure unit sales.
The output results should be like:
salary |Eggs |Meat
Brand1|Brand2|Brand3|Brand4|Brand5|Brand1|Brand2|Brand3|Brand4|Brand5
&lt 10.000k |Name1 |Name2 |Name3 |Name4 |Name5 |Name1 |Name2 |Name3 |Name4 |Name5
&gt 10.000k |Name1 |Name2 |Name3 |Name4 |Name5 |Name1 |Name2 |Name3 |Name4 |Name5
Any help would be much appreciated.

You need to filter data on the basis of ranks. Take a look at the example below. It is based on adventureworks. In the example for each salary group, I am returning the top three subcategories, for each product category based on their values.
WITH
MEMBER [Measures].[Internet Sales Amount Rank] AS
RANK( ([Product].[Category].currentmember,[Product].[Subcategory].CurrentMember),
ORDER( ([Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].Members) , [Measures].[Internet Sales Amount], BDESC)
)
select
non empty
([Product].[Category].[Category],filter([Product].[Subcategory].[Subcategory],[Measures].[Internet Sales Amount Rank]<4))
on columns,
non empty
([Customer].[Yearly Income].[Yearly Income],[Measures].[Internet Sales Amount])
on rows
from [Adventure Works]
Result

This is your script. There are a lot of braces which is signalling a tuple to Mondrian:
WITH
MEMBER [Measures].[Top5] AS
RANK (
(
[Product].[Product Category].currentmember
,[Product].[Brand Name].CurrentMember
)
, ORDER(
(
[Product].[Product Category].currentmember
,[Product].[Brand Name].[Brand Name].Members
)
, [Measures].[Store Sales]
, BDESC
)
)
SELECT
non empty
(
[Product].[Product Category].[Product Category]
,filter(
[Product].[Brand Name].[Brand Name]
, [Measures].[Top5]<6
)
) on columns,
non empty (
[Yearly_Income].[Yearly Income],
[Measures].[Store Sales]
) on rows
from [projetDW];
Maybe try using the CROSSJOIN function to produce your sets of tuples:
WITH
MEMBER [Measures].[Top5] AS
RANK (
CROSSJOIN(
{ [Product].[Product Category].currentmember }
,{ [Product].[Brand Name].CurrentMember }
)
, ORDER(
CROSSJOIN(
{ [Product].[Product Category].currentmember }
,[Product].[Brand Name].[Brand Name].Members
)
, [Measures].[Store Sales]
, BDESC
)
)
SELECT
non empty
CROSSJOIN(
[Product].[Product Category].[Product Category].MEMBERS
,FILTER(
[Product].[Brand Name].[Brand Name]
, [Measures].[Top5]<6
)
) on columns,
non empty
(
[Yearly_Income].[Yearly Income],
[Measures].[Store Sales]
) on rows
from [projetDW];

Related

Power BI - DAX How to filter on an aggregate like COUNT

I'm new to DAX and Power BI. I'm having trouble filtering something that is probably not very complicated but I've finally given up. Any assistance is appreciated.
My starting data looks like this. I want to distinctcount how many drugs each person got (3 for Amy and 2 for Willy), then filter by 3.
Starting Data:
Key Name DOB OTC
AmyZ32874 Amy Z 1/1/1990 Motrin
AmyZ32874 Amy Z 1/1/1990 Tylenol
AmyZ32874 Amy Z 1/1/1990 Bayer
WillyV32906 Willy V 2/2/1990 Motrin
WillyV32906 Willy V 2/2/1990 Tylenol
WillyV32906 Willy V 2/2/1990 Tylenol
Desired Result: Ideally, this would be the desired result because the DisctinctCount of OTC is 3 for Amy Z.
Key Name DOB OTC
AmyZ32874 Amy Z 1/1/1990 Motrin
AmyZ32874 Amy Z 1/1/1990 Tylenol
AmyZ32874 Amy Z 1/1/1990 Bayer
Things I tried:
Creating a distinctcount column on OTC, but this gave me total rows
I tried separating the drugs into separate tables then doing a natural innerjoin, but it threw an error because of the many to many relationship.
I tried doing grouping, but that didn't give me a distinct count.
Thank you!!
If you want a result set as a table in your post, then create a new table and paste this code:
TestTbl =
VAR SummaryTbl =
ADDCOLUMNS(
YourTbl,
"UniqueCount",CALCULATE(DISTINCTCOUNT(YourTbl[OTC]),ALLEXCEPT(YourTbl,YourTbl[Name]))
)
RETURN
FILTER(
SummaryTbl,[UniqueCount]=3)
Then Result:
Create a measure:
MyFilter :=
CALCULATE( DISTINCTCOUNT( Table1[OTC] ), ALLEXCEPT( Table1, Table1[Name] ) )
then drag this into the Filters pane and set it equal to your desired value, e.g. 3.

count unique cities having total sales more than 50% in Power BI, DAX query

Here is my data sample, of Table "Sales"
City Items Target Sales
ABC Pen 100 20
ABC Pencil 200 40
ABC Glue 100 68
CDE Copy 50 37
CDE Books 70 20
CDE Rubber 200 156
I want a DAX query for Power BI, to count unique cities having total sales more than 50% for a large set of records with multiple Cities / States / Items
You need to iterate over the distinct values of the City field, evaluate the Sales % Target for each City, then count the number of values exceeding 50%.
Cities >50% =
COUNTX (
VALUES ( Sales[City] ),
IF (
CALCULATE (
DIVIDE (
SUM ( Sales[Sales] ),
SUM ( Sales[Target] )
)
) > 0.5,
1
)
)

Calculating running count and percentages from long-type data

Hello lovely people of SO,
I have a dataset that looks like so:
ID_SALE
PRODUCT
STORE
SE_056
AAA
NORTH
XT-558
AAA
NORTH
8547Y
AAA
NORTH
TY856
BBB
NORTH
D-895
BBB
SOUTH
ER5H
CCC
SOUTH
5F6F-GD
CCC
SOUTH
65-FFD
TTT
SOUTH
56-YU
UUU
SOUTH
I want to be able to plot a table that will show the count of each PRODUCT and the contribution of the global percentage of each PRODUCT as well as the cumulative percentage like so:
PRODUCT
Subtotal
Percentage
running %
AAA
3
0,33333333
0,33333333
BBB
2
0,22222222
0,55555556
CCC
2
0,22222222
0,77777778
TTT
1
0,11111111
0,88888889
UUU
1
0,11111111
1
I also want to be able to have a filter in the PBI sheet that will filter by STORE so if I choose "NORTH" my table will show the following:
PRODUCT
Subtotal
Percentage
running %
AAA
3
0,75
0,75
BBB
1
0,25
1
First THANKS A LOT guys pbi is truly coming for me and my mental health and although I have used the quick-measure feature to get the cumulative total I get get it to sort in order my data and so I figured that DAX is the only way.
if you guys can help me out I will be so thankful I will be very attentive to your responses.
Assuming your table is named "Table".
Subtotal = COUNTROWS('Table')
Percentage = [Subtotal]/CALCULATE(COUNTROWS('Table'),REMOVEFILTERS())
running % =
VAR cursor = MAX('Table'[PRODUCT])
RETURN
CALCULATE( [Percentage], REMOVEFILTERS(),'Table'[PRODUCT]<= cursor)

Power BI - Cumulative / Running Average

I need to have the "Running average" for the table rows within Power BI. I was able to generate the "Cumulative" Column, but I am not able to make the math work after all.
In the third column, I simply need something like "divide (Cumulative Row) by (running number of rows)"
E.g.:
(42 + 44 + 39) / 3
or:
123 / 3
My current code:
Average total =
DIVIDE(
Calculate
([Summe_FeaturesPI12],
Filter(Allselected('ADOPI12_1_Feature Aufteilung'),'ADOPI12_1_Feature Aufteilung'[Current Sprint]<=MAX('ADOPI12_1_Feature Aufteilung'[Current Sprint])),
Filter(Allselected('ADOPI12_1_Feature Aufteilung'),('ADOPI12_1_Feature Aufteilung'[State] in {"Done/Cancelled"})
))
, COUNTROWS('ADOPI12_1_Feature Aufteilung')
)
Power BI Table:
I got some help from a colleague. The solution of my problem lies in the second half. For anyone who should need it, please take a look on the solution:
Running Average =
DIVIDE(
Calculate
([Summe_FeaturesPI12],
Filter(Allselected('ADOPI12_1_Feature Aufteilung'),'ADOPI12_1_Feature Aufteilung'[Current Sprint]<=MAX ('ADOPI12_1_Feature Aufteilung'[Current Sprint])),
Filter(Allselected('ADOPI12_1_Feature Aufteilung'),('ADOPI12_1_Feature Aufteilung'[State] in {"Done/Cancelled"})
))
,
CALCULATE
(DISTINCTCOUNT('ADOPI12_1_Feature Aufteilung'[Current Sprint]),
Filter(Allselected('ADOPI12_1_Feature Aufteilung'),'ADOPI12_1_Feature Aufteilung'[Current Sprint]<=MAX ('ADOPI12_1_Feature Aufteilung'[Current Sprint]))
)
)

DISTINCTCOUNT applying greater than filter (Power BI)

Hi I am looking to create a measure from a column in a table (to be shown on a card visual) which counts all products where their individual respective counts total is >= 5.
For example:
Product Column
AAA
BBB
CCC
AAA
AAA
AAA
AAA
Based on the above the count would be 1 as only AAA has an individual count >=5
Any help would be greatly appreciated!
One solution would be to create a new table like so:
[Product Volume Greater Than 5] :=
FILTER (
SUMMARIZECOLUMNS (
'Table'[Product Column],
"Count", COUNT ( 'Table'[Product Column] )
),
[Count] >= 5
)
And just reference the new 'Product Column' in a visual.