Power BI RankX function - powerbi-desktop

I have a simple table ,Sales with just product,Quantity , and totalsales.
I am trying to rank product based on total sales.
m1=rankx('sales','sales'[totalsales])
It is giving me all sort of errors.

To get the products based on order of total sales use the following steps:
Create a Total Sales measure :
TotalSalesMeasure = Sum('Product'[TotalSales])
Use the above measure in your DAX to calculate RANK
RankProduct = RANKX(ALLSELECTED('Product'[Product]),[TotalSalesMeasure],,DESC,Skip)
Hope this solves your issue.
Best Regards,
Shani Noorudeen

Related

Power Bi - Competitor Average Price Measure

Attached is the data I have the question for. I want to create a Measure in Power Bi that gives me the average price of a competitor's farm product. (If looking at Beth's product I want to see the average of Amy's product) If a filter is applied (Example: Product type = Fruit) then I want to see the average price of Amy's fruits). The goal of this is to find the % difference each product is from its filtered competition.
SpreadSheet Data
My unsuccessful attempt:

DAX Measure no longer calculates accurately if I sort an axis by another column?

I'm having some trouble creating a DAX Measure in PowerBI that will group sales by quarter, but still sort correctly. Our sales data is received cumulatively and once a quarter. I've created the following DAX Measure to calculate the quarterly sales:
Quarterly Sales = SWITCH(
SELECTEDVALUE(FactSales[Quarter]),
"Q1 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2018/19"),
"Q2 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q2 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2018/19"),
"Q3 2018/19",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q3 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q2 2018/19"),
"Q4 2019/20",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q4 2018/19")-CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q3 2018/19"),
"Q1 2019/20",CALCULATE(sum(FactSales[Quantity]),FactSales[Quarter]="Q1 2019/20")
)
The problem is, while this calculates accurate numbers, it chart sorts this alphabetically, so the two Q1s are beside each other. when I try to sort the Quarter column by a Sorting dummy column, the calculations then all become out of wack and are no longer accurate.
I feel like there must be an easy way to fix this, but I can't figure out how to do it. Thank you in advance for any assistance!

Weighted average price of a product per day in Pivot Table

I am having issues translating the following formula to a pivot table; either through a regular pivot table, or through DAX and powerpivot.
=SUMPRODUCT((C$2:C$11)*(D$2:D$11)*(A$2:A$11=A2)*(B$2:B$11=B2))/SUMIFS(D$2:D$11,A$2:A$11,A2,B$2:B$11,B2)
The background is, I have a number of products that appear on an e-commerce site, and I need to find out their price per day. However, these prices change daily, based on things like promo codes, visitor location etc. Therefore, I need their weighted price based on the number of visitors that saw a particular price.
Can anyone help with this translation, or alternatively, offer a better way to approach this problem?
PS- I need it in a pivot table due to the volume of data. At 250,000 rows, standard Excel cannot handle this formula.
The following is in Excel 2010 sans Powerpivot. However, the general approach should work:
Explanation:
I added a column that multiplies the Prices and Visits. The pivot table uses Dates, then Product SKU as the row labels. Then I added a calculated field that divides the Price*Visits by the Visits.

Excel PowerPivot DAX Calculated Field

I think I've got a relatively easy problem here on my hands, just having trouble getting it to work. Let me preface this by saying I'm new to DAX.
Consider the following PowerPivot Data Model :
It consists of a Sales Records table, which is joined to a Date lookup table, as well as a "Daily Expenses" table. The "Daily Expenses" table stores a single value which represents the averaged cost of running a specific trading location per day.
It's extremely easy to produce a PivotTable that provides me with the total Sales Amount per store per [insert date period]. What I'm after is a DAX formulation that will calculate the profit per store per day - ie. Total Sales minus DailyExpenses (operating cost):
In theory, this should be pretty easy, but I'm confused about how to utilise DAX row context.
Failed attempts:
Profit:=CALCULATE(SUM(SalesInformation[SaleAmount] - DailyStoreExpenses[DailyExpense]))
Profit:=CALCULATE(SUM(SalesInformation[SaleAmount] - DailyStoreExpenses[DailyExpense]), DailyStoreExpenses[StoreLocation])
Profit:=SUM(SalesInformation[SaleAmount] - RELATED(DailyStoreExpenses[DailyExpense])
etc
Any help appreciated.
Zam, unfortunately your failed attempts are not close :-)
The answer, and best practice, is use a 4th table called 'Stores' which contains a unique record per store - not only is this useful for bringing together data from your two fact tables but it can contain additional info about the stores which you can use for alternative aggregations e.g. Format, Location etc.
You should create a relationship between each of the Sales and Expenses tables and the Store table and then use measures like:
[Sales] = SUM(SalesInformation[SaleAmount])
[Expenses] = SUM(DailyStoreExpenses[DailyExpense])
[Profit] = [Sales] - [Expenses]
Provided you have the Date and Store tables correctly linked to the two 'Fact' tables (ie Sales and Expenses) then the whole thing should line up nicely.
Edit:
If you want to roll this up into weeks, years etc. and you have no kind of relationship between expenses and the calendar then you'll need to adjust your expenses measure accordingly:
[Expenses] = SUM(DailyStoreExpenses[DailyExpense]) * COUNTROWS(DateTable)
This will basically take the number of days in that particular filter context and multiply the expenses by it.

How do I get the proper average in a pivot based on pivot data?

I'm trying to get the average number of "on time shipment" based on items rolled up to "ship numbers" and then by "order number". I have one order number in this scenario that is shipped via multiple shipments. It seems to me that after rolling it up via PowerPivot and then creating a pivot table, it's calculating the average based on the total lines of the "order number" instead the pivot.
PowerPivot Data:
Pivot based on data above:
How can I get the average number based on the pivot table rather than the PowerPivot total data of the order number? I'm probably not making any sense, but hopefully the images below explain it better. As you can see, when you roll up the items by ship number then by order number, you'll see that the actual average is 0.6 but the pivot is showing 0.5.
Help!
Technically speaking, the average is correct - if you look at the source data, for some reason all rows are duplicated and if you do regular average calculation, it's actually 0.5.
What you are looking for is calculating average for distinct values, which can be done easily with AVERAGEX function.
I have copied your table and created those 2 Calculated Fields (in Excel 2010, it's Measures):
Average on Time:
=AVERAGE(Table1[On Time])
Average on Time (UNIQUE)
=AVERAGEX(VALUES(Table1[Ship Number]), [Average on Time])
Using AverageX with VALUES() function makes it easier to calculate any expression ONLY for unique values.
If you then put both measures on PivotTable, you should get this:
First column is same as yours (using "regular" AVERAGE function). The second one shows the average calculated over distinct (unique) values of Ship Numbers.
Hope this helps.
PS: This great article by Kasper de Jonge helped me quite a bit with similar scenarios.

Resources