Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
ScottBrown
Helper II
Helper II

Week Index Column for Power Query

I am trying to create a week index for Power Query. 

  • It would show something like this for the date range  ...-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5... for all weeks in the date table.
  • My formula for the Week Index is below with the other related fields.
  • However it is caluclating an incorrect offset of 7. 

Any thoughts or if I am using the incorrect calculations?

 

M Code
StartOfCurrentWeek = Table.TransformColumnTypes(Table.AddColumn(#"Fiscal Year Status", "StartOfCurrentWeek", each Date.StartOfWeek(DateTime.FixedLocalNow(),+1)), {{"StartOfCurrentWeek", type date}}),
  StartOfWeek = Table.TransformColumnTypes(Table.AddColumn(StartOfCurrentWeek, "StartOfWeek", each Date.StartOfWeek([DateKey], 1)), {{"StartOfWeek", type date}}),
  #"Fiscal Week Index" = Table.TransformColumnTypes(Table.AddColumn(StartOfWeek, "Fiscal Week Index", each [StartOfWeek] - [StartOfCurrentWeek]), {{"Fiscal Week Index", Int64.Type}}),
ScreenshotScottBrown_0-1733549534606.png

 

 
1 ACCEPTED SOLUTION
ZhangKun
Super User
Super User

7 or -7 is the difference in days, not weeks, so just divide by 7.

#"Fiscal Week Index" = Table.TransformColumnTypes(
  Table.AddColumn(
    StartOfWeek, 
    "Fiscal Week Index", 
    each ([StartOfWeek] - [StartOfCurrentWeek]) / 7
  ),
  {{"Fiscal Week Index", Int64.Type}}
)

View solution in original post

1 REPLY 1
ZhangKun
Super User
Super User

7 or -7 is the difference in days, not weeks, so just divide by 7.

#"Fiscal Week Index" = Table.TransformColumnTypes(
  Table.AddColumn(
    StartOfWeek, 
    "Fiscal Week Index", 
    each ([StartOfWeek] - [StartOfCurrentWeek]) / 7
  ),
  {{"Fiscal Week Index", Int64.Type}}
)

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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