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, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Powell360
Helper I
Helper I

Dynamic Sorting Based on Current Month

I have creatded a reference table in my .pbix so that I can order months in one of a few ways, as dictated by the context of the report. 

 

I have a table which contains  MonthName, CallendarSort, FYSort and DynamicSort.

 

Month name is a string, callendar sort has the numeric value based on the callendar (Jan-Dec), FYSort has the numerical value based on financial year (Apr-Mar) and I want to create DynamicSort based on the current month with current month = 12, previous month = 11 and so on.

 

I implemented a very rough nested "if" solution but it won't work for months still to come based on the standard calendar. Here is the script I have used to populate my custom column, can anyone let me know what changes I need to make please?

 

Thanks in advance

 

if([CallendarSort]=Date.Month(DateTime.LocalNow())) then(12) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-1) then(11) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-2) then(10) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-3) then(9) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-4) then(8) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-5) then(7) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-6) then(6) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-7) then(5) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-8) then(4) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-9) then(3) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-10) then(2) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-11) then(1) else(
0
)
)
)
)
)
)
)
)
)
)
)
)
1 ACCEPTED SOLUTION
Powell360
Helper I
Helper I

I was hoping to come up wit

if([CallendarSort]=Date.Month(DateTime.LocalNow())) then(12) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-1) then(11) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-2) then(10) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-3) then(9) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-4) then(8) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-5) then(7) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-6) then(6) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-7) then(5) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-8) then(4) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-9) then(3) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-10) then(2) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-11) then(1) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+1) then(1)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+2) then(2)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+3) then(3)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+4) then(4)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+5) then(5)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+6) then(6)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+7) then(7)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+8) then(8)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+9) then(9)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+10) then(10)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+11) then(11)else(
0)))))))))))))))))))))))

h a more elegant solution but I have just continued the logic of the 1st draft to look forwards as well

View solution in original post

2 REPLIES 2
Powell360
Helper I
Helper I

I was hoping to come up wit

if([CallendarSort]=Date.Month(DateTime.LocalNow())) then(12) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-1) then(11) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-2) then(10) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-3) then(9) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-4) then(8) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-5) then(7) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-6) then(6) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-7) then(5) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-8) then(4) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-9) then(3) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-10) then(2) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())-11) then(1) else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+1) then(1)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+2) then(2)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+3) then(3)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+4) then(4)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+5) then(5)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+6) then(6)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+7) then(7)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+8) then(8)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+9) then(9)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+10) then(10)else(
if([CallendarSort]=Date.Month(DateTime.LocalNow())+11) then(11)else(
0)))))))))))))))))))))))

h a more elegant solution but I have just continued the logic of the 1st draft to look forwards as well

Anonymous
Not applicable

Thank you very much!! You help me a lot.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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