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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Einomi
Helper V
Helper V

Declare a variable in a step

Hi all,

 

I have the following code to add the ISO week number based on my column date, which is called DateProduced

 

AddWeekNo = Table.AddColumn(#"Colonnes renommées1", "Week No",
            each if
            Number.RoundDown((Date.DayOfYear([DateProduced])-(Date.DayOfWeek([DateProduced], Day.Sunday)+1)+10)/7)=0
            then 
            Number.RoundDown((Date.DayOfYear(#date(Date.Year([DateProduced])-1,12,31))-(Date.DayOfWeek(#date(Date.Year([DateProduced])-1,12,31), Day.Sunday)+1)+10)/7)
            else if
            (Number.RoundDown((Date.DayOfYear([DateProduced])-(Date.DayOfWeek([DateProduced], Day.Sunday)+1)+10)/7)=53
            and (Date.DayOfWeek(#date(Date.Year([DateProduced]),12,31), Day.Sunday)+1<4))
            then
            1
            else 
            Number.RoundDown((Date.DayOfYear([DateProduced])-(Date.DayOfWeek([DateProduced], Day.Sunday)+1)+10)/7)),

 

I use this step into many queries and I would like to add a variable INSIDE this step to be able to update the column date, in this query my dates are sitting in the column DateProduced, but in other queries they are sitting in different columns names such as OrderDate, Date etc...

 

Anyone can help ?

 

Thanks for your time

1 ACCEPTED SOLUTION
artemus
Microsoft Employee
Microsoft Employee

oh sorry about that the date at the end should be removed:

AddWeekNo = Table.AddColumn(#"Colonnes renommées1", "Week No", each 
            let date = [DateProduced] in
if
            Number.RoundDown((Date.DayOfYear(date)-(Date.DayOfWeek(date, Day.Sunday)+1)+10)/7)=0
            then 
            Number.RoundDown((Date.DayOfYear(#date(Date.Year(date)-1,12,31))-(Date.DayOfWeek(#date(Date.Year(date)-1,12,31), Day.Sunday)+1)+10)/7)
            else if

etc....

 in fact you should be able to just add this almost anywhere:

let myVar = SomeCalculation in

 

View solution in original post

8 REPLIES 8
Einomi
Helper V
Helper V

Thanks @artemus 

 

However, I am not sure where to put it in my M code above

 

Does it work if I do like below ?

 

AddWeekNo = Table.AddColumn(#"Colonnes renommées1", "Week No",
            let
               date = [DateProduced]
            in
               date


each if
            Number.RoundDown((Date.DayOfYear(date)-(Date.DayOfWeek(date, Day.Sunday)+1)+10)/7)=0
            then 
            Number.RoundDown((Date.DayOfYear(#date(Date.Year(date)-1,12,31))-(Date.DayOfWeek(#date(Date.Year(date)-1,12,31), Day.Sunday)+1)+10)/7)
            else if

etc....

 

artemus
Microsoft Employee
Microsoft Employee

The problem with your example is that you have the each after the let. Any code before each doesn't have access to the current row. In short, a let before the each would make any values you declare the same for every row. If after the each, then it can access the row data.

 

. You can put the let in anaywhere you would put a number or a string, or similar value. 

@artemus  I am sorry I am still confused, any chance you could help with writing the code, look like you know very good M code 🙂 😉

artemus
Microsoft Employee
Microsoft Employee

From your example, you would move the each up to just before your let statement like:

AddWeekNo = Table.AddColumn(#"Colonnes renommées1", "Week No", each 
            let
               date = [DateProduced]
            in
               date


if
            Number.RoundDown((Date.DayOfYear(date)-(Date.DayOfWeek(date, Day.Sunday)+1)+10)/7)=0
            then 
            Number.RoundDown((Date.DayOfYear(#date(Date.Year(date)-1,12,31))-(Date.DayOfWeek(#date(Date.Year(date)-1,12,31), Day.Sunday)+1)+10)/7)
            else if

etc....

Hi @artemus 

 

Thanks for your reply.

 

However, my query editor indicated me that we need a comma like below

 

let
    mydate = [DateProduced]
in
    mydate,

etc...

 

From here there is no more syntax erros, however, in the UI, I got an error saying that PQ cannot find mydate...

artemus
Microsoft Employee
Microsoft Employee

oh sorry about that the date at the end should be removed:

AddWeekNo = Table.AddColumn(#"Colonnes renommées1", "Week No", each 
            let date = [DateProduced] in
if
            Number.RoundDown((Date.DayOfYear(date)-(Date.DayOfWeek(date, Day.Sunday)+1)+10)/7)=0
            then 
            Number.RoundDown((Date.DayOfYear(#date(Date.Year(date)-1,12,31))-(Date.DayOfWeek(#date(Date.Year(date)-1,12,31), Day.Sunday)+1)+10)/7)
            else if

etc....

 in fact you should be able to just add this almost anywhere:

let myVar = SomeCalculation in

 

Hi @artemus it seems to work now, thanks for your time and your help

artemus
Microsoft Employee
Microsoft Employee

you can use let .. in anywhere e.g.

Table.AddColumn(Source, each 
let 
   sum = [A] + [B],
   calc = (sum - [A]) * (sum - [B])
in
   calc)

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.