Forum Discussion
Sorting MonthShort based on YearMonthNumber values (for current vs prior year on one figure)
Hi all,
I have a requirement to show all 12 months in X-Axis so that I bring on Current Year vs. Last years sales data. My issue now is to sort months on a chronological order, but can't use other fields like YearMonth.
This is easy if I wanted to use MM YYYY on axis then sort that. but no I need to use MM on x-axis but also respect time chronology.
Here is my visual: (current month should be the most recent one).
here is my Date table:
let
Source = #date(2017, 1, 1),
Custom1 = List.Dates(Source, Number.From(DateTime.LocalNow())- Number.From(Source) ,#duration(1,0,0,0)),
#"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Duplicated Column" = Table.DuplicateColumn(#"Converted to Table", "Column1", "Column1 - Copy"),
#"Extracted Year" = Table.TransformColumns(#"Duplicated Column",{{"Column1 - Copy", Date.Year}}),
#"Renamed Columns" = Table.RenameColumns(#"Extracted Year",{{"Column1 - Copy", "Year"}}),
#"Duplicated Column1" = Table.DuplicateColumn(#"Renamed Columns", "Column1", "Column1 - Copy"),
#"Renamed Columns1" = Table.RenameColumns(#"Duplicated Column1",{{"Column1 - Copy", "Month"}}),
#"Inserted Month" = Table.AddColumn(#"Renamed Columns1", "Month.1", each Date.Month([Month]), type number),
#"Renamed Columns2" = Table.RenameColumns(#"Inserted Month",{{"Month.1", "MonthName"}}),
#"Extracted Month Name" = Table.TransformColumns(#"Renamed Columns2", {{"Month", each Date.MonthName(_), type text}}),
#"Renamed Columns3" = Table.RenameColumns(#"Extracted Month Name",{{"MonthName", "Month"}, {"Month", "MonthName"}}),
#"Duplicated Column2" = Table.DuplicateColumn(#"Renamed Columns3", "Column1", "Column1 - Copy"),
#"Inserted Day" = Table.AddColumn(#"Duplicated Column2", "Day", each Date.Day([#"Column1 - Copy"]), type number),
#"Inserted Day Name" = Table.AddColumn(#"Inserted Day", "Day Name", each Date.DayOfWeekName([#"Column1 - Copy"]), type text),
#"Inserted Days in Month" = Table.AddColumn(#"Inserted Day Name", "DaysInMonth", each Date.DaysInMonth([#"Column1 - Copy"]), type number),
#"Inserted Week of Year" = Table.AddColumn(#"Inserted Days in Month", "WeekOfYear", each Date.WeekOfYear([#"Column1 - Copy"]), type number),
#"Inserted Week of Month" = Table.AddColumn(#"Inserted Week of Year", "WeekOfMonth", each Date.WeekOfMonth([#"Column1 - Copy"]), type number),
#"Inserted Start of Week" = Table.AddColumn(#"Inserted Week of Month", "StartOfWeek", each Date.StartOfWeek([#"Column1 - Copy"]), type date),
#"Inserted End of Week" = Table.AddColumn(#"Inserted Start of Week", "EndOfWeek", each Date.EndOfWeek([StartOfWeek]), type date),
#"Added Custom" = Table.AddColumn(#"Inserted End of Week", "MonthShort", each Date.ToText([Date], "MMM","es-419")),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"MonthShort"}),
#"Renamed Columns4" = Table.RenameColumns(#"Removed Columns",{{"Column1", "Date"}}),
#"Added Custom1" = Table.AddColumn(#"Renamed Columns4", "MonthShort", each Date.ToText([Date],"MMM")),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Column1 - Copy"}),
#"Added Conditional Column" = Table.AddColumn(#"Removed Columns1", "Period", each if [Month] = 1 then 7 else if [Month] = 2 then 8 else if [Month] = 3 then 9 else if [Month] = 4 then 10 else if [Month] = 5 then 11 else if [Month] = 6 then 12 else if [Month] = 7 then 1 else if [Month] = 8 then 2 else if [Month] = 9 then 3 else if [Month] = 10 then 4 else if [Month] = 11 then 5 else 6),
#"Added Prefix" = Table.TransformColumns(#"Added Conditional Column", {{"Period", each "0" & Text.From(_, "en-AU"), type text}}),
#"Extracted Last Characters" = Table.TransformColumns(#"Added Prefix", {{"Period", each Text.End(_, 2), type text}}),
#"Added Custom2" = Table.AddColumn(#"Extracted Last Characters", "FY", each if [Month] > 6 then [Year] else [Year] -1),
#"Inserted Merged Column" = Table.AddColumn(#"Added Custom2", "YP", each Text.Combine({Text.From([FY], "en-AU"), [Period]}, ""), type text),
#"Changed Type" = Table.TransformColumnTypes(#"Inserted Merged Column",{{"Date", type date}}),
#"Month Year" = Table.AddColumn(#"Changed Type", "Month Year", each Text.From([MonthName]) & " " & Text.From([Year])),
#"Month Number" = Table.AddColumn(#"Month Year", "Month Number", each Text.PadStart(Text.From([Month]),2,"0")),
YearMonthNumber = Table.AddColumn(#"Month Number", "YearMonthNumber", each Text.Combine({Text.From([Year], "en-AU"), Text.From([Month Number], "en-AU")}, ""), type text),
#"Inserted Start of Month" = Table.AddColumn(YearMonthNumber, "Start of Month", each Date.StartOfMonth([Date]), type date),
#"Inserted End of Month" = Table.AddColumn(#"Inserted Start of Month", "End of Month", each Date.EndOfMonth([Date]), type date),
#"Sorted Rows" = Table.Sort(#"Inserted End of Month",{{"Date", Order.Descending}})
in
#"Sorted Rows"
thanks in advance,
I found the solution myself. best way is to create a mini table for month sorting, and do a calculated column to sort like below:
MonthSorting = if (MonthSorting[MonthNumber] < month(now()),month(now())-MonthSorting[MonthNumber],abs(MonthSorting[MonthNumber]-(12 +month(now()))))this will sort based on months away from current month, and does the trick!
5 Replies
- Ashish_MathurSuper User
Hi,
In the Data Model window, you should be able to sort the Month name column in the Calendar Table (from A to Z) by the Month number.
- sshokri89Helper I
Hi Ashish and thanks for your answer,
but that's not what I'm after. actually that is alraedy in place but that will only sort it from Jan - Dec in order. what I want is to sort from 12 months ago until Month(Today()) in order, e.g. for today it will start from October 2020... Nov 2020....Jan 2021....May 2021...July 20201....September 2021.
- Ashish_MathurSuper User
Hi,
You need to see 12 months rolling information for the month selected by the end user. Please see if my solution here helps - Flex a Pivot Table to show data for x months ended a certain user defined month.
- sshokri89Helper I
I found the solution myself. best way is to create a mini table for month sorting, and do a calculated column to sort like below:
MonthSorting = if (MonthSorting[MonthNumber] < month(now()),month(now())-MonthSorting[MonthNumber],abs(MonthSorting[MonthNumber]-(12 +month(now()))))this will sort based on months away from current month, and does the trick!