Forum Discussion

PaulDBrown's avatar
PaulDBrown
Icon for Community Champion rankCommunity Champion
9 years ago
Solved

New to "M": help needed with Syntax Error

Hello,

 

I am new to Power Bi, and I'm trying to create a calendar table using M code from a book. The code reads like this:

 

= let
ChangedType = Table.TransformColumnTypes(Campañas,{{"FechaEmisión", type date}}),
MaxDate = Record.Field(Table.Max(ChangedType,"FechaEmisión"),"FechaEmisión"),
MinDate = Record.Field(Table.Min(ChangedType,"FechaEmisión"),"FechaEmisión"),
DaysElapsed = Number.From(MaxDate-MindDate),
DateList = List.Dates(MindDate, DaysElapsed+1,Duration.From(1)),
RawDatesTable = Table.FromList(DatesList,Splitter.SplitByNothing(),
{"Día"}, null, ExtraValues.Error),
ChangedType1 = Table.TransformColumnTypes(RawDatesTable,{{"Día",type date}}),
InsertedMonth = Table.AddColumn(ChangedType1,"Día", each Date.Day([Día]), type number),
InsertedMonth = Table.AddColumn(InsertedDay,"Mes", each Date.Month([Día]), type number),
InsertedYear = Table.AddColumn(InsertedMonth,"Año", each Date.Year([Día]), type number),
In
InsertedYear

 

Which asks for a "=" after the In in the penultimate line (no = sign in the code I'm copying from a book...). When I add an = sign,

as in:

In =

InsertedYear

it then says I need a token "In"...

 

Which suggests that the original code is incomplete?

 

Any help is very much appreciated!

Thank you!

  • There are various errors in your code, like

    • MinDate -> MindDate
    • DateList -> DatesList
    • 2x InsertedMonth
    • 2x Día
    • and maybe others

    Below adjusted code that is working.

    As your code is based on a table Campañas that I don't have, I just created a table.

     

    let
    //    ChangedType = Table.TransformColumnTypes(Campañas,{{"FechaEmisión", type date}}),
        ChangedType = #table(type table[FechaEmisión = date],{{#date(2017,1,1)},{#date(2017,12,31)}}),
        MaxDate = Record.Field(Table.Max(ChangedType,"FechaEmisión"),"FechaEmisión"),
        MinDate = Record.Field(Table.Min(ChangedType,"FechaEmisión"),"FechaEmisión"),
        DaysElapsed = Number.From(MaxDate-MinDate),
        DateList = List.Dates(MinDate, DaysElapsed+1,Duration.From(1)),
        RawDatesTable = Table.FromList(DateList,Splitter.SplitByNothing(),
                {"Fecha"}, null, ExtraValues.Error),
        ChangedType1 = Table.TransformColumnTypes(RawDatesTable,{{"Fecha",type date}}),
        InsertedDay = Table.AddColumn(ChangedType1,"Día", each Date.Day([Fecha]), type number),
        InsertedMonth = Table.AddColumn(InsertedDay,"Mes", each Date.Month([Fecha]), type number),
        InsertedYear = Table.AddColumn(InsertedMonth,"Año", each Date.Year([Fecha]), type number)
    in
        InsertedYear

2 Replies

  • MarcelBeug's avatar
    MarcelBeug
    Icon for Community Champion rankCommunity Champion

    There are various errors in your code, like

    • MinDate -> MindDate
    • DateList -> DatesList
    • 2x InsertedMonth
    • 2x Día
    • and maybe others

    Below adjusted code that is working.

    As your code is based on a table Campañas that I don't have, I just created a table.

     

    let
    //    ChangedType = Table.TransformColumnTypes(Campañas,{{"FechaEmisión", type date}}),
        ChangedType = #table(type table[FechaEmisión = date],{{#date(2017,1,1)},{#date(2017,12,31)}}),
        MaxDate = Record.Field(Table.Max(ChangedType,"FechaEmisión"),"FechaEmisión"),
        MinDate = Record.Field(Table.Min(ChangedType,"FechaEmisión"),"FechaEmisión"),
        DaysElapsed = Number.From(MaxDate-MinDate),
        DateList = List.Dates(MinDate, DaysElapsed+1,Duration.From(1)),
        RawDatesTable = Table.FromList(DateList,Splitter.SplitByNothing(),
                {"Fecha"}, null, ExtraValues.Error),
        ChangedType1 = Table.TransformColumnTypes(RawDatesTable,{{"Fecha",type date}}),
        InsertedDay = Table.AddColumn(ChangedType1,"Día", each Date.Day([Fecha]), type number),
        InsertedMonth = Table.AddColumn(InsertedDay,"Mes", each Date.Month([Fecha]), type number),
        InsertedYear = Table.AddColumn(InsertedMonth,"Año", each Date.Year([Fecha]), type number)
    in
        InsertedYear
    • PaulDBrown's avatar
      PaulDBrown
      Icon for Community Champion rankCommunity Champion

      MarcelBeug

       

      Marcel,

       

      Thank you so much for your help!!

      You, Sir, are a star!

       

      Best regards,

       

      Paul.