Search code examples
excelpowerbipowerquerypowerbi-desktopm

I keep running into a token error while trying to create a custom function in Power Query but I don't know why


enter image description here

I have a custom function that I am writing that will transform dates from Julian to Gregorian as I pull them from SQL. I keep getting a Token ')' expected error in the Function after the first parameter and I can't figure out why. Can someone please help? I will attach the code below.

I have gone through and checked every parentheses and they all appear to match up. I am running out of ideas.

TransformDates = (TableName as text, DateColumns as list) =>
let
      // Connect to the SQL database and load the data
    Source = Sql.Database("Server Hidden", "RepJDE", [Query="SELECT * FROM " & TableName]),
    
    // Define transformation logic for a single column
    TransformColumn = (columnName as text) =>
    let
        // Date Transformation
        #"Split Column by Position" = Table.SplitColumn(Source, columnName, Splitter.SplitTextByPositions({0, 3, 5}, true), {"C", "Y", "D"}),
        #"Changed To Integer" = Table.TransformColumnTypes(#"Split Column by Position",{{"C", Int64.Type}, {"Y", Int64.Type}, {"D", Int64.Type}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed To Integer",{{"C", "Century"}, {"Y", "Year"}, {"D", "Day"}}),
        #"Added BaseDate" = Table.AddColumn(#"Renamed Columns", "BaseDate", each let
        
        Century = [Century],
        YearInCentury = [Year],
        DayOfYear = [Day],
        
        // Convert to a standard date
        FullYear = 1900 + Century * 100 + YearInCentury,
        StandardDate = Date.From((Text.From(FullYear)))
        in
            StandardDate),
            #"Added Date" = Table.AddColumn(#"Added BaseDate", "Date", each let
            HalfwayPoint = [BaseDate],
            Day = [Day],

            // Add the specified number of days to each date in the HalfwayPoint column
            UpdatedColumn = Date.AddDays(HalfwayPoint, Day)


        in
            UpdatedColumn)
    in
        #"Added Date"

    // Apply the TransformColumn function to each column in the list
    TransformedColumns = List.Transform(DateColumns, each TransformColumn(_)),

    // Combine transformed columns into a table
    ResultTable = Table.FromColumns(TransformedColumns, DateColumns)
in
    ResultTable

Solution

  • Change your very first line from:

    TransformDates = (TableName as text, DateColumns as list) =>
    

    To just:

    (TableName as text, DateColumns as list) =>
    

    Then rename your query from Query1 to TransformDates.

    The name of the Query is the name of the Function.