Get Week number in Power Query Custom Column

powerwill

Board Regular
Joined
Sep 14, 2018
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hi experts!

This is the formula I use to get the Week number in Excel, that takes 1-7 of every month as Week 1, then 8-14 as Week 2..and so on...It resets back to Week 1 once the next month starts.

="Week "&IF((QUOTIENT(DAY([@[Createdon Date]])-1,7)+1)>4,4,(QUOTIENT(DAY([@[Createdon Date]])-1,7)+1))

I am now trying to replicate this formula to add a custom column in Power Query using the below M code I used but I get an error. Any thoughts?

Power Query:
Week =
"Week " & IF(
    (INT(DATEDIFF(MIN(TableName[Createdon Date]), MIN(TableName[Createdon Date]), WEEK) % 4) + 1 > 4,
    4,
    (INT(DATEDIFF(MIN(TableName[Createdon Date]), MIN(TableName[Createdon Date]), WEEK) % 4) + 1)
)

Error:

Expression.Error: We cannot convert the value #date(2022, 2, 8) to type Duration.
Details: Value=08-02-2022
Type=[Type]
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You have that function on the ribbon to extract the Weeknumber.
Also in a recent thread we have shared a couple of ways to have isoweeknumber function available in PQ.
If I can find it back will share a link to that one.
 
Upvote 0
I misunderstood this thread before.
So there is indeed an action on the ribbon (Week of Month) , however it's not exactly a replicate of your formula in excel.
You tried to concatenate text with number and that works fine in excel, it's forbidden in Power Query. So you need to turn the number to text first.
Both ways are in the query below.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Dates", type datetime}}),
    #"Inserted Week of Month" = Table.AddColumn(#"Changed Type", "Week of Month", each "Week " & Text.From(Date.WeekOfMonth([Dates], 0))),
    #"Calc Week of Month" = Table.AddColumn(#"Inserted Week of Month", "Week of Month calc", each
let
    Quotient = Number.IntegerDivide(Date.Day([Dates])-1,7)+1,
    WeekNr = if Quotient > 4 then 4 else Quotient
in
    "Week " & Text.From(WeekNr) )
in
    #"Calc Week of Month"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top