Top N and Others in Power BI - powerbi

I have a simple database with a doctors table and a detail table with their patients.
How can I efficiently group doctors with less than 300 patients into an other doctors category to obtain a pie chart like the picture below?
What I end up doing is to create a DAX table
DoctorsWithPatientsCount = SUMMARIZE(Patient, Patient[Doctor.FullName], "PatientCount", COUNT(Patient[Id]))
with the following calculated column
TopDoctor = SWITCH(TRUE(), 'DoctorsWithPatientsCount '[Count] > 300, DoctorsWithPatientsCount[Doctor.FullName], "Other doctors combined")
and have that ploted.
As you can see it does work, however, is there another more direct and efficient way to achieve this? Thanks!

TopN and Others has been a long requested feature in PBI which you can vote on here: https://ideas.powerbi.com/ideas/idea/?ideaid=08369fa0-5152-4d55-b858-e36c06737d10
There are currently a few ways of achieving this.
Your way which has the disadvantage of not being dynamic and responding to slicers because it is a calculated table. Usually, you would use a RANKX or TOPN rather than your DAX.
If you want the TopN to be dynamic, you need a disconnected table. There are simple implementations described here: https://goodly.co.in/top-n-and-others-power-bi/ or more complicated but versatile approaches described here: https://www.sqlbi.com/articles/filtering-the-top-products-alongside-the-other-products-in-power-bi/
You can use a custom visual like Deneb to perform a window transform that calculates the TopN for you and displays it automatically.
As an addendum, your pie chart should really only display 5 or 6 categories max to be useful to end users.

In doctors table, you can define a new computed column "computed doctor"; then use this new computed column to make pie chart.

Related

How can I force only two selection out of 5 available on slicer visual?

Can someone please help me get a fix for this please.
I want to let people make only two selection at a time, no more no less from the given options. The intention is to get data on matrix dynamic column based on these two selections.
Thanks
Avi
You did not make clear what the purpose of this exercise is and how the result should look like ("get data on matrix dynamic column"), but you could
Create 2 single selection slicers based on copies of your category column
Make sure they are not related to the rest of your data model
Get the selected rows via the SELECTEDVALUE() function
You may have to consider the case where both selections are the same.
This would be a feature of a slicer visual, but I don't know of any built-in or custom slicer visual that does this.
As a workaround you can show a warning and instruction to select two items, eg a big Card visual with red text and a transparent background in front of your other visuals displaying a measure like:
AreTwoCategoriesSelected =
IF (
DISTINCTCOUNT ( DimProductCategory[ProductCategoryName] ) <> 2,
"Select 2 Categories for this report",
""
)
Which might look like
and
You can't easily use the trick where you have the measure conditionally return BLANK because most evaluations of the measure will have a filter context with just one of the selected values.

Using IN operator in DAX with Power Bi Desktop chart?

I'm new to DAX so please bear with me.
Can I use the IN operator in DAX to create a query directly in Power Bi desktop?
For example, the screenshot below displays over 120 curencies in DimCurrency.CurrencyName. Is it possible to create a DAX query where I can include all my filters using IN operator?
For example, maybe something like this?
CALCULATE (
[Sales Amount],
Products[Color] IN { "Red", "Black" }
)
The reason I'm interested in using DAX is because my current filter has over 200 different items, so I don't want to scroll through the 200-item list and select 5 different items.
This is a data modeling problem. In Dimensional Modeling terms your Dimension needs some additional attribute hierarchies to drive the filtering. This is just like why a Calendar table doen't just have a Day column, it needs Month, Year, Quarter, so you don't have to select all the individual Days.
Basically DimCurrency needs an additional column, so that you can include those 50 currencies by selecting fewer values of that new column.
One way to modify your data model is with DAX calculated columns, with an expression of the form
IsFacoriteCurrency = 'DimCurrency'[CurrencyName] in {"Algerian Dinar","Argentine Peso"}
you can also modify the data model in the data source or in Power Query.

Power BI / DAX return values from table from rows filtered out by slicer

I have a table visualisation that shows the populations of countries and a toggle switch that flips between 'sold' and 'unsold'. (This works with a measure that checks is a country is present in a sales table and assigns a 1 or 0 which is then used as a filter on the table visualisation).
Various slicers in the dashboard are used to filter the data model and retain the details of sales. When 'unsold' is selected therefore, the relevant countries are already filtered out of the countries data table and it is not possible to display them with their populations.
At the moment the workaround is to use a duplicate countries table that only has a one way filter, so that the rows remain regardless of filtering. This means that other slicers which interact with the rest of the data model don't filter the table visualisation as desired.
I am sure this must be possible using some combination of CALCULATE(), FILTER() and ALL() but I haven't managed to achieve this.
N.B. I can force the unsold countries to appear in a table visualisation using a constant measure (with formula: measure_name = 0) in a column .
Apologies if this is not very well explained, any help much appreciated.
Thanks for reading,
S
Image attached to (hopefully!) explain problem better.
Real scenario is more complicated hence not screenshotting from PBI.

How can I have dynamic axis which is responsive on slicer value, without using bridge table, use just DAX

Hi everyone, I have a chart which I need that the x value (Axis) changes by changing the slicer value (this slicer is the yellow one that has all the Dims (x values)) and it comes from DimList table. For example, at the moment the chart is totalfreight by custid, but I need if I check the empid from the yellow slicer the chart value changed to totalfreight by empid. This happens for all of the slicer values.
But I don't like bridge table or any method that has a bad effect on performance because the FactTable has a billion rows and I modeled it in SSAS, tabular model and has a live connection. Thanks in advance.
I assume you're referring to the approach that uses a bridge table described in this article. I agree that you may face some performance problems given the amount of data in your model.
First of all, you should try to see if there's some other Power BI frontend functionality that you can use directly in the report, without changing your data model. Perhaps you can use Power BI bookmarks, links or maybe a custom visual?
If not, there's another approach you can use in the data model, that does not rely on bridge tables. Disclaimer: I haven't tested this - there could be other performance issues involved.
Construct a new dimension table with all the members from your individual dimensions. Ie. create a union of all EmpIDs, CustIDs, etc. Make sure you indicate the type of ID in a separate column. The table should look like this:
DimensionId MemberId
categoryid 1
categoryid 2
categoryid 3
custid 1
custid 2
custid 3
...
Let's name this table 'All Dimensions'. The table should not have any relationships to other tables (this is similar to the Parameter Table pattern.
Change your measures to apply a virtual relationship whenever something is selected on the 'All Dimensions' table, to properly filter the fact table:
SUM('factSale'[Freight])
would become:
SWITCH(
SELECTEDVALUE('All Dimensions'[DimensionId]),
"categoryid", CALCULATE(SUM('factSale'[Freight]),
KEEPFILTERS(TREATAS(VALUES('All Dimensions'[MemberId]), 'factSale'[CategoryId])),
"custid", CALCULATE(SUM('factSale'[Freight]),
KEEPFILTERS(TREATAS(VALUES('All Dimensions'[MemberId]), 'factSale'[CustId])),
"empid", CALCULATE(SUM('factSale'[Freight]),
KEEPFILTERS(TREATAS(VALUES('All Dimensions'[MemberId]), 'factSale'[EmpId])),
// ... etc. for all dimensions ...
, // Fallback, when nothing is selected on 'All Dimensions'
IF(NOT ISFILTERED('All Dimensions'[MemberId]),
SUM('factSale'[Freight])
)
)
Put the [DimensionId] column into a slicer and use the [MemberId] column as the axis on your bar chart. Note that the chart will not show anything unless exactly one item has been filtered on the [DimensionId] slicer.
Explanation: The SWITCH statement determines if any selection has been made on the [DimensionId] column of the 'All Dimensions' table. In that case, a filter is applied to the fact table, depending on which dimension has been selected, using the TREATAS function. We're using KEEPFILTERS to make sure that any existing filters made directly on the individual dimensions are kept as-is.
In case no selection has been made on the [DimensionId] column, we want to fall back to the standard measure SUM('factSale'[Freight]) but since we don't want to repeat this measure for all items on the [MemberId] column, we use IF(NOT ISFILTERED( ... to make sure that we return only a blank value, if [MemberId] is currently used on the chart axis.

Based on slicer selection create dynamic calculated table in Power BI

I’m new to Power BI. Currently facing similar issue explained below in my product development.
I have created power bi modle with below dimensions and facts from adventureworksDW.
Then I created a calculated table, which gives result as sum of sales group by ProductSubCategory and ProductCategory. Below is the DAX for the calculated table.
Now I want to create a new calculated table, which gives me TOPn ProductSubCategory based on the Total sales amount.
Below is the DAX to do this.
and model relationships looks like below.
I want this TOPn rows to be displayed based on filter condition on product category. Something like below.
This works fine when I hardcode the product category value in the DAX itself. But if I want to change this product category values from the slicer selection, then I didn’t get any results.
What you are asking for is not possible as Power BI is currently designed. Slicers cannot affect calculated tables. Calculated columns and calculated tables are evaluated once when the data is first loaded and are static until the data is refreshed.
However, you can get the table visual you want in a much simpler manner by writing the appropriate measure and putting that in the table instead of defining an entirely separate table.
TotalSales = SUM(FactInternetSales[SalesAmount])
The Top N filtering is available in the visual level filters settings.
You can simply use the SELECTEDVALUE function as shown below.
var __SelectedValue = SELECTEDVALUE('ProductSales'[EnglishProductCatogaryName])
return
Filter(
'ProductSales',
'ProductSales'[EnglishProductCatogaryName] = __SelectedValue
)
)