Forum Discussion
Sort Dim Date descending without a circular dependency
- Anonymous2 years ago
Hi Anonymous ,
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.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi Anonymous ,
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.
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.
- Anonymous2 years agoNot applicable
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.