Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
RobbLewz
Helper II
Helper II

RANKX() not working over two table on calculated column

My end purpose is to dynamically look at the SUM() values of a customer based on the slicer selection, rank them, for the top 4 show in a chart and combine everyone else greater than rank 4 into an others group for the chart.

 

First I need to rank them, So I created a measure:

 

RANKX ( ALL(DimTest[ClientName]), SUMX(RELATEDTABLE(FactTest), [LineTtl]) )

 

 This works fine, when I look at the measure, it displays the correct rank.

 

So I turn that into an IF() statement as a calculated column, if rank < 4, client name, else other; however it isn't correctly grouping the "Others" or displaying the rank 4 names.

 

To debug, I used the above measure as a calcualted column to see what it was evaluating, and I found it was somehow doing a LINE COUNT of all the entries,  in the FACT table rather actually SUM and Ranking??

 

Here is some sample data

 

DimTest
IdClientNameSectionId
1Client A14888
2Client B18419
3Client C16674
4Client D28165
4Client D19470
4Client D20018
4Client D12012
4Client D36822
4Client D30691
4Client D71145
4Client D15058
4Client D78461
2Client B19966
2Client B10394
2Client B22573
2Client B21171
2Client B30155
1Client A16291
1Client A14478
1Client A20726

 

FactTest
SectionIdLineTtl
148881500000
18419500000
1667410000000
28165444444
19470444444
20018444444
12012444444
36822444444
30691444444
71145444444
15058444444
78461444444
19966500000
10394500000
22573500000
21171500000
30155500000
162911500000
144781500000
207261500000

 

 

SQL code to generate Data

 

SET NOCOUNT ON; SET DATEFORMAT DMY;
GO

DROP TABLE IF EXISTS dbo.DimTest;
GO
CREATE TABLE dbo.DimTest
	( Id INT NOT NULL
	, ClientName VARCHAR(100) NOT NULL
	, SectionId  INT NOT NULL);
GO
DROP TABLE IF EXISTS dbo.FactTest;
GO
CREATE TABLE dbo.FactTest
	( SectionId INT NOT NULL
	, LineTtl INT NOT NULL)
GO

DECLARE @table TABLE 
		( Id INT NOT NULL
		, ClientName VARCHAR(100) NOT NULL
		, SectionId INT NOT NULL
		, LineTtl INT NOT NULL);

WITH	CTE
		(Id, ClientName, Cnt, Total, x)
AS		(
		SELECT 1, 'Client A', 4, 6000000, 1
			UNION ALL
		SELECT	2, 'Client B', 6, 3000000, 1
			UNION ALL
		SELECT	3, 'Client C', 1, 10000000, 1
			UNION ALL
		SELECT	4, 'Client D', 9, 4000000, 1
		)
,		_re
		(Id, ClientName, Cnt, Total, x, SectionId, lineTtl)
AS		(
		SELECT	Id, ClientName, Cnt, Total, x, LEFT(ABS(CHECKSUM(NEWID())), 5), c.Total / c.Cnt
		FROM	cte AS c
				UNION ALL
		SELECT	Id, ClientName, Cnt, Total, x + 1, LEFT(ABS(CHECKSUM(NEWID())), 5), _re.lineTtl
		FROM	_re
		where	x < cnt
		)

INSERT	@table
		( 
		Id, ClientName, SectionId, LineTtl
		)
SELECT	Id, ClientName, SectionId, lineTtl
FROM	_re;


INSERT	dbo.DimTest (Id, ClientName, SectionId)
SELECT	Id, ClientName, SectionId FROM @table;

INSERT	dbo.FactTest (SectionId, LineTtl)
SELECT	SectionId, lineTtl FROM @table


SELECT * FROM dbo.DimTest;
SELECT * FROM dbo.FactTest

 

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hello @RobbLewz ,

Following the blog post below, the taxi achieve the final result:

https://blog.gbrueckl.at/2019/05/power-bi-dynamic-topn-others-with-drill-down/

See the attach file that was created based on this solution and your only data question is that I made the rank value below 2 that you need to change to 4.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1 REPLY 1
MFelix
Super User
Super User

Hello @RobbLewz ,

Following the blog post below, the taxi achieve the final result:

https://blog.gbrueckl.at/2019/05/power-bi-dynamic-topn-others-with-drill-down/

See the attach file that was created based on this solution and your only data question is that I made the rank value below 2 that you need to change to 4.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.