DAX RANKX Within Group - powerbi

I have a table called CarSales with the following sample data:
I already have a measure that ranks all Brands by sales:
Rank = RANKX(ALL('CarSales'[Brand]),[NetSales])
However, I need another measure/column **"Rank(Country)" ** which ranks the Brands within the Country group (without having to display the Country group in the table)
Thanks

Try the following measure:
=
VAR ThisCountry =
MIN( CarSales[Country] )
RETURN
RANKX(
FILTER( ALLSELECTED( CarSales ), 'CarSales'[Country] = ThisCountry ),
[NetSales]
)

Related

power bi dax, sum up all latest monthly entries

Hi I have a data table in powerbi structured
id date data
1 2022-10-30 123
1 2022-11-01 130
1 2022-11-30 456
the data spans multiple user ids and multiple years and it the values are cumulative (like minutes on a phone plan for instance). This is not the real data
I want to add up the end of month data. In the ideal case, my table would be complete and 2022-10-31 would exist for instance, then I could do
Measure =
CALCULATE(
SUM( 'Table'[data] ),
'Table'[dates] = EOMONTH( 'Table'[dates],0 )
)
This returns 456 but I want 579 (123+456). So i cannot use EOMONTH
I think the answer is some combination of the dax above and
FILTER( Table, Table[date] = MAX( Table[date] ) )
Though if I paste that in solo, it grabs the actual latest date only, not all monthly latest dates
Also I will use slicers on user ID's in case that changes the DAX
Please use this measure to get what you need:
Measure_ =
VAR TblSummary =
ADDCOLUMNS ( YoursTable, "EOM", CALCULATE ( ENDOFMONTH ( YoursTable[date] ) ) )
RETURN
SUMX ( TblSummary, IF ( [EOM] = [date], [data] ) )
If we test our above measure on a table visual:

Problem on using ALLEXCEPT to get percentage

I have a problem of getting the percentage on customer sales in matrix table thta is sliced by store_id and employee_id using ALLEXCEPT function. The percentage of customer sales need to obrain from dividing customer sales measure in current filter by the same measure of the respective store_id, instead of total customer sales of all the store_id.
Here is my expectation of the output and the DAX when using ALL function on the staff_id.
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
[Customer Sales],
ALL(
'Employee Lookup'[staff_id]
)
)
VAR Ratio =
DIVIDE(
[Customer Sales],
ALLExceptSales,
BLANK()
)
RETURN Ratio
I have tried using ALLEXCEPT on store_id from sales by store table, ALLEXCEPT store_id from store lookup table and ALLEXCEPT on both the tables, still giving me different output.
Here is the pbix file for testing.
https://drive.google.com/file/d/1fRrfsikHl9aK06GzAozJ9Wc16Ue0YJm2/view?usp=sharing
Anyone can hint me on?
Replace DAX with following
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
sum(Table[Customer Sales]),
ALLEXCEPT(Table, 'Table'[store_id])
)
VAR Ratio =
DIVIDE(
sum(Table[Customer Sales]),
ALLExceptSales,
BLANK()
)
RETURN Ratio
Have you tried ALLSELECTED instead of ALLEXCEPT?
% of Customer Sales =
DIVIDE (
[Customer Sales],
CALCULATE ( [Customer Sales], ALLSELECTED ( 'Employee Lookup'[staff_id] ) )
)

DAX options to filter 1 side on 1:M relationship

I have salesman dimension and sales fact with 1:M relationship on salesman_id.
I am trying to create a measure for count of salesman that have made sales in location is 6.
CALCULATE (
DISTINCTCOUNT ( Salesman[SalesmanKey] ),
Sales,
Sales[LocationId] = 6
)
I think this is not working because the filter doesn't flow from sales into salesman table.
I could change the filter direction as both but I'm looking at other option like using DAX CALCULATE with CROSSFILTER. Is there any other option like using CALCULATETABLE?
You can use the following Dax formula to achieve your goal:
Measure =
CALCULATE(
DISTINCTCOUNT( Salesman[SalesmanKey] ),
CROSSFILTER( Sales[salesman_id], Salesman[SalesmanKey], Both ),
Sales[LocationId] = 6
)
However I recomend you using the salesman id from the fact table:
Measure =
CALCULATE(
DISTINCTCOUNT( Sales[salesman_id] ),
Sales[LocationId] = 6
)
Edit: i added the option using the calculatetable:
Measure =
CALCULATE(
DISTINCTCOUNT( Salesman[SalesmnaKey] ),
CALCULATETABLE(
Sales,
Sales[LotacionId] = 6
)
)

How to calculate average of `whole number` data type column by Category

I am currently facing an issue where I need to calculate Average of no. of approvers for each contract type. But the issue over here is that the Approver data i.e. (contract_approval_order_1) field is nothing but USER IDs ( whole number data type). Thus Power BI is not calculating the averages properly as shown in the image below.
I tried the following formula in the column:
Average Approver Order 1 Assigned =
DIVIDE (
COUNT ( view_contracts[contract_approval_order_1] ),
DISTINCTCOUNT ( view_contracts[contract_id] ),
0
)
I am expecting 0.5 as the average of the sample data given below.
Use the following dax formula to create a measure:
Average Approver Order 1 Assigned =
VAR __numerator =
CALCULATE(
COUNT ( view_contracts[contract_approval_order_1] ),
ALLEXCEPT( view_contracts, view_contracts[contract_approval_order_1] )
)
VAR __denominator =
CALCULATE(
DISTINCTCOUNT ( view_contracts[contract_id] ),
ALLEXCEPT( view_contracts, view_contracts[contract_type] )
)
Return
DIVIDE( __numerator, __denominator, 0 )
The ALLEXCEPT statement in the denominator variable may be slightly different if there are other filters ar slicers affecting your table.
This is the result

How to summarize column with condition on power bi?

I'm trying to create table summary table with following conditions
From the Original table to summary table we have to create using the following conditions
1) select distinct ids
2) select screen name base on highest count group by id and today date
3) If two screens are same value on today date with the same id then pick the first screen
This yields the desired result as a calculated table.
SummaryTable =
ADDCOLUMNS(
ADDCOLUMNS(
FILTER(
SUMMARIZE(
OriginalTable,
OriginalTable[ID],
OriginalTable[StartDate]
),
OriginalTable[StartDate] = TODAY()
),
"Count", CALCULATE( MAX( OriginalTable[Count] ) )
),
"Screen",
VAR CurrentCount = [Count]
RETURN CALCULATE( MIN(OriginalTable[Screen]), OriginalTable[Count] = CurrentCount )
)
Output:
You could create a Rank calculation using the following formula:
Rank = IF(Original[Start Date]=TODAY(),RANKX(CALCULATETABLE(Original,ALLEXCEPT(Original,Original[ID])),Original[Count]),0)
Output:
You should replace "Original" with your table name in the calculation. Once the Rank is created, you can just filter for Rank=1 and you should have the desired result. Hope this helps.