Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

changing the source to SQL table from CSV file

Hi,   I am trying to edit the M code to change the source from csv to directly from SQL server. but, i am losing my custom columns. If i just changing the source, it is throwing error. I am quite n...
  • nandukrishnavs's avatar
    6 years ago

    Anonymous 

     

    You could create another query for getting the data from the SQL table. Make sure you have all the columns are available in SQL as well. Column names should be the same. Then go to advance editor and copy PowerQuery. Then replace the initial part of your existing query.

     

    Example

    let
        Source = Sql.Databases("Your source address"),
        #"Databasename" = Source{[Name="Your Data base name"]}[Data],
        dbo_YourTableName = #"Databasename"{[Schema="dbo",Item="YourTableName"]}[Data],
        #"Added Custom" = Table.AddColumn(#"dbo_YourTableName", "Aircraft", each if Text.Contains([Rank], "320") then "320" else
    if Text.Contains([Rank], "321") then "320" else "ATR"),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Position", each if Text.Contains([Rank], "CP") then "CP" else "FO"),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each true),
        #"Added Custom2" = Table.AddColumn(#"Filtered Rows", "Status_key", each if Text.Contains([STATUS], "Line Pilot") then 1 else if Text.Contains([STATUS], "Management Pilot") then 1 else if Text.Contains([STATUS], "Trainer") then 1 else if [TrainingQualfication] = "SFI" then 0 else 0),
        #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Status_Key1", each if [TrainingQualfication] = "SFI" then 0 else [Status_key]),
        #"Filtered Rows1" = Table.SelectRows(#"Added Custom3", each true),
        #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows1",{{"Status_key", Int64.Type}}),
        Custom1 = #"Changed Type1"
    in
        Custom1

     



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    🙂