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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Rankx function to find the median using manual formula

I have a summary table called, YF_Summary. So while trying to use the built-in median function, the answer was wrong. So i tried to use the manaul formula to calculate median. My requirement is to find the Median of the Unique_Quotes field for last 30 days(this will not change, its 30 always, so median will always be [15throw+16th row]/2. I have applied the filter to the table visual which will select only the last 30 days(exclude today). I used the below query ,

DAX Query:

Median_Quotes =
VAR MinDate = TODAY() - 30
VAR MaxDate = TODAY()

VAR FilteredTable =
    FILTER(
        ALL('BSDW YF_Summary'),
        'BSDW YF_Summary'[Date] >= MinDate &&
        'BSDW YF_Summary'[Date] <= MaxDate
    )

VAR GroupedTable =
    SUMMARIZE(
        FilteredTable,
        'BSDW YF_Summary'[Date]
    ) 

VAR TotalDays = COUNTROWS(GroupedTable)
VAR MedianRow = DIVIDE(TotalDays, 2)

RETURN
IF(
    TotalDays = 0,
    BLANK(),
    IF(
        MOD(TotalDays, 2) = 0,
        (
            MAXX(
                FILTER(GroupedTable, RANKX(GroupedTable, [Unique_Quotes]) = MedianRow),
                [Unique Quotes]
            ) +
            MAXX(
                FILTER(GroupedTable, RANKX(GroupedTable, [Unique_Quotes]) = MedianRow + 1),
                [Unique Quotes]
            )
        ) / 2,
        MAXX(
            FILTER(GroupedTable, RANKX(GroupedTable, [Unique_Quotes]) = MedianRow + 1),
            [Unique Quotes]
        )
    )
)
The problem is , because of the Rankx functionality, its skipping the 15th, 16th row rank if the consecutive row Unique_Quotes value are same. But irrespective of the value i need to assign rank to the rows. I tried something called Earlier(), skip, and countrows() function too, but it gives balnks as anwer for all rows. Is there anyother way to achieve what i am expecting. 
Current Output :
saranya24894_2-1694779099950.png

Since there are many values repeating it for the consecutive rows/dates, there is no 15th and 16th rank, so it returns blank as answer.

Your guidance would be of great help.
 Thanks in advance. 
0 REPLIES 0

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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