Forum Discussion

Simon_Evans's avatar
Simon_Evans
Helper I
4 years ago
Solved

if statements in M code

Hi all

 

I'm attempting to get a URL to have a dynmamically built start date based on the current date. This is so that I don't have to come in to the editor and change the URL each year.

 

Below is the code snippet for the list.generate function which this is all contained within. The URLtoDate works perfectly fine it is just the startyear variable I am having trouble with.

 

let
SessionAttendance = List.Generate( () =>
[
todate = Date.AddDays(Date.From(DateTime.LocalNow()),+1),
year = Number.ToText(Date.Year(todate)),
month = Text.PadStart(Number.ToText(Date.Month(todate)),2,"0"),
day = Text.PadStart(Number.ToText(Date.Day(todate)),2,"0"),
URLtoDate = year&"-"&month&"-"&day,
startyear = if month = "08" or month = "09" or month = "10" or month = "11" or month = "12" then startyear = year else startyear = year - 1,
StartDate = startyear&"-"&"08-31",

URL = "/attendance/session?attendance_date_after="&StartDate&"&attendance_date_before="&URLtoDate,

Result = Json.Document(Web.Contents("https://********************/v1.0/schools/**************",[RelativePath=(URL)]))],

 

The error I'm getting is "The import startyear matches no exports. Did you miss a module reference?"

 

So it's not liking the way I have formatted the if statement. Could anyone please assist with how I should be writing the if statement please?

 

Kindest regards

Simon

  • Simon_Evans , I think the problem is here with syntax 

     

    startyear = if month = "08" or month = "09" or month = "10" or month = "11" or month = "12" then startyear = year else startyear = year - 1,

     

    You need use only year there

     

    startyear = if month = "08" or month = "09" or month = "10" or month = "11" or month = "12" then  year else  year - 1

1 Reply

  • Simon_Evans , I think the problem is here with syntax 

     

    startyear = if month = "08" or month = "09" or month = "10" or month = "11" or month = "12" then startyear = year else startyear = year - 1,

     

    You need use only year there

     

    startyear = if month = "08" or month = "09" or month = "10" or month = "11" or month = "12" then  year else  year - 1