Forum Discussion
Ranking
I think i got the Rank 1, and Rank 2 as seperate functions...
Rank 1 Count = IF(ISBLANK(COUNTX(FILTER(ALLSELECTED(Table1[ID]),[Rank]=1),[Total_Amount])),0,COUNTX(FILTER(ALLSELECTED(Table1[ID]),[Rank]=1),[Total_Amount]))
Is there a dynaminc function i can use, where i don't have to have 5 formulas for the top 5? I can just use one and filter on the top x that i want, or would i have to create another table with the numbers 1, 2, 3, 4... and go about it that way?
I took a very different approach to solving this. I used the query editor to add the Rank by creating a custom function that would sort the value field in ascending order and then add an index (starting with 1). Then, I grouped the original data table by ID, with the aggregation set to "All Rows". Then, invoke this custom function as a new column and it will give you a rank column after expanding the table.
Here is the function:
let
Source = (column) as table => let
Source = column,
#"Sorted Rows" = Table.Sort(Source,{{"Value", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Rank", 1, 1)
in
#"Added Index"
in
SourceThen, using DAX, I created the following 3 measures:
Rank1 = IF(CALCULATE(COUNT(Table1[Index]),Table1[Index]="1")=0,0,CALCULATE(COUNT(Table1[Index]),Table1[Index]="1"))
Rank2 = IF(CALCULATE(COUNT(Table1[Index]),Table1[Index]="2")=0,0,CALCULATE(COUNT(Table1[Index]),Table1[Index]="2"))
Total = [Rank1]+[Rank2]
After putting these into a table with the Company field set to "Show Items With no Data" and "Do Not Summarize", you get the following:
- dkay84_PowerBI9 years agoMicrosoft Employee
I just noticed the very last ask you had for the rank to be dynamic based on the filter context, so this solution will not work for that.
After looking at the DAX rank measure provided by others in this thread, it does not appear to dynamically update either.
- amotto119 years agoHelper II
Wow, I did not even know the query editor existed, but as I said I am very new to Power BI. It is great to see so many different ways to solve a problem, it really helps someone trying to learn, Thanks!
Based on these two methods, which do you think is more efficient, which method should one use going forward?
- dkay84_PowerBI9 years agoMicrosoft Employee
In general, any time you can bring in a column from the source data or from the query editor, it will have better compression. However, with measures, it is better to use DAX. In the case of the RankX measure, it is probably better to use DAX, but for my sanity I used the query editor ;)
- Sean9 years agoCommunity Champion
dkay84_PowerBI wrote:After looking at the DAX rank measure provided by others in this thread, it does not appear to dynamically update either.
Compression :smileylol: What does compression have to do with my Measure not updating dynamically?
- dkay84_PowerBI9 years agoMicrosoft EmployeeCompression has nothing to do with your measure updating dynamically. That was a response to the question about using query editor vs DAX.
As for dynamically updating, my interpretation was that the rank would change depending on the companies included in the filter/slicer. So for example, if only 2 companies are selected, the ranks would always be 1 & 2 since there are only 2 companies. - amotto119 years agoHelper II
Thank you all for your help. I have one last question.
My dataset is pretty large, over 5 million rows. When i use Import, i am able to use these DAX commands and i have it working, but it is extremely slow. just filtering by a few companies may take a few minutes to reload. Is DirectQuery any quicker? I have read that you cannot use DAX in DirectQuery mode, since that is the case, how would i go about doing what i have done in import in directquery mode?
- dkay84_PowerBI9 years agoMicrosoft EmployeeYou can use DAX with Direct Query but there are restrictions on functions you can use.
In general, any slicing/filtering on large data sets will take time and resources. If your data source supports Direct Query, it will generally be faster as the filtering will get pushed to the underlying DB engine rather than run inside the Power BI data model. - amotto119 years agoHelper II
I tried to use my existing RankX and CountX functions and they would not work in DirectQuery, are these supported and i was just doing something wrong, or are they not supported?
- Sean9 years agoCommunity Champion
- dkay84_PowerBI9 years agoMicrosoft Employee
It looks like RankX is not supported:
https://msdn.microsoft.com/en-us/library/mt723603.aspx
However, if you go to the File > Options > Direct Query menu, you can check the box for unrestricted measures. This allows all DAX but won't necessarily work if the backend DB doesn't support it since with DQ the queries get sent to the DB for execution.
- amotto119 years agoHelper II
When i do my countx formula for the total it seems to work, but when i do my visual i get
Couldn't load the data for this visual
The resultset of a query to external data source has exceeded the maximum allowed size of '1000000' row.
Is this just saying that my dataset is too large for directquery?
- dkay84_PowerBI9 years agoMicrosoft Employee
Yes. You need to aggregate the data so that the rows returned are below that limit.
- amotto119 years agoHelper II
This may be a stupid question, but no question is stupid right...:smileyhappy:
How do I aggregate the data?
would i break the table into x number of tables, where x is the number of companies in the database? so each company has its own table and therefore its own information, then link them back up inside Power BI?
- dkay84_PowerBI9 years agoMicrosoft Employee
Direct Query is designed more to return aggregations that the source DB performs, rather than return the raw data. While filtering always consumes a lot of resources, with an import data model (rather than DQ) it is possible that your structure/model is partly to blame for the performance problems. You should be using a star schema with lookup tables and fact tables. I would recommend going back to an import data model and seeing if you can improve the model structure to get a better response time when slicing the data.