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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
reynaldo_malave
Helper III
Helper III

Rank day of the week by date

Hi guys,

I am working on a forecast. I am evaluating last 4 weeks and need to rank dates by day of the week like the following table:

Date                          Dow      Rank

02-07-202151
03-07-202161
04-07-202101
05-07-202111
06-07-202121
07-07-202131
08-07-202141
09-07-202152
10-07-202162
11-07-202102
12-07-202112
13-07-202122
14-07-202132
15-07-202142
16-07-202153
17-07-202163
18-07-202103
19-07-202113
20-07-202123
21-07-202133
22-07-202143
23-07-202154
24-07-202164
25-07-202104
26-07-202114
27-07-202124
28-07-202134
29-07-202144

 

I develop the following dax:

 

 

 

 

 

 

 

forecast =
VAR fecha28dias =
    TODAY () - 29
    
VAR ventas28dias =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Venta, Venta[fecha] ),
            "Venta", [$CLP Total],
            "diasemana", WEEKDAY ( Venta[fecha], 2 ),
            "indice", 
            	var fecha = Venta [Fecha] 
            	var diasemana = WEEKDAY(Venta[Fecha], 2)
            	return
            		CALCULATE(
            			RANK.EQ( fecha, Venta[fecha], ASC ),
            			FILTER( KEEPFILTERS( Venta ),  WEEKDAY(Venta[Fecha], 2) = diasemana )
            		)
            		
        ),
        Calendario[Fecha] >= fecha28dias
    )

 

 

 

 

 

 

This return a table like the following table, which gives me 1 only on the first week, I don´t really undesrtand the beahavior.

 

reynaldo_malave_0-1627671052182.png

I did reference the following article, https://community.powerbi.com/t5/Desktop/DAX-Ranking-Date/m-p/98237 

Any help would be greately appreciated.

 

Thanks, 

 

Reynaldo

1 ACCEPTED SOLUTION

I did find a solution here https://community.powerbi.com/t5/Desktop/DAX-Ranking-Date/m-p/98237 

Althought I was looking for a table within my code.

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

That's not really a ranking question, it's more related to date math.  You can subtract "Today" from the date, and integer divide the difference by 7. Add the desired offset (for example +1) as needed.

thanks @lbendlin , 

I did try your solution but is not what I am looking for. The result is increasing while I move further apart form today. I need the biggest number to match my closest sales. 

I did solve it creating the table in my model and the using it within my dax code. I would have love to do it all using variables inside the code.

Here is the code and the result of your "rank" vs my "rank2"

 

fecharankeada =

    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Venta, Venta[fecha] ),            
            "rank", ROUNDUP( DATEDIFF(  Venta[fecha], ultimafecha, day ) / 7, 0 ),
            "rank2", LOOKUPVALUE( Rank28dias[rankfecha], Rank28dias[Fecha], Venta[fecha] )
        ),
        Calendario[Fecha] >= fecha28dias
    )

 

reynaldo_malave_0-1628002769942.png

 



I did find a solution here https://community.powerbi.com/t5/Desktop/DAX-Ranking-Date/m-p/98237 

Althought I was looking for a table within my code.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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