Calculate MAX by subcategory Dax - powerbi

Hello I m newbie in dax ,
I want to calculate the max of value by subcategory and category.
it s like Max(Max(subcategory)category ) this example explain what I need .
Category Subcategory value
animal lion 5
food tomato 4
food potato 6
animal dog 5
plants flower 2
Category Max value
animal 5 ( if we have 2 subcategory with same value he choose randomly one )
food 6 ( potato )
plants 2
example

In Power BI, you do not have to calculate anything. You can simply use a visual to get teh result you want. Import the data, drag the table-visual on the report. Drag the Category and the value on the table.
As last step, set the value to the Max and you are done..
Enjoy!

Related

PowerBI Sum topN by category for applied filters

In PowerBI
Please can someone assist: I am trying to show the total sales in a card (Value)
Based on the following Top 10 in Category 1, Top 10 in category 2, Top 10 in Category 3, and Top 10 in Category 4. The top 10 will be based on volume of the products sold.
Further I have a country filter and the value shown in the above calculation needs to change based on the country selected.
Category
Product
Volume
Country
Car
Volvo
10
SA
Bike
Hill
15
UK
Tried multiple videos online, but nothing seems to be working for me

How to plot maximum/minimum value plot for selected item's category

Table 1:
Date
Item
Sales Amont
10/01/2021
Apple
300
10/02/2022
Orange
700
20/03/2022
Carrot
300
21/03/2022
Onion
800
Table 2:
Date
Item
Category
Sales Amount
10/01/2021
Apple
Fruits
300
20/02/2022
Carrot
Vegetable
800
10/02/2022
Orange
Fruits
700
21/03/2022
Onion
Vegetable
200
Relationship: Table1[Item] and Table2[Item] are connect using Many to many relationship.
Im trying to plot three lines of graph for table 1.
Line 1- Represents the maximum sale amount based on its category(category value retrieved from selected value of item displayed in slicer)
Line 2 – Represents the sale amount of selected item over the date.
Line 3 -- Represents the minimum sale amount based on its category(category value retrieved from selected value of item displayed in slicer)
My Problem is
Unable to plot the maximum/minimum sale amount line based on the slicer item category.
When you change the item in the slicer, the maximum/minimum line chart must be updated based on the selected item category, but in this case, the lines change based on the selected item value rather than the max/min value.
My Question is:
How to plot the maximum/minimum value chart only based selected item’s category.
Example :
If I choose the apple in the slicer, the maximum/minimum plot will show the maximum and minimum value of the fruits category.
Based on your comment I've given it a try.
First, I've modified the model to be closer to a typical PowerBI model. It makes everything much easier. Namely:
I've converted Table2 into a Category table.
I've created a Calendar table.
The new Category table looks like that:
Item
Category
Apple
Fruits
Carrot
Vegetable
Orange
Fruits
Onion
Vegetable
The calendar table is just a table expression:
Calendar = CALENDAR(DATE(2022, 01, 01), DATE(2022, 03, 31))
I end up with a model looking like this:
Then, regarding your calculations:
Line 1- Represents the maximum sale amount based on its category(category value retrieved from selected value of item displayed in slicer)
Cat_MaxSalesAmount =
MAXX(
Sales,
Sales[Sales Amont]
)
Line 2 – Represents the sale amount of selected item over the date.
Sales_AllCategories =
CALCULATE(
SUM(Sales[Sales Amont]),
ALL(Category[Category])
)
Line 3 -- Represents the minimum sale amount based on its category(category value retrieved from selected value of item displayed in slicer)
Cat_MinSalesAmount =
MINX(
Sales,
Sales[Sales Amont]
)
I've tried it in table visualisation and the different calculations are modified by the filters.

How to deal with multiple ids multiple categories table to reach THIS on Power BI

I have a problem that i was trying to solve 3 days ago and i'm not able to.
I have the following tables:
Companies
company_id
sales
1
2000
2
3000
3
4000
4
1000
Categories
company_id
category
1
medical
1
sports
2
industrial
3
consumption
4
medical
4
consumption
All i want to reach is a COLUMN CHART with a CATEGORY SLICER where i choose the category and i see the TOP 5 companies by category and sales. Yes, in this example the TOP is not needed but in my real case i have 400 companies so i want to:
Select and Show only the required category.
In that category, show only the 5 better companies by sales.
The problem here is Power BI takes all the companies for the TOP N filter so if i choose a category, and also try a top 5, if the companies are not in the TOP5 all companies list, it doesn`t show anything.
Thanks!
If you always want to show the same Top N values in your visual, you can use the filter pane to achieve that.
Below a walk through:
The to add the Top N filtering, I add the following:
It is in Dutch, so a little clarification:
I add a 'filter on this visual'
I selected Populairste N, which is Top N
And as a value I drag and dropped the maximum of sales.
Results:
Things to keep in mind:
You are using a many to many relationship, make sure that this is activated in the Power BI model.
Make sure the direction of filtering is from category to sales, otherwise the slicer will not work. It looks like this:

Power BI dynamic ranking with some blank values and using slicers

I have a table with 15 people that each month get 7-day scores. I want to use the RANKX formula in Power BI to rank the lowest (1) to the highest average score.
This works fine if I look at all, but start to act weirdly when I use a slicer and only look at one or two months for example. The ranking doesn't start with 1 anymore?
I use this formula:
Rank = RANKX(
ALLSELECTED('Score Table'[Person]);CALCULATE(AVERAGE('Score Table'[Score]));;ASC;Dense)
Look at the image attached, please.
Help much appreciated image showing the issue
Can you try this and see if it works?
Rank =
RANKX(
CALCULATETABLE(
VALUES( 'Score Table'[Person] ),
ALLSELECTED( 'Score Table'[Person] )
),
CALCULATE( AVERAGE( 'Score Table'[Score] ) ),
,
ASC,
Dense
)
Let's think about the original code step by step.
It iterates over "Person 1" to "Person 20" and calculates the average score of that person.
Evaluate the average score of the person of current filter context (say "Person 1").
Find the ranking position of "Person 1" in 20 persons.
In the step (1), it includes all Persons from 1 to 20 because there is no Person filter in the visual. Here, it looks there is no scores of Person 15 and 18 in the selected period, so it evaluates to BLANK.
Now, the document of RANKX says,
If expression or value evaluates to BLANK it is treated as a 0 (zero) for all expressions that result in a number, or as an empty text for all text expressions.
The average scores of Person 8 and 15 are BLANK, so RANKX treats it as 0. Now going back to Person 1, her average score was 62.43, and there were two people with average score of 0. Therefore, the rank of Person 1 will be 2.
By wrapping 'Score Table'[Person] with VALUES inside CALCULATETABLE, you can omit persons who has no scores in the selected period.

Using Parameters in PowerBI in DAX for creating new measures

I am fairly new to PowerBI and have a situation at hand.
I have a column on which I have to create two parameters and compare the values against each other.
Example:
Animal: Food: Capacity:
Dog Milk 10
Dog Bone 26
Cat Milk 20
Cat Bone 8
Bird Grain 6
Bird Water 9
When I select Parameter1 as Dog and Parameter2 as Cat, I should get something like this:
Food: CalcMeasureDog CalcMeasureCat<br>
Milk 10 20
Bone 26 8
Is this something we can achieve in PowerBI?
Main challenge is to use the Parameters in the calculation to do the below:
CalcMeasureDog=IF(Animal=Parameter1,Capacity,0)
CalcMeasureCat=IF(Animal=Parameter2,Capacity,0)
Also, I don't really want to show the animal column on the report on which i have the parameters created.
Appreciate any help i can get.
First, create a measure for all animals:
Total Capacity = SUM(Table[Capacity])
where Table is the name of your table.
Then, create measures for specific animals:
Dog Capacity = CALCULATE( [Total Capacity], Table[Animal] = "Dog")
Cat Capacity = CALCULATE( [Total Capacity], Table[Animal] = "Cat")
If you drop these measures into a matrix with food on the rows, you should get your desired result.
The simplest way to do this is to just put them all in a matrix visual with Food on the rows, Animal on the columns, and SUM(Table[Capacity]) as the values and use a single slicer for both parameters:
I'm not sure how well this works for your particular report as I don't quite understand what you meant by
I don't really want to show the animal column on the report on which i have the parameters created.