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
nickmart_bi
Frequent Visitor

Sort Dim Date descending without a circular dependency

I want to avoid using slicers on my sheet, and in the filter pane there is no obvious way to sort my DimDate[Date] column descending. I have attempted to create rankx expressions to create an ordinal/sort column, but I continue to get a circular dependency when I attempt to sort using this column. My DimDate table references the date column from my fact table and I cannot tell if this is the root of the problem, but I want the calendar to be dynamic and correctly represent my dataset. How do I sort my DimDate[Date] column descending?

 

DimDate = CALENDAR (
min(Fact_Table[RECORD_DATE]),
date(YEAR(today()), month(TODAY()), day(TODAY()))
)
 
1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @nickmart_bi ,

 

I suggest you to create a Calendar table in Power Query Editor and add an index to sort [Date] column.

For reference: Create Calendar Table Using Power Query M Language

let
    Source = Excel.Workbook(File.Contents("C:\Users\...\Desktop\Sample.xlsx"), null, true),
    Sheet2_Sheet = Source{[Item="Sheet2",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Value", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Date", "RECORD_DATE"}}),
    RECORD_DATE1 = #"Renamed Columns"[RECORD_DATE],
    #"Converted to Table" = Table.FromList(RECORD_DATE1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Filtered Rows" = Table.SelectRows(#"Converted to Table", each ([Column1] = List.Min(RECORD_DATE1))),
    #"Renamed Columns1" = Table.RenameColumns(#"Filtered Rows",{{"Column1", "Start Date"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns1", "End Date", each Date.From(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Date", each {Number.From([Start Date])..Number.From([End Date])}),
    #"Expanded Date" = Table.ExpandListColumn(#"Added Custom1", "Date"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Start Date", "End Date"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Date", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type)
in
    #"Added Index"

Result is as below.

vrzhoumsft_0-1695625423819.png

vrzhoumsft_1-1695625507417.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
v-rzhou-msft
Community Support
Community Support

Hi @nickmart_bi ,

 

I suggest you to create a Calendar table in Power Query Editor and add an index to sort [Date] column.

For reference: Create Calendar Table Using Power Query M Language

let
    Source = Excel.Workbook(File.Contents("C:\Users\...\Desktop\Sample.xlsx"), null, true),
    Sheet2_Sheet = Source{[Item="Sheet2",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Value", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Date", "RECORD_DATE"}}),
    RECORD_DATE1 = #"Renamed Columns"[RECORD_DATE],
    #"Converted to Table" = Table.FromList(RECORD_DATE1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Filtered Rows" = Table.SelectRows(#"Converted to Table", each ([Column1] = List.Min(RECORD_DATE1))),
    #"Renamed Columns1" = Table.RenameColumns(#"Filtered Rows",{{"Column1", "Start Date"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns1", "End Date", each Date.From(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Date", each {Number.From([Start Date])..Number.From([End Date])}),
    #"Expanded Date" = Table.ExpandListColumn(#"Added Custom1", "Date"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Start Date", "End Date"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Date", Order.Descending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type)
in
    #"Added Index"

Result is as below.

vrzhoumsft_0-1695625423819.png

vrzhoumsft_1-1695625507417.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

For my use case this worked well, adding index columns for different groups was pretty straight forward and sorting columns to adjust the direction of the index was easy. 

Ahmedx
Super User
Super User

 

pls try this

sort by Order column

 

Calendar = VAR BaseCalendar =
VAR _ToDay = TODAY()
RETURN
    CALENDAR ( min(Fact_Table[RECORD_DATE]), date(YEAR(_ToDay), month(_ToDay), day(_ToDay))) 
 
RETURN
    GENERATE (
        BaseCalendar,
        VAR BaseDate = [Date]
        VAR YearDate = YEAR ( BaseDate )
        VAR MonthNumber = MONTH ( BaseDate )
        VAR DayMonth = DAY(  BaseDate )
        VAR YearMonthDayNumber = (YearDate * 100 + MonthNumber)*100+DayMonth
        RETURN ROW (
            "Year", YearDate,
            "Month Number", MonthNumber,
            "Month", FORMAT ( BaseDate, "mmmm"),
            "Order", YearMonthDayNumber,
            "Year Month", FORMAT ( BaseDate, "mmm yy")
        )
    )

 

 

This does seem to work around the circular dependency error. Any tips how to add a descending order? So most recent to earliest date.

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.