Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount.
Register nowI 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
I'm not clear exactly on what that pic is showing.
Can you share some sample data?
How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingHi edhans
Thanks for your message.
Here is the link to a google drive folder with 2 files in it
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
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"
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingHi @edhans
Thanks for your response. A couple of things:
Thanks, I look forward to your response.
Using the same numbering as you did:
I think your question is good one and would be an interesting project to migrate to Power BI. Again, 100% confident it can be done, but it is, I think, really outside the scope of a question in these forums.
Does that make sense? Not trying to brush this off, but my next step honestly is to stop posting in the forum with something like this and arranging a phone call, screen share, and getting a deeper understanding of what the Excel model is doing, and the exploding that logic and putting it back together in the world of Power BI.
I think if I were you and didn't want to call in a consultant, it would be to largely do what I've described above, then post back specific questions on specific issues - and there could be half a dozen or more - vs trying to get this in one response.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingThanks @edhans
OK, I understand re: where you got the Max Stop-Loss from. As you can see in excel it is a calculated field which I would want to replicate in PQ.
Based on your whole comment I don’t think it’s relevant to deal specifically with “a” through “e”, except to say that it jumps from 3 to 41 because I needed the intervening rows to set out my explanation in column Q; otherwise it would have gone from row 3 to row 4. 🙂
f. Yes that is the conclusion I am coming to myself. See 4 below.
2. I think it just confused things because of the intervening rows. That’s why I put the actual source file into the folder.
3. OK
4. This is precisely what I was thinking. It does seem like DAX would do a better job; the question is how to get the data into the Data Model. Do I bring it directly from the source or do I take it as far as I have/can in PQ and then pass the final query over to the data model and continue with the iteration formulas from there? What do you think and what is the best way to do this? Also, will the link between PQ and data model remain so that if I change one of the inputs to PQ it will flow through to the Data Model?
If you are willing to do a Teams call and then I can screen share and talk you through the logic if that will enable you to answer my questions in point 4. And from there I agree I will then attempt the solution myself rather than use a consultant as I’m enjoying the challenge.
Thanks again. I look forward to hearing from you.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
If you love stickers, then you will definitely want to check out our Community Sticker Challenge!
User | Count |
---|---|
30 | |
23 | |
16 | |
15 | |
11 |