RANKX without creating a summary table - powerbi

I'm a newbie in DAX and I'm trying to create a visual in Power BI that shows the top 10 customers by sales.
I tried to create a calculated column in the 'Food sales' table, but I couldn't figure out how to create the rank by client ID.
I ended up creating a summary table in the model:
Consolidation Ventes =
SUMMARIZE('Food Sales',
'Food Sales'[ID Client],
"Ventes",SUMX('Food Sales', 'Food Sales'[Quantité]*'Food Sales'[Prix de vente])
)
and adding the rank calculated column like that:
Rank = RANKX('Consolidation Ventes', 'Consolidation Ventes'[Ventes],,DESC,Dense)
I obtained what I was looking for:
But I'm pretty sure there's a better way to do it, without creating an extra table in the model.
I tried creating a measure instead, but it always returned "1" and I couldn't understand why...
Is there a better way to achieve the same result?
Is there a way to create a measure instead of a calculated column, to create dynamic ranks in order to slice the with more than one column (like for example, customer name and product type)?

I hope this helps:
#Orders := COUNTROWS(Sales)
SalesAmount := SUMX(Sales,Sales[Quantity]*Sales[Price])
TopN :=
COUNTROWS(INTERSECT(
CALCULATETABLE(
TOPN(5,VALUES(Customers[Name]),[SalesAmount],DESC),
ALL(Customers)
),
CALCULATETABLE(VALUES(Customers[Name]))
))
You add the TopN measure to "Filters on this visual" and define the filter condition to "TopN is 1".

Related

Can't do a cumulative sum with legends in a stacked chart (Power Bi)

I am new to Power Bi. My manager asked me to illustrate the growth of the company with a stacked chart.
All is fine when i don't add legends to it (the different stores). But when i do this happens :
Broken chart
I want the orange part (Omega company) to also appear in 2022.(even though they made no benefits for that year)
What measure do I need to make this happen ?
Any help would be much appreciated.
Here's a simplified caption of what the excel file for the power bi report looks like.
Thank you !!
Excel sample
You can use the Quick measure "Running Total" to achieve that, but you need a separate "Dates" table that you can build as a calculated table via
Dates = GROUPBY('Table','Table'[Date])
and that you need to relate 1-to-many to your original table.
Now in the Quick measures UI for "Running total" you set the Base value to "Sum of Benefits" and the Field to "Date - Year"
With that measure "Benefits running total in Year" and your Excel sample data from above your Stacked column chart looks like this:
What does your measure look like?
If you have a relationship between your fact table and date dimension table, you can try something like this
Running Total Benefits =
CALCULATE (
SUM ( 'Fact Table'[Benefits] ),
ALL ( 'Date Table' ),
'Date Table'[Date] <= EARLIER ( 'Date Table'[Date] )
)
Or use DATESBETWEEN function if they don't have relationship

PowerBI - How to show only 10 categories and wrap the rest on "Others"

I have a customer service ticket list and need to build a report with it, one of the charts must be a "Qty per motive" and the problem is that I have too many motive to show on a chart, so I want to show let's say 10 of them and wrap the rest on a "Others" categories.
I've found some posts showing hot to do this with values like Sales, but I couldn't figure out how to make it work with a count on my data.
the structure of the data is like
ClientID | Ticket ID | Date | Motive | Description
In the posts that I've found the solution involves a SUM() and then sorting by the SUM() column, but I don't have a value column, I need to count per motive
There are a couple of ways to doing that.
New group
use built-in PBI feature that you have to manually set up. Right-click on a column name in the Fields section and chose New group. A new window will pop up. There, you have to choose the categories you want to label as others. A new field (Column name (groups)) will appear in your table. This method requires you to decide arbitrarily which columns you want to be displayed.
Create calculated table
You could create a brand new Calculated table in your model that is going to set new labels based on the total quantity. In that case, the labels will dynamically change rather than being static. If new motive comes to the model and its quantity will be in TOP N then it will be visible on the chart. This solution creates a separate table that you have to connect via relationship with your main table later.
Select Calculated table on the ribbon and then write DAX:
TopNCategories =
VAR keepLabels = 3
VAR tbl =
ADDCOLUMNS(
VALUES( 'Product'[Brand] ),
"#TotalSales", CALCULATE( [Sales Amount] )
)
VAR addRank =
ADDCOLUMNS(
tbl,
"#Rank",
RANKX(
tbl,
[#TotalSales],
,
DESC
)
)
VAR result =
SELECTCOLUMNS(
addRank,
"BrandKey", 'Product'[Brand],
"NewLabel", IF( [#Rank] <= keepLabels, 'Product'[Brand], "Others" )
)
RETURN
result
Calculated column
If you don't want to create another instance in your model, you can use the above logic to create a set of Calculated column in your main table instead. Depends on your model size and cardinality, this solution may have an impact on the performance of the whole report.

Filter by last not blank date in Power BI

I have data from multiple countries on a monthly basis. Since the updates are not regular, I want to set up filter to visuals, so they would show the last month for which I have data from all the countries. I have data from each country loaded into a separate dataset, which then are merged into one big. Is there an easy way to place such filter? I managed to use "LASTDATE" function in each of country sets to find which date is last, but if I try to filter with that measure, I simply get nothing in a result. Thanks!
Well, this feels a little clunky to me but I believe it will work for you. There are two steps. The first is to create a summary table that reads through your data and counts the number of distinct countries that you have in each month. This will be a new table in your model, so go into the modeling tab, click 'New Table' and add this DAX. Obviously, correct for your table and column names.
SUMMARIZED_ROWS = SUMMARIZE(
'Table1'
,Table1[Month]
,"CountOfCountries"
,DISTINCTCOUNT(Table1[Country])
)
Now add a measure to the table (or anywhere) like this:
MonthWithMostCountries = CALCULATE(
LASTNONBLANK(SUMMARIZED_ROWS[Month], 1 )
, FILTER(SUMMARIZED_ROWS, SUMMARIZED_ROWS[CountOfCountries] = MAX(SUMMARIZED_ROWS[CountOfCountries]) ) )
This is going to give you the month where you have the most distinct countries in your data. You'll want to look at it in a card or similarly isolated visual as it is a measure and can be affected by filter context.
So, on the left is my mock data - 3 countries, 3 months each with a 1 month stagger. On the right you see the result of the Summarize table. Then the measure showing the final result.
Hope it helps.

Calculated Column to get average from values in another table in PowerBI

This might be very basic but I am new to PowerBI.
How do I get average of values for unique ID into another table.
For eg. My Table 1 has multiple ID values. I have created another table for unique ID which I am planning to used to join other table.
I want a calculated column in table 2 which will give me average value of respective ID from table 1.
How do I get the calculated column like shown below
In stead of creating a new table with the averages per ID and then joining on that, you could also do it directly with a calculated column using the following DAX expression:
Average by ID = CALCULATE(AVERAGE('Table 1'[Values]),ALLEXCEPT('Table 1','Table 1'[ID]))
Not exactly what you asked for, but maybe it's useful anyway.
How's it going?
The quickest way I can think of doing this would be to use
SUMMARIZECOLUMNS
You can accomplish this by creating another table based on your initial fact table like so:
Table 2 =
SUMMARIZECOLUMNS ( 'Table 1'[ID], "Avg", AVERAGE ( 'Table 1'[Values] ) )
Once this table has been created, you can create a relationship.
This will work in either SSAS or in PowerBI directly.
Hope this helps!! Have a good one!!

Power BI Rank within Matrix Visual

I've spent many weeks trying to get this to work including a posting at another site. No luck. Not sure what I'm missing.
The following code gets me the table of data that I need. However, I want just the "rank" value and I want it for each row within a matrix visual.
First some background, the PowerBI page has slicers on dates and "base" product. Any given product may be sold in multiple countries. All carry the same base product suffix with a country-specific prefix.
The matrix in question displays several aspects of all the product variants for the selected base product. I want to show where each variant ranks (unit sales) within its country.
VAR tblCountrySales =
SUMMARIZE (
FILTER(
tblSalesUnits,
RELATED(tblProducts[Country])="US"
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSalesUnits[Units] )
)
RETURN
ADDCOLUMNS (
tblCountrySales,
"ProductRank", RANKX ( tblCountrySales, [Total Sales] )
)
Is there a way to either pull just the rank for a single product from the above code, or, better yet, a DAX pattern that will get me the rank by unit sales of each product variant within its respective country?
I've use these statements to successfully get the product ID and country for each row within the visual.
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
Any help is greatly appreciated. I assume I'm missing something with row context within the visual as a simple COUNTROWS on the table expressions above always returns "1". Being new to PowerBI and DAX I'm still trying to grasp the nuances of filter and row contexts.
The products table contains data like...
ProdID.....BaseID....Name...Country
7190...........7190.....xxxx.....US
150207190......7190.....XXXX....Panama
241807190......7190.....xxxx.....Spain
The sales table contains data like...
ProdID......SalesMonth......Units.....USD
7190........July 2010.......4563....$23491.00
150207190...July 2010.......2543....$16235.00
241807190...July 2010.......1263....$8125.00
There is a dates table as well that links the SalesMonth to the usual selection of date display formats. The two tables above are linked via ProdID, though the visuals are sliced by the BaseID. I'd attach a picture, but don't have permissions to do so.
With some more tinkering I found this solution.
Country Rank =
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
VAR tblCountrySales =
SUMMARIZE (
FILTER(
ALLEXCEPT(tblProducts,tblProducts[BaseID]),
tblProducts[Country]=ProdCountry
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSales[Units] )
)
RETURN
IF(ProdSales>0,COUNTROWS(FILTER(tblCountrySales,[Total Sales]>ProdSales))+1,BLANK())