Forum Discussion

Stilish's avatar
Stilish
Frequent Visitor
6 years ago

How to iterate through the rows in a Power Query table applying formulas to each row's data

I am trying to build a FX rate regression model using Power Query that tests a stop loss strategy looking back at historical exchange rates.

I've developed my model to the point where I need to start iterating through the rows(each row represents a time period) in the query to determine when the relevant stop-loss will trigger and at what rate. I've included a picture below that hopefully explains what I am trying to do.

I would be most grateful if someone could guide me how to do this. I have had a look at List.Accumulate but struggling to see how it would help. Greg_Deckler and edhans , you guys seem to really know your stuff..😁

Thanks

6 Replies

    • Stilish's avatar
      Stilish
      Frequent Visitor

      Hi edhans

      Thanks for your message.

      Here is the link to a google drive folder with 2 files in it

      Google Drive Link 

      The first file is simply the file I copied the picture from.

      The second file is my actual model file with Power Query connections included. Please excuse my novice attempt.

      The "Detailed Steps Excel Basic" sheet sets out how I would achieve my objective using basic excel. I have since enhanced the file by improving the formulas and also by passing the data to an array and using VBA. But I need to use Power Query for larger data sets.

      Only the first two rows calculations are included but obviously they would need to be repeated for each row in the data set.

      Thanks again

      • edhans's avatar
        edhans
        Community Champion

        Ok. I am not sure I 100% understand what this is doing. I trade ETFs about once every 5 years, so stop losses and such are way outside of my zone. đŸ˜

         

        I used your Excel sheet to generate the query in the code box below, and the last column is the math I think you want. The key is this formula:

        if [open] < [#"Stop-Loss"] and [#"Max Stop-Loss"] <= [#"Stop-Loss"]
            then List.Max({[#"Max Stop-Loss"],[low]})
        else
            if [low] < [#"Max Stop-Loss"] 
            then [#"Max Stop-Loss"] 
        else 0

         

        See if that works. If not, ping back with where I blew it in understanding what you needed here. 

         

        1) In Power Query, select New Source, then Blank Query
        2) On the Home ribbon, select "Advanced Editor" button
        3) Remove everything you see, then paste the M code I've given you in that box.
        4) Press Done

         

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcvdCYAwDATgVaTP0ibNT6+uUrr/GhqsKBICH7ncGIlLLZUYGw62tCfWDHFfcmnPzbBk6KHYt0t0p27XBC0LumCVVLwxR0C5codCf/48zXkC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [time = _t, open = _t, high = _t, low = _t, close = _t, Period = _t, RefRateTime2 = _t, #"Reference Rate.Reference Rate" = _t, #"Take Profit" = _t, #"Stop-Loss" = _t, #"Loss Interval" = _t, #"Adjustment Interval" = _t, #"Max Stop-Loss" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"time", type datetime}, {"open", type number}, {"high", type number}, {"low", type number}, {"close", type number}, {"Period", Int64.Type}, {"RefRateTime2", type datetime}, {"Reference Rate.Reference Rate", type number}, {"Take Profit", type number}, {"Stop-Loss", type number}, {"Loss Interval", type number}, {"Adjustment Interval", type number}, {"Max Stop-Loss", type number}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [open] < [#"Stop-Loss"] and [#"Max Stop-Loss"] <= [#"Stop-Loss"]
            then List.Max({[#"Max Stop-Loss"],[low]})
        else
            if [low] < [#"Max Stop-Loss"] 
            then [#"Max Stop-Loss"] 
        else 0, type number)
        in
            #"Added Custom"