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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
nickchobotar
Skilled Sharer
Skilled Sharer

Dynamically return percentiles values

Hello,

 

I would appreciate some help with building a virtual table that would dynamically calculate percentiles based on the Cartesian product of two physical (unrelated) tables. In my model my physical table are Sales and Percentiles. The percentile K values are coming from a Percentiles table which is one column table and contains K values.  The result of this table will be further used as a variable to incorporate additional business logic.   

Here is the end result of the virtual table. As you can see the Percentile Amount is calculated for each ClientID based on the Percentile K field which dynamically changes 3 times: 0.2, 0.5 and 0.8. 

Here is a link to the PBIX file: https://1drv.ms/u/s!AsgNvkRwqGC7hHq3YUsaKMQbByRo
Excel file: https://1drv.ms/x/s!AsgNvkRwqGC7hHs1qC0szXT9V_64

To sum it up. I am looking to build an in-memory table below.  ClientId and Amount is a CROSSJOIN of Sales and Percentiles tables and Percentile Amount is what I need a little help with.

 

 


Capture.JPG

 

 

Here in DAX Studio, you can see I am using an iterator PERCENTILEX  as I discovered it is not possible to dynamically feed K values into regular PERCENTILE function.  I believe this is the correct path to solve this.

 

Capture1.JPG

 

 

 

 

 

 

 
DEFINE
    VAR crossjoinTbl =
        CROSSJOIN ( 
        	Sales, 
        	VALUES ( Percentiles[Percentile K] )
    	)
EVALUATE
ADDCOLUMNS (
    crossjoinTbl,
    "Percentile Amount", 
  	 PERCENTILEX.INC ( 
    		crossjoinTbl, 
    		Sales[Amount], 
    		Percentiles[Percentile K]
 	)    		
)
   	

 

 

Any ideas will be highly appreciated.

Thanks

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey,

 

this DAX statement ...

 

Table = 
var tblCrossJoin = CROSSJOIN('Sales', 'Percentiles')
return
ADDCOLUMNS(
    tblCrossJoin
    ,"percentileAmount"
        ,var currentClientID = 'Sales'[ClientId]
        var currentPercentile = 'Percentiles'[Percentile K]
        var sumAmount = CALCULATE(SUM('Sales'[Amount]))
        return
        PERCENTILEX.INC(
        FILTER(
            tblCrossJoin
            ,'Sales'[ClientId] = currentClientID && 'Percentiles'[Percentile K] = currentPercentile       
        )
        ,'Sales'[Amount] * 1.0, currentPercentile
    )
)
		

... helps to create this table ...

 

image.png

 

It looks pretty much the same as your expected result.

 

Hopefully it is what you are looking for.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

Hey,

 

this DAX statement ...

 

Table = 
var tblCrossJoin = CROSSJOIN('Sales', 'Percentiles')
return
ADDCOLUMNS(
    tblCrossJoin
    ,"percentileAmount"
        ,var currentClientID = 'Sales'[ClientId]
        var currentPercentile = 'Percentiles'[Percentile K]
        var sumAmount = CALCULATE(SUM('Sales'[Amount]))
        return
        PERCENTILEX.INC(
        FILTER(
            tblCrossJoin
            ,'Sales'[ClientId] = currentClientID && 'Percentiles'[Percentile K] = currentPercentile       
        )
        ,'Sales'[Amount] * 1.0, currentPercentile
    )
)
		

... helps to create this table ...

 

image.png

 

It looks pretty much the same as your expected result.

 

Hopefully it is what you are looking for.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Tom,

Thank you. Yes, that's a very good approach and plus I am getting back a very good query plan too. I was also exploring a noniterative approach with PERCENTILE and it works too, so there is a path in that direction too only needs tweaking the nested conditional statements.


DEFINE
    VAR tbl =
        CROSSJOIN (
            Sales, 
            VALUES ( Percentiles[K] )
        )

EVALUATE

ADDCOLUMNS (
    tbl,
    "Percentile Amount",CALCULATE(    
	        CALCULATE (
	            PERCENTILE.INC ( 
	                Sales[Amount], 
	                    if( VALUES(Percentiles[K]) = 0.2 , 0.2, 
	                    if( VALUES(Percentiles[K]) = 0.5 , 0.5, 
	                    if( VALUES(Percentiles[K]) = 0.8 , 0.8 
                )))),  
	            ALL ( Sales[Amount] ),
	            CROSSJOIN ( 
	                VALUES ( Sales[ClientId] ), 
	                VALUES ( Percentiles[K] ) 
	            )
	        )
        )
   )



 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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