Forum Discussion
M Code: Day of Fiscal Year (date table)
- 3 years ago
You can use this logic and get this result:
Here is sample code to paste in and test with:
let Source ={Number.From(#date(2020,5,1))..Number.From(#date(2027,4,30))}, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}), #"Added Fiscal Year" = Table.AddColumn(#"Changed Type", "Fiscal Year", each if Date.Month([Date]) > 4 then Date.Year([Date]) + 1 else Date.Year([Date]), Int64.Type), #"Grouped Rows" = Table.Group( #"Added Fiscal Year", {"Fiscal Year"}, { { "All Rows", each Table.AddIndexColumn( Table.Sort(_, {"Date", Order.Ascending}), "Day of Year", 1 ), type table [Date=nullable date, Fiscal Year=number, Day of Year=number] } } ), #"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Date", "Day of Year"}, {"Date", "Day of Year"}) in #"Expanded All Rows"Here is what I did:
First, I Grouped the data by a fiscal year column, so May 1, 2022 through April 30, 2023 would be in the Fiscal 2023 group. The aggregation was "All Rows"
Then I edited the M code. That Grouping will just have an "each _, type table..."
I wrapped that with Table.AddIndexColumn and Table.Sort (to ensure the dates were ascending)Then I expanded everything but the Fiscal Year column. You will want to reorder your columns to put Fiscal Year where it belongs vs in the first column.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
You can use this logic and get this result:
Here is sample code to paste in and test with:
let
Source ={Number.From(#date(2020,5,1))..Number.From(#date(2027,4,30))},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),
#"Added Fiscal Year" = Table.AddColumn(#"Changed Type", "Fiscal Year", each if Date.Month([Date]) > 4 then Date.Year([Date]) + 1 else Date.Year([Date]), Int64.Type),
#"Grouped Rows" =
Table.Group(
#"Added Fiscal Year",
{"Fiscal Year"},
{
{
"All Rows",
each
Table.AddIndexColumn(
Table.Sort(_, {"Date", Order.Ascending}),
"Day of Year",
1
),
type table [Date=nullable date, Fiscal Year=number, Day of Year=number]
}
}
),
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Date", "Day of Year"}, {"Date", "Day of Year"})
in
#"Expanded All Rows"
Here is what I did:
First, I Grouped the data by a fiscal year column, so May 1, 2022 through April 30, 2023 would be in the Fiscal 2023 group. The aggregation was "All Rows"
Then I edited the M code. That Grouping will just have an "each _, type table..."
I wrapped that with Table.AddIndexColumn and Table.Sort (to ensure the dates were ascending)
Then I expanded everything but the Fiscal Year column. You will want to reorder your columns to put Fiscal Year where it belongs vs in the first column.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.