Forum Discussion

Juri_Messenboeck's avatar
Juri_Messenboeck
Copper Contributor
Dec 24, 2019

Changing data from imported websites

In Leila Gharani's video (https://www.youtube.com/watch?v=NdUZx_yyEqY&ab_channel=LeilaGharanihttps://www.youtube.com/watch?v=NdUZx_yyEqY&ab_channel=LeilaGharani) she shows how to import weather data to Excel which is very useful. However, it shows up in mph and Fahrenheit to me. How do I change them to km/h and Celsius? I tried changing the way it is displayed in the website but that doesn't change anything. Additionally, I tried around a bit myself but the result was not ideal, see attached file. Any other ideas on how to solve this? The first time I imported data, it worked fine for the first test country but for the second one, it didn't. 

 

Thank you in advance!

6 Replies

  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    Juri_Messenboeck 

    And there is much easier solution - instead of using default US region in URL like for Berlin

    https://weather.com/weather/tenday/l/Berlin+Germany?canonicalCityId=49875d033efa3f8a515cc07b7ab1897df30ce65616175ffb4591cb95c5f646ff

    use the link with your preferable region/language, e.g. UK/English URL for the Berlin will be

    https://weather.com/en-UK/weather/tenday/l/5ca23443513a0fdc1d37ae2ffaf5586162c6fe592a66acc9320a0d0536be1bb9

    (region code after weather.com/).

    Result will be in units for this region. I have no idea why km/h is default for UK and why US and UK forecast for Berlin are bit different. Script is simple then

    let
        Source = Web.Page(Web.Contents("https://weather.com/en-UK/weather/tenday/l/5ca23443513a0fdc1d37ae2ffaf5586162c6fe592a66acc9320a0d0536be1bb9")),
        Data0 = Source{0}[Data],
        ColumnNames=List.FirstN(Table.ColumnNames(Data0),6),
        RemoveDayColumn = Table.RemoveColumns(Data0,{"Day"}),
        TempColumnNames = Table.ColumnNames(RemoveDayColumn),
        ListOfList = List.Zip({TempColumnNames,ColumnNames}),
        RenameTableColumns = Table.RenameColumns(RemoveDayColumn, ListOfList),
        DayInOneString = Table.ReplaceValue(
            RenameTableColumns,
            "#(cr)#(lf)",
            " ",
            Replacer.ReplaceText,{"Day"}
        )
    in
        DayInOneString

    See in second sheet for Berlin attached.

  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    Juri_Messenboeck 

    Changing units on site is done by java script, it doesn't affect returned table.

     

    It's much better to do all transformations within the query - more reliable and to avoid risk of not syncing added data with returned by query.

     

    I did that for Berlin, you may check steps in attached file. Script is

    let
        Source = Web.Page(Web.Contents("https://weather.com/weather/tenday/l/Berlin+Germany?canonicalCityId=49875d033efa3f8a515cc07b7ab1897df30ce65616175ffb4591cb95c5f646ff")),
        Data0 = Source{0}[Data],
        ColumnNames=List.FirstN(Table.ColumnNames(Data0),6),
        RemoveDayColumn = Table.RemoveColumns(Data0,{"Day"}),
        TempColumnNames = Table.ColumnNames(RemoveDayColumn),
        ListOfList = List.Zip({TempColumnNames,ColumnNames}),
        RenameTableColumns = Table.RenameColumns(RemoveDayColumn, ListOfList),
        SplitWind = Table.SplitColumn(
            RenameTableColumns,
            "Wind",
            Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv),
            {"Wind.1", "Wind.2", "Wind.3"}
        ),
        VelocityToNumber = Table.TransformColumnTypes(
            SplitWind,
            {
                {"Wind.1", type text},
                {"Wind.2", Int64.Type},
                {"Wind.3", type text}
            }
        ),
        MphToKmh = Table.AddColumn(
            VelocityToNumber,
            "Custom",
            each Number.ToText([Wind.2]*1.60934,"F1")
        ),
        ReplaceUnit = Table.ReplaceValue(
            MphToKmh,
            "mph","km/h",
            Replacer.ReplaceText,{"Wind.3"}
        ),
        AddNewWind = Table.AddColumn(
            ReplaceUnit,
            "Wind",
            each Text.Combine(
                {[Wind.1], [Custom], [Wind.3]}, " "
            ),
            type text
        ),
        ReplaceNoTempreture = Table.ReplaceValue(
            AddNewWind,
            "--","--°",
            Replacer.ReplaceText,
            {"High / Low"}
        ),
        SplitByDegreeCharacter = Table.SplitColumn(
            ReplaceNoTempreture,
            "High / Low",
            Splitter.SplitTextByDelimiter("°", QuoteStyle.Csv),
            {"High/Low.1", "High/Low.2", "High/Low.3"}
        ),
        DegreeToNumber = Table.TransformColumnTypes(
            SplitByDegreeCharacter,
            {
                {"High/Low.1", Int64.Type},
                {"High/Low.2", Int64.Type},
                {"High/Low.3", type text}
            }
        ),
        HighDfToDc = Table.ReplaceValue(
            DegreeToNumber,
            each [#"High/Low.1"],
            each Number.ToText(([#"High/Low.1"] - 32)*5/9,"F1"),
            Replacer.ReplaceValue,{"High/Low.1"}
        ),
        LowDfToDc = Table.ReplaceValue(
            HighDfToDc,
            each [#"High/Low.2"],
            each Number.ToText(([#"High/Low.2"] - 32)*5/9,"F1"),
            Replacer.ReplaceValue,{"High/Low.2"}
        ),
        ReplacErrors = Table.ReplaceErrorValues(
            LowDfToDc,
            {
                {"High/Low.1", "--"},
                {"High/Low.2", "--"}
            }
        ),
        InsertNewHighLow = Table.AddColumn(
            ReplacErrors,
            "High / Low",
            each Text.Combine({[#"High/Low.1"], [#"High/Low.2"]}, "°") & "°",
            type text
        ),
        DayInOneString = Table.ReplaceValue(
            InsertNewHighLow,
            "#(cr)#(lf)",
            " ",
            Replacer.ReplaceText,{"Day"}
        ),
        RemoveUnused = Table.SelectColumns(
            DayInOneString,
            {"Day", "Description", "High / Low", "Wind", "Precip", "Humidity"}
        )
    in
        RemoveUnused

     

    • Juri_Messenboeck's avatar
      Juri_Messenboeck
      Copper Contributor

      SergeiBaklan thank you for this, I appreciate it! However, when I paste the code (regardless which one) into the advanced editor in the power query editor, it says there is a "Token eof error", see image attached. The problem in both cases appears to be "let" at the beginning of both java scripts. 

      How do I circumvent this so that it runs because it doesn't let me close it without removing the token eof error?

      Thanks again for helping 

      • SergeiBaklan's avatar
        SergeiBaklan
        Diamond Contributor

        Juri_Messenboeck 

        You shall not paste but replace the code. It shall be no two sequential let ... in blocks in one query, only one. In this sample we have EOF error since first in ends the script

        let
            One = "a"
        in
            One
        
        let
            Two="b"
        in
            Two

        However, blocks could be nested, like

        let
            One = "a",
            Two = let Two="b" in Two
        in
            One & Two

        but that's not your case here.

        By the way, script which works in Power Query is called M-script. Java script works on web site, weather.com.