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

View all the Fabric Data Days sessions on demand. View schedule

Reply
victoryamaykin
Advocate I
Advocate I

RANKX for Measure Calculated with DatesBetween()

I'm making a leaderboard for the backroom where employees tag donations as they get sorted. Each item tagged is said to be "produced". Here's an exampe of the table called FactUserProduction. I have a DimDates and DimUser tables connected to this fact table.  I need the rank of the employees for the Items they produced last week. These names are fake data. Sorry that I can't provide a test pbix file. 

 

DateTeamMemberItemsStocked
8/11/2024Vlad Biford2926
8/11/2024Jo Peotz2286
8/11/2024Zsa zsa Lewing2158
8/11/2024Drucy Ivanin2000
8/11/2024Waylen Faint1774
8/15/2024Mikel Northidge1454
8/15/2024Fayre Winterson1323
8/15/2024Kelcie Guy1265
8/15/2024Clarissa Topliss569
8/15/2024Juline Ganter542


It's not working as expected.

TeamMemberItemsStockedRank
Vlad Biford29264
Jo Peotz22869
Zsa zsa Lewing215810
Drucy Ivanin200014
Waylen Faint177420
Mikel Northidge145428
Fayre Winterson132339
Kelcie Guy126545
Clarissa Topliss569142
Juline Ganter542150



Here's the DAX code as I have it now: 

 

Rank User Items Produced Last Week :=
VAR _start =
    DATEADD(
        DimDates[Start of Week], -7 , DAY
    )

VAR _end =
    DATEADD(
        DimDates[End of Week], -7 , DAY
    )

VAR _rank_items_produced =
 
    RANKX(
       
         ALL(DimUser[EmployeeName]),
 
        CALCULATE(
 
            SUMX(FactUserProduction,[ItemsStocked]),
 
            DATESBETWEEN(DimDates[Date], _start, _end)
 
        ),        ,
 
        DESC,
 
        Dense
 
    )

RETURN
IF(
    HASONEVALUE(FactUserProduction[TeamMember]),
    _rank_items_produced
)

Any help you can provide is appreciated. I'm using the hasonevalue() to remove the total for rank in the table.
1 ACCEPTED SOLUTION

I have a date filter calculated column called "isToday" to get the current day. The start and end of week is based on that date in the DimDates table. The DimDates table is much more involved than what I put in the sample file. 

Is there another way to get items produced last week? Here's what I've been doing: 

Items Produced Last Week =

VAR _start =
    DATEADD(
        DimDates[Start of Week], -7 , DAY)
// error message here says parameter is not the correct type, but it was working before
VAR _end =
    DATEADD(
        DimDates[End of Week], -7 , DAY)

RETURN
CALCULATE(
    SUM(FactUserProduction[ItemsStocked]),
    DATESBETWEEN(DimDates[Date], _start, _end)
)

View solution in original post

7 REPLIES 7
lbendlin
Super User
Super User

hard to help without seeing the date table definition or the data model.  Most likely you need to replace 

 

ALL(DimUser[EmployeeName])

 

with

 

ALLSELECTED(DimUser[EmployeeName])

Thanks for your response. This solution works in my semantic model file, but for some reason, it will not work the same way in the report file. Any ideas? How do I upload a pbix file? 
I am using a page filter with a calculated column called "isToday" to get today's results. There is another filter for the department as well. 

victoryamaykin_0-1723833252936.png

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

PBIX file: 
https://drive.google.com/file/d/1zSnhbSKhsHYeJ5iAO0SGvRdqqSUrwU5y/view?usp=sharing 

Data: 
https://docs.google.com/spreadsheets/d/1cFX4lYAsa1AS1FtFIdx7qG6aWUZgAF_LJB3FCZ7hGAY/edit?usp=sharing...

Expected outcome: 

EmployeeNameRank User Items Produced Last WeekItems Produced Last Week
evan brinton12926
Barbara Bunten22286
Alan Kester32158
Vicky Williams42000
Arlene Swindell51774
patrick cooksey61454
Bryan Jones71323
Amber Eisman81265
Molly Irwin9569
Eric Metzler10542

SELECTEDVALUE(DimDates[Start of Week])  has no meaning as you don't have a filter on the date and the calendar table is connected to the data model.  This would only work if you had a date picker slicer AND if it would be fed by a disconnected table.
 
a simple ranking formula would be
 
Rnk = rankx(allselected(DimUser[EmployeeName]),calculate(sum(FactUserProduction[Items Produced])))
 
Note that the calculation needs to happen inside the RANKX, not before.

I have a date filter calculated column called "isToday" to get the current day. The start and end of week is based on that date in the DimDates table. The DimDates table is much more involved than what I put in the sample file. 

Is there another way to get items produced last week? Here's what I've been doing: 

Items Produced Last Week =

VAR _start =
    DATEADD(
        DimDates[Start of Week], -7 , DAY)
// error message here says parameter is not the correct type, but it was working before
VAR _end =
    DATEADD(
        DimDates[End of Week], -7 , DAY)

RETURN
CALCULATE(
    SUM(FactUserProduction[ItemsStocked]),
    DATESBETWEEN(DimDates[Date], _start, _end)
)

 

 

VAR _start =
    DATEADD(
        DimDates[Start of Week], -7 , DAY)

 

 

This returns a table, not a scalar value. Not something you can use in DATESBETWEEN.

 

Something like 

 

 

MIN(DimDates[Start of Week])-7

 

 

  • might work. But since you already have a calculated column for "IsToday" then you can add another one for "IsInLastWeek".

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors