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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
NandinAna
Frequent Visitor

calculating median using all and ranx

Hi,

I would like to get help on the following formula where i need to calculate median time to sell nd rank it, for each user when they login,but i am getting the results wrong.

. Median_FIRM_TimeToSell_ALL_RANK =

var firm_userprincipalname = [MF_USERPRINCIPALNAME]

RETURN

IF([.Sum_FIRM_Sales_ALL] >= [.val_Rank_Threshold],

CALCULATE(

    RANKX (

        ALL ( IMPORT_SQL_monthly[MemberFirm] ),

        CALCULATE ( MEDIAN( IMPORT_SQL_monthly[TimeToSell] ) ),

        CALCULATE ( MEDIAN( IMPORT_SQL_monthly[TimeToSell] ), LOOKUP_MF_USERS[PrincipalName] = firm_userprincipalname )

        ,ASC),

    OR(IMPORT_SQL_monthly[CurrentStatus]="Sold", IMPORT_SQL_monthly[CurrentStatus]="SaleComplete"),

    USERELATIONSHIP( IMPORT_SQL_monthly[DateSold], TBL_CALENDAR_date[Date]),

    USERELATIONSHIP( IMPORT_SQL_monthly[MemberFirm], LOOKUP_MF_USERS[firm])

)

, "-" ).

 

Please could someone help on this.

2 REPLIES 2
Greg_Deckler
Super User
Super User

@NandinAna Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Hello,

 

Thanks for your reply.I need some one to talkthrough where i am going wrong.So i have two app created one for our internal purpose and other for external customers where they can view thier sales and other stuff  for their company.So problem here is the calculation of the rank based on the time to sell is showing correct in our internal view but its wrong in the clients view except for this all the other calculations are exact same in both the view. So above is the calculation for determining rank of the company each year based on time to sell.

The below is the example where the time to sell column is incorrect for a firm Parissteele,

NandinAna_0-1678975884321.png

 

Formula used for above,

. Median_FIRM_TimeToSell_ALL_RANK =

var firm_userprincipalname = [MF_USERPRINCIPALNAME]

RETURN

IF([.Sum_FIRM_Sales_ALL] >= [.val_Rank_Threshold],

CALCULATE(

    RANKX (

        ALL ( IMPORT_SQL_monthly[MemberFirm] ),

        CALCULATE ( MEDIAN( IMPORT_SQL_monthly[TimeToSell] ) ),

        CALCULATE ( MEDIAN( IMPORT_SQL_monthly[TimeToSell] ), LOOKUP_MF_USERS[PrincipalName] = firm_userprincipalname )

        ,ASC),

    OR(IMPORT_SQL_monthly[CurrentStatus]="Sold", IMPORT_SQL_monthly[CurrentStatus]="SaleComplete"),

    USERELATIONSHIP( IMPORT_SQL_monthly[DateSold], TBL_CALENDAR_date[Date]),

    USERELATIONSHIP( IMPORT_SQL_monthly[MemberFirm], LOOKUP_MF_USERS[firm])

)

, "-" )

 

Actually the time to sell for the firm parie steele for the year 2013 should be 37 and not 120.I have managed to get the median days right now,again when I try to rank it as below but still dint get the rnking quite right.

testmediantimetosell =

var firm_userprincipalname = [MF_USERPRINCIPALNAME]

RETURN

IF([.Sum_FIRM_Sales_ALL] >= [.val_Rank_Threshold],

CALCULATE(

CALCULATE ( CALCULATE ( MEDIAN( IMPORT_SQL_monthly[TimeToSell] ), LOOKUP_MF_USERS[PrincipalName] = firm_userprincipalname )),

OR(IMPORT_SQL_monthly[CurrentStatus]="Sold", IMPORT_SQL_monthly[CurrentStatus]="SaleComplete"),

   USERELATIONSHIP( IMPORT_SQL_monthly[DateSold], TBL_CALENDAR_date[Date]),

    USERELATIONSHIP( IMPORT_SQL_monthly[MemberFirm], LOOKUP_MF_USERS[firm])

))

 

Correct display of time to sell:

NandinAna_1-1678975884324.png

 

 

Formula used for the above,

.RankMedian_TimeToSell = IF([.Count_Sales_ALL] < 10, "-", RANKX(ALLSELECTED(IMPORT_SQL_monthly[MemberFirm]),[.Median_TimeToSell_ALL],,ASC))

 

.Median_TimeToSell_ALL = CALCULATE(MEDIAN(IMPORT_SQL_monthly[TimeToSell]),OR(IMPORT_SQL_monthly[CurrentStatus]="Sold",IMPORT_SQL_monthly[CurrentStatus]="SaleComplete"), USERELATIONSHIP('TBL_CALENDAR_date'[Date],IMPORT_SQL_monthly[DateSold]), ALL(IMPORT_SQL_monthly[OrganisationId]), USERELATIONSHIP(IMPORT_SQL_monthly[SellingPrice_bin_25K_label], TBL_SLICE_Price[Axis]))

 

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors